How to stop the execution of a Stored Procedure using SQL Server?

Lets say I have a stored procedure which has a simple IF block. If the performed check meets the criteria, then I want to stop the procedure from further execution.

What is the best way to do this?

  • Is sql injection possible in stored procedures with text param?
  • How to protect a database from the Server Administrator in Sql Server
  • I'm looking for a reliable way to verify T-SQL stored procedures. Anybody got one?
  • Convert value into a URL friendly format - Unicode decomposition ähhh
  • Get distinct values from each table and each column with SQL Server
  • Load very big CSV-Files into s SQL-Server database
  • Here is the code:

    IF EXISTS (<Preform your Check>)
    BEGIN
        // NEED TO STOP STORED PROCEDURE EXECUTION
    END
    ELSE
    BEGIN
        INSERT ()...
    END
    

    Thanks for any help with this!

  • Why can't I simply add an index that includes all columns?
  • SQL Server: Get data for only the past year
  • Must declare the scalar variable “@ID” for insert parameter
  • Mysterious CPU Activity in MS SQL 2005
  • Selecting nvarchar 'like' in a left join - inner join combination
  • Performance of SQL Server 2005 Query
  • 2 Solutions collect form web for “How to stop the execution of a Stored Procedure using SQL Server?”

    Just make a call to RETURN:

    IF EXISTS (<some condition>)
    BEGIN
        // NEED TO STOP STORED PROCEDURE EXECUTION
        RETURN
    END
    

    This will return the control back to the caller immediately – it skips everything else in the proc.

    You could simply put a jump label at the end of the SP body, and issue a GOTO in the first IF statement. Alternatively, you can extend the first BEGIN ... END block to contain the entire rest of the SP body, and reverse the condition (IF NOT EXISTS...).

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.