For future users: the bottom of this question contains a revised working code.
I know that 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 that 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
CREATE TABLE
(
commentid INT DEFAULT ((0)),
userid INT DEFAULT ((0)),
comment VARCHAR(max) DEFAULT '',
datemodified SMALLDATETIME,
username NVARCHAR(200) DEFAULT '',
points INT DEFAULT ((0))
);
INSERT INTO
(
commentid,
userid,
comment,
datemodified
)
SELECT id,
userid,
comment,
datemodified
FROM hanoncs_askme.hanoncs_hanoncs.comments
WHERE postid=1
AND status=1;
UPDATE
SET username = m.username
FROM
LEFT JOIN hanoncs_securelogin.hanoncs_hanoncs.members m
ON m.id = c.userid;
UPDATE
SET points =
(
SELECT Count(*)
FROM hanoncs_askme.hanoncs_hanoncs.commentvotes
WHERE postid=c.commentid)
FROM
SELECT *
FROM
IF @@ERROR != 0
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
END
In MS SQL Management Studio, this returns the temp table as I want:
EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = 1
I call 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
CREATE TABLE
(
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
(
commentid,
userid,
comment,
datemodified
)
SELECT id,
userid,
comment,
datemodified
FROM hanoncs_askme.hanoncs_hanoncs.comments
WHERE postid=1
AND status=1;
UPDATE
SET username = m.username , Avatar = m.avatar
FROM
LEFT JOIN hanoncs_securelogin.hanoncs_hanoncs.members m
ON m.id = c.userid;
UPDATE
SET points =
(
SELECT Count(*)
FROM hanoncs_askme.hanoncs_hanoncs.commentvotes
WHERE postid=c.commentid)
FROM
SELECT *
FROM
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);