对于未来的用户:此问题的底部包含更正的工作代码。
我知道Select *不是最好的,但是在这个例子中,我试图从php调用存储过程并返回ENTIRE结果集,这样我就可以在我的代码中遍历数组。
这是我当前的存储过程:
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
在MS SQL Management Studio中,这将返回我想要的临时表:
EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = 1
我在php中调用它:
$stmt = $PDO->prepare('EXEC [hanoncs_hanoncs].[CommentsTemp] @QuestionID = ?');
$stmt->bindParam(1, $QuestionID, PDO::PARAM_INT);
$stmt->execute();
$rows6 = $stmt->fetch(PDO::FETCH_BOTH);
我得到的错误是:
PDOException SQLSTATE[IMSSP]: The active result for the query contains no fields.
/
编辑:对于未来的用户!工作代码如下。
存储过程:
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);
答案 0 :(得分:4)
尝试添加SET NOCOUNT ON;在存储过程内(不在您创建过程的位置上方)。
在AS BEGIN 之后添加在之前添加BEGIN TRANSACTION;
像这样:
CREATE PROCEDURE [hanoncs_hanoncs].[CommentsTemp]
@QuestionID INT
AS BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION
从我看来,结果集首先包含受影响行的行数,然后是您想要的实际数据。