I. Syntax points:
Begin try
{SQL _statement extends statement_block}
End try
Begin catch
[{SQL _statement extends statement_block}]
End catch
[;]
Exception section:
Within the scope of the catch block, you can use the following system functions to obtain the error message that causes the Catch Block to be executed:
Error_number () returns the error number.
Error_severity () returns the severity.
Error_state () returns the error Status Number.
Error_procedure () returns the name of the stored procedure or trigger with an error.
Error_line () returns the row number in the routine that causes the error.
Error_message () returns the complete text of the error message. This text can include any value provided by replaceable parameters, such as length, object name, or time.
II. Instance:
Step 1:
Create procedure syslogerror
As
Begin
Insert into systemerrorlog
(Spname, description, logtime)
Values
(Error_procedure (), convert (nvarchar (max), error_line () + ':' + error_message (), getdate ())
End
Step 2:
Begin try
Begin transaction;
....
Commit transaction;
End try
Begin catch
If @ trancount> 0
Begin
Rollback transaction;
End
Execute [DBO]. [syslogerror];
End catch;
# Database Technology