Normally in asp.net I can run many queries one after another to create my temp tables. We pull from a lot of tables so its a huge speed advantage. I would then do a select from #999.
GOAL:
This is one of my pages in 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; ");
ISSUE:
In php I am trying to do the same thing with prepared statements.
Attempt1: The code is below. I'm getting invalid object #ViewQuestionComments error on the second execute.
Attempt2: I tried to prepare each sql and then execute once and I get the same error on that execute.
Attempt3: I also tried to concatenate all the sql and run in one prepare and execute statement with the same error.
Any suggestions on how to do this the right way?
Here is the code I'm running.
$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);