Does the @error line even make sense here? Would it even be hit in an error scenario? What happens if there are multiple sql statements inside a try block each followed by a similar check?
begin try
Select a,b from tableC where a > 3 and b < 2
Set @error = @@error
If @error <> 0 begin raiseerror("error",16,1)end
end try
begin catch
select error_message()
end catch
No, it never gets hit. This is easy to demonstrate here:
I suspect someone updated some old code without really understanding what they were doing.
The
@errorwill be set to 0 after each successful statement, but never get a value assigned in case of an error since it will be bypassed by theTRY...CATCH.