What is the difference between using TRY ... CATCH and @@ ERROR in SQL Server?

When writing SQL transaction transactions, I often use @@ ERROR to find out if any problem occurred during the transaction or not. But I really don't know what are the advantages and disadvantages of handling exceptions with TRY ... CATCH, or which one is better. Please clarify the difference between those and the scenarios in which they are useful.

Thanks in advance, Vinu.

+4
source share
2 answers

@@ERROR , , @@ERROR reset NULL, , .., , .

TRY...CATCH, TRY, CATCH, , . , CATCH.

:

ERROR_LINE()
EROR_MESSAGE()
ERROR_Severity()
ERROR_PROCEDURE()
ERROR_STATE()

, TRY...CATCH , .

+2

, , , - , :

  • @@ERROR error number for the last Transact-SQL statement executed., , .
  • TRY-CATCH . , , CATCH .

TRY-CATCH, , ERROR_MESSAGE() ERROR_NUMBER() .

, .

+1

All Articles