通常在asp.net中,我可以一個接一個地運行多個查詢來創建我的臨時表。我們從很多桌子拉出來,所以它具有巨大的速度優勢。然後我會從#999中選擇一個。在SQLSRV中創建MS SQL臨時表PHP中的PDO
目標:
這是我在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錯誤。
嘗試2:我試圖準備每個SQL,然後執行一次,我得到相同的錯誤執行。
嘗試3:我也嘗試連接所有的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);
是我的標題說,MS SQL以及。我會看一看。 –
瞭解版本將允許直接鏈接到該版本的文檔。一旦你關注鏈接,你仍然可以切換版本。 – WillG
感謝您更新您的答案。我只是讀了這個,http://programmers.stackexchange.com/questions/158534/pros-and-cons-of-holding-all-the-business-logic-in-stored-procedures-in-web-appl和the答案和評論有一些很好的反對使用存儲過程的論點。由於我們沒有DBA,而且我不擅長調優SQL。我們也有多個程序員。所以我想我可能只是將SQL保存在代碼中。 –