Get RETURN value from stored procedure in SQL

SqlSql Server

Sql Problem Overview


I have a stored procedure where it ends with a RETURN value of 0 or 1.

I want to use this value in an IF statement in another stored procedure.

How can I get the return value of the former stored procedure and save it in a variable in the latter?

I couldn't find anything related. All questions are about fetching the RETURN values in C#.

I was thinking, maybe something like this :

SP_Two

DECLARE @returnValue INT
SET @returnValue = EXEC SP_One

IF @returnValue = 1
BEGIN
   --do something
END
ELSE
BEGIN
   --do something else
END

Sql Solutions


Solution 1 - Sql

This should work for you. Infact the one which you are thinking will also work:-

 .......
 DECLARE @returnvalue INT

 EXEC @returnvalue = SP_One
 .....

Solution 2 - Sql

The accepted answer is invalid with the double EXEC (only need the first EXEC):

DECLARE @returnvalue int;
EXEC @returnvalue = SP_SomeProc
PRINT @returnvalue

And you still need to call PRINT (at least in Visual Studio).

Solution 3 - Sql

Assign after the EXEC token:

DECLARE @returnValue INT

EXEC @returnValue = SP_One

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhermannView Question on Stackoverflow
Solution 1 - SqlRahul TripathiView Answer on Stackoverflow
Solution 2 - SqlNicholas PetersenView Answer on Stackoverflow
Solution 3 - SqlAlex K.View Answer on Stackoverflow