在PHP中的SQLSRV PDO中创建MS SQL临时表

时间:2015-07-04 23:22:12

标签: php sql-server pdo

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

ISSUE:

在php中,我试图用准备好的语句做同样的事情。

尝试1:代码如下。我在第二次执行时收到无效对象#ViewQuestionComments错误。

尝试2:我尝试准备每个sql然后执行一次,然后在执行时得到相同的错误。

尝试3:我还尝试连接所有sql并在一个prepare和execute语句中运行同样的错误。

有关如何以正确方式执行此操作的任何建议吗?

以下是我正在运行的代码。

        $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);

2 个答案:

答案 0 :(得分:1)

尽管您的高级程序员不喜欢它们,但我经常在有意义的情况下使用存储过程。

从我的第一手经验来看,大多数sql活动(不是单个select,update,insert或delete语句)都可以从存储过程中受益。与工具箱中的任何工具一样,它取决于您正在做什么。当我构建临时表时,它是在存储过程中完成的,因为它是一个复杂的动作,并且经常涉及大量的sql。

在您的示例中,检查对象是否存在,删除它,构建新的数据库对象,添加数据,然后将数据拉出是imho,这是一个强烈建议存储过程的系列操作。其他人可能会争论一些数据访问对象。无论如何,您都希望将其封装起来以保持产品的可维护性。存储过程做得很好。与一次发送一个组件sql语句相比,它们还需要更少的网络流量。它们通常比在线sql运行得更快,而不仅仅是因为网络延迟,数据库所做的许多事情之一是为它运行的任何sql生成执行计划,而准备好的语句帮助,存储过程帮助更多。到目前为止,人们对它们的最大抱怨,从我所看到的,是sql不像java或C#或Objective C或任何其他语言那样强大的编程语言。还有其他一些人列为缺点的东西,但是能够以轻松和高效的方式做你需要做的事情是最重要的。

同样,它们是工具箱中的工具之一,并且像任何工具一样,有时间和地点使用它们。但不要单方面解雇他们,那将是一个错误。就个人而言,多年来我使用它们已经非常好了,随着我对sql的能力和知识的增加,我发现它们的用途更好。了解所有工具。

查看Creating Stored Procedures的t-sql文档 如果你想要更多关于存储过程的利弊,那么互联网就充满了这一点。以下是一些链接。

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

Why use Stored Procedures?

Stored Procedures Are Evil

Google&#34;存储过程与内联sql&#34;你会得到更多。请记住,正确的工作是正确的工具。

答案 1 :(得分: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();