存储过程更新了多少行

时间:2015-02-09 10:55:04

标签: sql-server sql-server-2012

我创建了这个存储过程:

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

SET @UpdateRowCount = @@ROWCOUNT

参考:

@@ROWCOUNT (Transact-SQL)

  

返回最后一个语句影响的行数。如果行数超过20亿,请使用ROWCOUNT_BIG。