Figure 1 Error Handling Code
กก
SET XACT_ABORT ON

BEGIN TRY

  BEGIN TRAN
    -- your code goes here
  COMMIT TRAN

END TRY
BEGIN CATCH TRAN_ABORT

  DECLARE @err AS INT
  SET @err = @@error
  
  IF @err = <error id 1>
  BEGIN
    -- Handle error 1
  END
  ELSE IF @err = <error id 2>
  BEGIN
    -- Handle error 2
  END
  -- ... other errors ...
  ELSE
  BEGIN
    -- Handle unexpected error
  END

  IF @@trancount > 0 ROLLBACK

END CATCH