@@ERROR reset to 0 when you check it. What for? Because it reflects the status of the last executed statement.
IF @@ERROR <> 0 ...
is an operator, and this statement completes successfully, with the result that @@ERROR set to 0.
The correct way to check and use @@ERROR values ββis as follows:
DECLARE @ErrorCode INT INSERT INTO Table ... SET @ErrorCode = @@ERROR IF @ErrorCode <> 0 BEGIN INSERT INTO ErrorLog (ErrorCode, Message) VALUES (@ErrorCode, 'The INSERT operation failed.') END
source share