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!
You can use RETURN to stop execution of a stored procedure immediately. Quote taken from Books Online: Exits unconditionally from a query or procedure. RETURN is immediate and complete and can be used at any point to exit from a procedure, batch, or statement block.
Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then click Delete.
The STOP technique is a procedure to follow for stopping work because of imminent danger within a workplace. Employees are required to stop working when they notice an unsafe condition, behaviour, or hazard that could cause serious injury.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With