0

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:

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

Returns 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.

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

2 Answers 2

4

Append OUTPUT Keyword when executing the Procedure:

DECLARE @returnCode INT
EXEC myProcedure 1, 1, @returnCode OUTPUT
SELECT @returnCode
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.