Exit the Current Scope without returning a value in SQL Server

You can use the RETURN statement to discontinue the execution of a the T-SQL batch statement.

For example , you want to display the Employees whose MaritalStatus is Divorced. You donot want the SQL Statements following it to be executed if this condition doesn’t match. You can use the IF NOT EXISTS and use the RETURN statement as shown in this example. The second statement wont be executed because there are no employees whose MaritalStatus is D.

Exit the Current Scope without returning a value in SQL Server

use AdventureWorks2014
GO
IF NOT EXISTS (SELECT BusinessEntityID     
                FROM HumanResources.Employee   
                 WHERE MaritalStatus = 'D')  
BEGIN     
    RETURN;  
END;  
SELECT *  FROM HumanResources.Employee   WHERE  MaritalStatus = 'D'; 
Exit the Current Scope without returning a value in SQL Server
%d bloggers like this: