Return Select * from PHP PDO Stored Procedure In MS SQL

好久不见. 提交于 2020-01-02 07:07:24

问题


For future users: The bottom of this question contains the corrected working code.

I know Select * is not the best, but in this example, I am trying to call a stored procedure from php and return the ENTIRE result set so I can loop through the array in my code.

Here is my current stored procedure:

USE [hanoncs_AskMe]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON;
GO

CREATE PROCEDURE [hanoncs_hanoncs].[CommentsTemp] 
    @QuestionID INT


 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=1
        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

In MS SQL Management Studio, this returns the temp table like I want:

EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = 1

I am calling it in php with:

    $stmt = $PDO->prepare('EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = ?');
    $stmt->bindParam(1, $QuestionID, PDO::PARAM_INT);
    $stmt->execute();
    $rows6 = $stmt->fetch(PDO::FETCH_BOTH);

The error I get is:

PDOException  SQLSTATE[IMSSP]: The active result for the query contains no fields.

/

EDIT:For future users!!! The working code is below.

Stored Procedure:

USE [hanoncs_AskMe]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [hanoncs_hanoncs].[CommentsTemp] 
    @QuestionID INT OUTPUT


 AS

 BEGIN
 SET NOCOUNT ON;
    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)),
                                  Avatar nvarchar(200) DEFAULT ''
                     );


                     INSERT INTO #viewquestioncomments 
                    ( 
                                commentid, 
                                userid, 
                                comment, 
                                datemodified 
                    ) 
        SELECT id, 
               userid, 
               comment, 
               datemodified 
        FROM   hanoncs_askme.hanoncs_hanoncs.comments 
        WHERE  postid=1
        AND    status=1;


        UPDATE #viewquestioncomments 
        SET       username = m.username , Avatar = m.avatar
        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

PHP:

   $stmt = $PDO->prepare('EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = ?');
   $stmt->bindParam(1, $QuestionID, PDO::PARAM_INT);
   $stmt->execute();
   $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);  

回答1:


Try adding SET NOCOUNT ON; inside the stored procedure (not above where you are creating the procedure).

Add it after the AS BEGIN and before the BEGIN TRANSACTION;

Like so:

CREATE PROCEDURE [hanoncs_hanoncs].[CommentsTemp] 
    @QuestionID INT
 AS  BEGIN
    SET NOCOUNT ON;
    BEGIN TRANSACTION

From what it looks like to me, your result set will contain the row counts of affected rows first, then the actual data you want back.



来源:https://stackoverflow.com/questions/31272566/return-select-from-php-pdo-stored-procedure-in-ms-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!