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...
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?
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!
2 Answers
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...).