为什么SQL rowcount没有在我的存储过程输出参数中返回?

时间:2022-02-13 02:16:20

I have a stored procedure with pseudocode like this:

我有一个伪代码的存储过程,如下所示:

ALTER PROCEDURE myProcedure(@param1 int, @param2 int, @returnCode int output)
AS 
BEGIN
   SELECT .... -- my query here

   SET @returnCode = @@ROWCOUNT
END

However, when I execute this stored procedure, @returnCode is NULL:

但是,当我执行此存储过程时,@returnCode为NULL:

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode
SELECT @returnCode

Returns NULL.

返回NULL。

However, if I just do a select within the proc rather than setting the return code - SELECT @@ROWCOUNT - I get the correct row count.

但是,如果我只是在proc中进行选择而不是设置返回代码 - SELECT @@ ROWCOUNT - 我得到正确的行数。

How can I return this row count in the output param?

如何在输出参数中返回此行数?

2 个解决方案

#1


4  

Append OUTPUT Keyword when executing the Procedure:

执行过程时附加OUTPUT关键字:

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode OUTPUT
SELECT @returnCode

#2


0  

You missed output parameter.

你错过了输出参数。

CREATE PROCEDURE myProcedure(@param1 int, @param2 int, @returnCode int output)
AS 
BEGIN
   SELECT 1
   UNION 
   SELECT 2;
   SET @returnCode = @@ROWCOUNT
END

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode OUTPUT
SELECT @returnCode

#1


4  

Append OUTPUT Keyword when executing the Procedure:

执行过程时附加OUTPUT关键字:

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode OUTPUT
SELECT @returnCode

#2


0  

You missed output parameter.

你错过了输出参数。

CREATE PROCEDURE myProcedure(@param1 int, @param2 int, @returnCode int output)
AS 
BEGIN
   SELECT 1
   UNION 
   SELECT 2;
   SET @returnCode = @@ROWCOUNT
END

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode OUTPUT
SELECT @returnCode