RETURN

The RETURN statement in SQL Server is a fundamental control-of-flow statement used to exit immediately from a stored procedure, function, or batch and optionally return an integer status code to the calling program or batch. It is a critical tool for managing execution flow, signaling success or failure, and controlling the behavior of T-SQL modules.

Purpose and Use Cases

Exit Stored Procedures or Functions: RETURN is used to terminate execution and exit from stored procedures or user-defined functions.
Return Status Codes: It allows returning an integer value, often used as a status or error code, to indicate the outcome of the procedure or function.
Control Execution Flow: It helps in conditionally stopping execution based on logic, such as error handling or validation failures.
Signal Success or Failure: Calling programs or scripts can check the returned value to determine if the operation succeeded or failed.

Syntax

RETURN [ integer_expression ];

The integer_expression is optional.
If omitted, the default return value is 0, which typically indicates success.
The return value must be an integer or an expression that evaluates to an integer.

How It Works

When SQL Server encounters a RETURN statement inside a stored procedure, function, or batch, it immediately stops execution and returns control to the caller. If an integer value is specified, that value is passed back to the caller as the return code.

Example Usage

Basic RETURN in a Stored Procedure

CREATE PROCEDURE CheckAge
    @Age INT
AS
BEGIN
    IF @Age < 18
    BEGIN
        PRINT 'Age is less than 18. Exiting procedure.';
        RETURN 1;  -- Return error code 1
    END

    PRINT 'Age is 18 or older. Continuing procedure.';
    -- Additional logic here

    RETURN 0;  -- Return success code 0
END

Calling the Procedure and Checking Return Value

DECLARE @Result INT;

EXEC @Result = CheckAge @Age = 16;

IF @Result = 1
    PRINT 'Procedure returned error: Age less than 18.';
ELSE
    PRINT 'Procedure completed successfully.';

Important Considerations

Return Value Type: Only integer values can be returned by RETURN. To return other data types, use output parameters or result sets.
Return vs Output Parameters: RETURN is for status codes; output parameters are for returning data.
Multiple RETURN Statements: You can have multiple RETURN statements in a procedure or function to exit at different points based on conditions.
Functions Must Use RETURN: User-defined functions must use RETURN to return a value.
Batch Termination: In a batch script, RETURN exits the batch immediately.

RETURN vs THROW vs RAISERROR

Statement Purpose
RETURN Exit and return an integer status
RAISERROR Raise an error message (older)
THROW Raise an exception (recommended)

While RETURN is useful for flow control, it does not generate an exception or rollback a transaction.

Practical Scenarios for Using RETURN

Error Handling: Return error codes to indicate failure conditions.
Input Validation: Exit early if input parameters do not meet criteria.
Conditional Logic: Stop execution when certain business rules are not met.
Status Reporting: Signal success or specific outcomes to calling applications.

Summary

The RETURN statement in SQL Server is a vital control-of-flow mechanism for exiting stored procedures, functions, and batches while optionally returning an integer status code. It facilitates robust error handling, clear execution flow control, and effective communication between T-SQL modules and calling programs. Mastery of RETURN is essential for writing maintainable, reliable, and professional SQL Server code.