
Applies to SQL Server 2019, 2022 and 2025.
The T-SQL RETURN statement does two jobs. In a stored procedure it stops the procedure and hands an integer status code back to the caller. In a scalar function it’s how the function gives back its value at all. Both are simple until the RETURN is missing, and then the two behave very differently: the procedure quietly hands back something you didn’t choose, and the function won’t even compile.
How the T-SQL RETURN statement works in a procedure
A procedure can end with RETURN n, where n is an int. The caller picks it up with EXEC @rc = procedure_name. The usual convention, and the one SQL Server’s own system procedures follow, is 0 for success and anything else for a failure you’ve defined yourself. If the procedure reaches the end without a RETURN, or uses a bare RETURN with no value, the caller gets 0.
CREATE OR ALTER PROCEDURE dbo.ReserveStock
@ProductID int,
@Qty int
AS
BEGIN
SET NOCOUNT ON;
IF @Qty <= 0
RETURN 1; -- bad quantity
IF @ProductID NOT IN (1, 2, 3)
RETURN 2; -- no such product
-- the real work would go here
RETURN 0; -- success
END;
GO
CREATE OR ALTER PROCEDURE dbo.NoReturn
AS
SET NOCOUNT ON;
DECLARE @x int = 1; -- does its work, never says RETURN
GO
CREATE TABLE #CallLog (CallDescription varchar(30), ReturnCode int);
DECLARE @rc int;
EXEC @rc = dbo.ReserveStock @ProductID = 1, @Qty = 5;
INSERT #CallLog VALUES ('Good request', @rc);
EXEC @rc = dbo.ReserveStock @ProductID = 1, @Qty = 0;
INSERT #CallLog VALUES ('Zero quantity', @rc);
EXEC @rc = dbo.ReserveStock @ProductID = 9, @Qty = 5;
INSERT #CallLog VALUES ('Unknown product', @rc);
EXEC @rc = dbo.NoReturn;
INSERT #CallLog VALUES ('No RETURN at all', @rc);
SELECT CallDescription, ReturnCode FROM #CallLog;
That gives 0, 1, 2 and 0. The last one is the point: a procedure with no RETURN still has a return code, and it’s 0.
Two other rules worth knowing. RETURN can’t hand back NULL: RETURN @status with @status NULL gives a warning (“attempted to return a status of NULL, which is not allowed”) and the caller gets 0. And it only ever returns an int, so it’s for status, not data. Values go back through OUTPUT parameters or a result set, as I covered in CREATE PROCEDURE in SQL Server.
The common mistake: trusting a missing RETURN
The mistake I see is calling code that checks IF @rc <> 0 against a procedure that never sets a return code, and taking 0 to mean “it worked”. Here’s what really comes back when a procedure without a RETURN hits an error:
CREATE OR ALTER PROCEDURE dbo.SplitBill
@Total money,
@People int
AS
SET NOCOUNT ON;
SELECT @Total / @People AS EachPays;
GO
DECLARE @rc int = 0;
EXEC @rc = dbo.SplitBill @Total = 60, @People = 0;
SELECT @rc AS ReturnCode;
The divide by zero (error 8134) doesn’t stop the batch, and on SQL Server 2025 @rc comes back as -6. A primary key violation gave me -4. SQL Server sets these negative values itself when a procedure without its own RETURN hits an error, but Microsoft doesn’t document what each one means, so treat “not zero” as the only thing you can rely on.
It gets worse. Put a RETURN 0 at the end of that procedure and the error is followed by a clean 0, because execution carries on to the RETURN. And some errors never assign the variable at all:
CREATE OR ALTER PROCEDURE dbo.ReadArchive
AS
SELECT OrderID FROM dbo.OrderArchive; -- table doesn't exist
GO
DECLARE @rc int = 0;
EXEC @rc = dbo.ReadArchive;
SELECT @rc AS ReturnCode;
Error 208 is raised, the procedure is abandoned, and @rc keeps whatever it held before. I set it to 0, so it reads as success. The same happens when the error is caught by a TRY…CATCH around the EXEC: the assignment never takes place.
So a return code is only as good as the error handling inside the procedure. If it matters, use TRY…CATCH in the procedure, RETURN a non-zero value (or THROW) from the CATCH block, and initialise @rc to something that can’t be mistaken for success, such as -1. My notes on error handling in transactions show the pattern I use.
Functions: RETURN must be the last statement
Scalar functions are stricter. The last statement inside BEGIN…END has to be a RETURN, and the parser checks that by position, not by working out whether every path returns. This looks complete but won’t compile:
CREATE OR ALTER FUNCTION dbo.StockBand (@Qty int)
RETURNS varchar(10)
AS
BEGIN
IF @Qty > 0
RETURN 'In stock';
ELSE
RETURN 'Out';
END;
GO
It fails with error 455: “The last statement included within a function must be a return statement.” Both branches return, but the last statement is the IF, not a RETURN. The fix is to set a variable and return it once at the end:
CREATE OR ALTER FUNCTION dbo.StockBand (@Qty int)
RETURNS varchar(10)
AS
BEGIN
DECLARE @Band varchar(10) = 'Out';
IF @Qty > 0
SET @Band = 'In stock';
RETURN @Band;
END;
GO
SELECT dbo.StockBand(5) AS FivePieces, dbo.StockBand(0) AS NoPieces;
A single RETURN CASE ... END works too, and it’s what I’d normally write for something this small.
Watch out for
- 0 by default. No RETURN means 0, which reads as success whether or not it was.
- RETURN NULL becomes 0. You get a warning, not an error, so it’s easy to miss in a job log.
- Undocumented negative codes. Don’t write logic that depends on -4 or -6 meaning something in particular.
- Unassigned variables. Some errors leave @rc untouched, so don’t initialise it to 0.
- RETURN in a batch. Outside a procedure, RETURN just stops the batch. It can’t take a value there.
Microsoft’s reference is RETURN (Transact-SQL). There are more T-SQL notes in my SQL Server DBA Handbook.
This topic came up in the SQLServerCentral Question of the Day: Missing Returns.
Clive Roberts DBA