Explain Codes LogoExplain Codes Logo

Get RETURN value from stored procedure in SQL

sql
stored-procedures
return-values
sql-scripts
Anton ShumikhinbyAnton ShumikhinΒ·Sep 20, 2024
⚑TLDR

Want to capture a stored procedure's RETURN value in SQL? Use a variable with the EXEC command. Simples! 🦝

DECLARE @Result INT; EXEC @Result = YourProcedureName; SELECT @Result;

The above executes YourProcedureName and stuffs its return value into @Result. You can then deploy this variable however you want.

A deeper dive into RETURN values

A Stored Procedure's RETURN value is essentially a way for it to communicate back. Using an INT datatype, it’s usually used to illustrate success or failure.

Creating a variable box for your RETURN value

You declare a variable to hold onto that precious RETURN value.

DECLARE @ReturnValue INT;

Next, you're going to assign your stored procedure's return value to this variable.

EXEC @ReturnValue = YourProcedureName;

Using the RETURN value: If it fits, I sits

IF-ELSE statements are your conditional friends, navigating your program based on the RETURN value.

IF @ReturnValue = 0 BEGIN PRINT 'The DBA walks into a bar, spots two tables and says... JOIN please!'; END ELSE BEGIN PRINT 'An error occurred: ' + CAST(@ReturnValue AS VARCHAR); -- Might need more cowbell next time END

Spilling the beans with PRINT

PRINT statement for that sweet, sweet debug life.

PRINT 'Returned: ' + CAST(@ReturnValue AS VARCHAR);

Watch as your RETURN value is beautifully displayed in a human-readable format.

One compact SQL script to rule them all

DECLARE the variable, CAPTURE the RETURN value, EVALUATE it, and PRINT the result, all in one go.

DECLARE @MyReturn INT; EXEC @MyReturn = MyProcedure; IF @MyReturn = 0 PRINT 'Looks good!'; ELSE PRINT 'Oops: ' + CAST(@MyReturn AS VARCHAR);

How to juggle multiple return values and use OUTPUT parameters

Don't fret when things get complex. SQL's got your back!

Capturing those cheeky OUTPUT parameters

Use OUTPUT parameters for more data goodness, besides just the integer return code:

DECLARE @Return INT, @OutParam VARCHAR(100); EXEC @Return = MyProcedureWithOutput @Data = @OutParam OUTPUT; PRINT @OutParam;

When one procedure isn't enough

Running multiple stored procedures? Keep track of each RETURN value:

DECLARE @FirstResult INT, @SecondResult INT; EXEC @FirstResult = FirstProcedure; EXEC @SecondResult = SecondProcedure;

Scaling the logic ladder with complex branching

For complex decision pathways, CASE statements or nested IF-ELSE statements can help navigate through the messy web of RETURN values and OUTPUT parameters.

Keeping your SQL bullet-proof: errors and edge cases

We've all been there. Unexpected errors, sneaky NULL values. Here's how to manage these pitfalls like a pro.

Error handling 101: TRY-CATCH

Use TRY-CATCH blocks to keep things smooth:

BEGIN TRY DECLARE @Result INT; EXEC @Result = ErrorFoo; IF @Result = 0 PRINT 'Row row row your code, gently down the stream...'; END TRY BEGIN CATCH PRINT 'Merrily merrily merrily... Life is but a screaming nightmare of relational database management systems!' END CATCH

Void Emptiness (NULL)

NULL values can be sneaky. Use ISNULL() or COALESCE() functions to handle them:

IF ISNULL(@ReturnValue, -1) = 0 PRINT 'All systems go!'; ELSE PRINT 'Houston, we have a NULL!';

Dynamic SQL and scoped variables

Dynamic SQL comes with its own RETURN value quirks. Use sp_executesql:

EXEC sp_executesql @DynamicSQL, N'@DynamicResult INT OUTPUT', @DynamicResult OUTPUT;

This keeps variables within scope, ensuring everything is where it needs to be.