I read about error handling in SQL Server in this article , and they suggest using SQL Server GOTO in certain situations to roll back a transaction. Example:
BEGIN TRAN UPDATE Authors SET Phone = '415 354-9866' WHERE au_id = '724-80-9391' SELECT @intErrorCode = @@ERROR IF (@intErrorCode <> 0) GOTO PROBLEM UPDATE Publishers SET city = 'Calcutta', country = 'India' WHERE pub_id = '9999' SELECT @intErrorCode = @@ERROR IF (@intErrorCode <> 0) GOTO PROBLEM COMMIT TRAN PROBLEM: IF (@intErrorCode <> 0) BEGIN PRINT 'Unexpected error occurred!' ROLLBACK TRAN END
This article was written almost 10 years ago, and I heard that itβs usually a bad idea to use GOTO. Is the ok method above error handling in SQL Server? If not, can anyone suggest a better alternative?
sql sql-server error-handling goto
Abe miessler
source share