Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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!

like image 790
Yagiz Ozturk Avatar asked Jan 02 '11 21:01

Yagiz Ozturk


People also ask

How do you exit a stored procedure in SQL Server?

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.

How do I close a stored procedure?

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.

What is stop procedure?

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.


1 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.

like image 69
marc_s Avatar answered Sep 23 '22 01:09

marc_s