I am asking this question for knowledge only. I have SQL Server 2008 R2 and I have created a stored procedure in which I have five insert statements that execute one by one for different tables.
I don’t have placed any locks or transaction code in that stored procedure. Now what if the 3rd insert statement throws an error? Will the remaining two statements be executed or not?
Thanks
Based on the type of error, SQL Server will abort the statement or the batch. If it aborts the statement, the other inserts will still run. If it aborts the batch, the procedure is aborted and the remaining inserts are not run.
Full details in an excellent article by Sommarskog from a comment by Martin Smith.
Here’s an example setup. It contains a stored procedure
TestProcthat does six inserts. The third insert causes a foreign key violation, and the fifth insert causes a conversion error. Only the second error causes the stored procedure to stop:Output: