2015-07-04 96 views
0

通常在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); 

回答

1

不管事實,你的高級程序員不喜歡他們,我經常使用的存儲過程在有意義的這樣做。

從我第一次親身體驗最SQL活動,這是不是一個單一的選擇,更新,插入或刪除語句,可以受益於一個存儲過程之中。與工具箱中的任何工具一樣,這取決於你在做什麼。當我構建臨時表時,它在一個存儲過程中完成,因爲它是一個複雜的操作,並且通常涉及大量的sql。

在你的榜樣,檢查對象的存在,放棄它,建立一個新的數據庫對象,添加數據,然後再拉這些數據,就是,恕我直言,一系列的動作,將有力地表明一個存儲過程。其他人可能會爭辯一些數據訪問對象層。無論如何,你都想要封裝這些以保持產品的可維護性。存儲過程很好。與一次只發送一個組件sql語句相比,它們所需的網絡流量也較少。他們通常比內聯sql運行速度更快,不僅僅是因爲網絡延遲,數據庫所做的很多事情之一是爲它運行的任何sql生成執行計劃,而準備好的語句有助於存儲過程提供更多幫助。到目前爲止,人們對他們最大的抱怨,從我看到的是,sql不像java或C#或Objective C或任何其他語言那樣健壯的編程語言。還有其他的東西被人們列爲缺點,但能夠做到你需要做的事情是輕鬆和高效的。

再次,他們是在工具箱中的工具之一,像任何工具,有一個時間和地點使用它們。但不要單方面解僱他們,那將是一個錯誤。就個人而言,多年來我已經非常好的使用它們,隨着我對sql的能力和知識的增加,我發現它們更好用。瞭解所有的工具。

查看t-sql文檔Creating Stored Procedures 如果您想了解更多關於存儲過程的優點和缺點,那麼除了別的以外,互聯網充滿了這一點。這裏有幾個鏈接。

Determine when to use stored procedures vs. SQL in the code

Why use Stored Procedures?

Stored Procedures Are Evil

谷歌 「存儲過程VS聯SQL」,你會得到更多。請記住,正確的工作是正確的工具。

+0

是我的標題說,MS SQL以及。我會看一看。 –

+0

瞭解版本將允許直接鏈接到該版本的文檔。一旦你關注鏈接,你仍然可以切換版本。 – WillG

+0

感謝您更新您的答案。我只是讀了這個,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保存在代碼中。 –

1

使用存儲過程,它並不難:

CREATE PROC updateQuestion() 
AS 
BEGIN 
    BEGIN TRANSACTION 

     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)) 
        ); 
        INSERT INTO #viewquestioncomments 
        ( 
           commentid, 
           userid, 
           comment, 
           datemodified 
        ) 
     SELECT id, 
       userid, 
       comment, 
       datemodified 
     FROM hanoncs_askme.hanoncs_hanoncs.comments 
     WHERE postid=? 
     AND status=1; 
     UPDATE #viewquestioncomments 
     SET  username = m.username 
     FROM  #viewquestioncomments c 
     LEFT JOIN hanoncs_securelogin.hanoncs_hanoncs.members m 
     ON  m.id = c.userid; 
     UPDATE #viewquestioncomments 
     SET points = 
       ( 
         SELECT Count(*) 
         FROM hanoncs_askme.hanoncs_hanoncs.commentvotes 
         WHERE postid=c.commentid) 
     FROM #viewquestioncomments c; 
     SELECT * 
     FROM #viewquestioncomments"; 

    IF @@ERROR != 0 
     ROLLBACK TRANSACTION 
     ELSE 
      COMMIT TRANSACTION 
END 

GO 

用法:

$stmt= $PDO->prepare('EXEC updateQuestion'); 
$stmt->execute(); 
+0

我做了一個編輯我的問題。請看底部。 –

+0

在這裏看到我的答案http://stackoverflow.com/a/31213745/1880431 – meda

+0

謝謝,我試過:SET NOCOUNT ON ;.但仍然有錯誤。 –