Is there any way to have a stored procedure automatically throw
if any statement fails due to an error?
I'm inside a stored proc with a merge
statement which can fail due to a primary key violation, however execution still continues.
Do I have to resort to if @@error != 0 throw ...
everywhere?
EDIT: I'm using MS SQL Server 2012
EDIT: This seems to work, but is there a less verbose solution? It seems as if the introduction of try/catch
makes flow jump to the catch
block when an error is encountered. From there I just rethrow the exception.
begin try
....do lots of sql code
end try
begin catch
throw;
end catch