存储过程更新了多少行

How many rows were updated by a stored procedure

我创建了这个存储过程:

CREATE PROCEDURE [dbo].[sp_put_question_responses] 
    @AnswerGridResponses     VARCHAR(20),   
    @UpdateRowCount     INT OUTPUT,
    @UserId             INT, 
    @UserTestQuestionId INT
AS
BEGIN   
   BEGIN        
      UPDATE  UserTestQuestion      
      SET     AnswerGridResponses = @AnswerGridResponses,
              Answered = 1
      WHERE   UserTestQuestionId = @UserTestQuestionId
      AND     UserId = @UserId
   END 
   SELECT @UpdateRowCount AS UpdateRowCount
END

我是这样称呼它的:

DECLARE @UpdateRowCount     INT;

exec dbo.sp_put_question_responses '1000',
                              @UpdateRowCount OUT,
                              '2',      
                              '3249'

SELECT @UpdateRowCount

SELECT userid, UserTestQuestionId from Usertestquestion
where  UserTestQuestionId = '3249'

但它没有为更新的行数提供正确的值:

UpdateRowCount
--------------
NULL

userid      UserTestQuestionId
----------- ------------------
2           3249

我做错了什么?

CREATE PROCEDURE [dbo].[sp_put_question_responses] 
    @AnswerGridResponses     VARCHAR(20),   
    @UpdateRowCount     INT OUTPUT,
    @UserId             INT, 
    @UserTestQuestionId INT
AS
BEGIN   

      UPDATE  UserTestQuestion      
      SET     AnswerGridResponses = @AnswerGridResponses,
              Answered = 1
      WHERE   UserTestQuestionId = @UserTestQuestionId
      AND     UserId = @UserId
  SET @UpdateRowCount = @@ROWCOUNT
   SELECT @UpdateRowCount AS UpdateRowCount
END

您没有将 @UpdateRowCount 设置为任何值,您只是传回空值。您可以使用 @@ROWCOUNT 到 return 受 SQL 语句影响的行数,如下所示:

SET @UpdateRowCount = @@ROWCOUNT

参考:

@@ROWCOUNT (Transact-SQL)

Returns the number of rows affected by the last statement. If the number of rows is more than 2 billion, use ROWCOUNT_BIG.