I’ve a SP that contains multiple update and insert queries. If any of the query gives the error, does execution move to next statement? For example,
CREATE PROCEDURE Test()
AS
BEGIN
INSERT INTO SomeTable VALUES (1, 2, 3)
UPDATE SomeTable SET X = 4 WHERE Y = 5
END
In above example, if INSERT statement throws the error, would execution move to next UPDATE statement?
It depends on the error severity. From severity 19 and up, the batch gets aborted:
However note that if the code above runs within a
BEGIN TRY/BEGIN CATCHblock (or is called from such a block) then errors with severity 10-18 cause the control flow to jump to the catch block (which is not the same as saying that the execution got aborted) and thus the next statement will not be executed.In addition the
XACT_ABORTsetting also causes the current transaction to rollback and the current batch to abort.If you want a template for how to write solid stored procedures code, see Exception handling and nested transactions