【发布时间】:2015-09-22 10:07:06
【问题描述】:
通常在 asp.net 中,我可以一个接一个地运行许多查询来创建我的临时表。我们从很多表中提取,因此它具有巨大的速度优势。然后我会从 #999 中进行选择。
目标:
这是我在 asp.net 中的页面之一:
sql.Append("Create Table #999 (SvID varchar(15) Default '', SvcID int Default ((0)), Value varchar(50) Default '', Store varchar(10) Default '', StoreID int Default ((0)), VisitDate varchar(100) Default '', Lead varchar(100) Default '', LeadID int Default ((0)),ShipTo varchar(100) Default '', ShipToAddress varchar(300) Default '', Parts Varchar(5000) Default '', ShipToID int Default ((0)), ShipToType Varchar(50) Default '', ProjID int Default ((0)), DivID Int Default ((0)), Combined int Default ((0)), HasTime int Default ((0)), DateOK Int Default ((0))); ");
//Insert StoreVisitID's
sql.Append("Insert into #999 (SvID) Select Distinct ID from StoreVisit where projid =" + ProjID + "; ");
//Update Store Visit Info
sql.Append("Update #999 Set SVcID = sub.svcid, Store = sub.Store, StoreID = sub.StoreID, ProjID = sub.ProjID, LeadID = sub.TeamLeadID, VisitDate = sub.VisitDate, DivID = sub.DivID, Value = sub.Status from #999 h Join (select svcID, Store, StoreID, ProjID, ID, TeamLeadID, Convert(Varchar, Visitdate, 1) as VisitDate, DivID, Status From StoreVisit where projid =" + ProjID + ") as sub on sub.id = h.svid; ");
//Update Store Visit Lead
sql.Append("Update #999 Set Lead = sub.Lead From #999 h Join ( Select id, FirstName + ' ' + LastName as Lead From Employee) as sub on sub.id = h.LeadID; ");
//Update ShipToID, ShipToType
sql.Append("Update #999 Set ShipToID = sub.ShipToID, ShipToType = sub.ShipToType From #999 h Join ( Select SviD, ShipToID, ShipToType from StoreVisit2) as sub on sub.svid = h.svid; ");
//Ship To Other Employee
sql.Append("Update #999 Set ShipTo = Sub.Lead + ' - Employee', ShipToAddress = sub.Address From #999 h Join (Select id, FirstName + ' ' + LastName as Lead, Address + '. ' + Address2 + ', ' + City + ', ' + State as Address From Employee) as sub on sub.id = h.ShipToID Where ShipToType='E' and ShipToID <> 0; ");
问题:
在 php 中,我试图用准备好的语句做同样的事情。
Attempt1: 代码如下。我在第二次执行时收到无效对象 #ViewQuestionComments 错误。
Attempt2: 我尝试准备每个 sql,然后执行一次,但我在执行时遇到了同样的错误。
Attempt3: 我还尝试连接所有 sql 并在一个准备和执行语句中运行,但出现相同的错误。
关于如何正确执行此操作的任何建议?
这是我正在运行的代码。
$sql = "IF OBJECT_ID('#ViewQuestionComments', 'U') IS NOT NULL DROP TABLE #ViewQuestionComments; Create Table #ViewQuestionComments (CommentID int default ((0)), UserID int default ((0)), Comment varchar(max) default '', DateModified smalldatetime, UserName nvarchar(200) default '', Points int default ((0)))";
$stmt = $PDO->prepare( $sql );
$stmt->execute();
$sql = "Insert Into #ViewQuestionComments (CommentID, UserID, Comment, DateModified) select ID, UserID, Comment, DateModified from hanoncs_askme.hanoncs_hanoncs.Comments Where PostID=? and Status=1";
$stmt = $PDO->prepare( $sql );
$stmt->bindParam(1, $QuestionID);
$stmt->execute();
$sql = "Update #ViewQuestionComments Set UserName = m.UserName From #ViewQuestionComments c Left Join hanoncs_securelogin.hanoncs_hanoncs.members m on m.id = c.UserID";
$stmt = $PDO->prepare( $sql );
$stmt->execute();
$sql = "Update #ViewQuestionComments set Points = (Select count(*) from hanoncs_askme.hanoncs_hanoncs.CommentVotes where PostID=c.CommentID) From #ViewQuestionComments c";
$stmt = $PDO->prepare( $sql );
$stmt->execute();
$sql = "Select * from #ViewQuestionComments";
$stmt = $PDO->prepare( $sql );
$stmt->execute();
$rows6 = $stmt->fetchAll(PDO::FETCH_BOTH);
【问题讨论】:
标签: php sql-server pdo