Error Functions in Sql Server

These are the functions you can use inside a CATCH block.  These functions all return NULL if they are called from outside a CATCH block.
ERROR_NUMBER:- The number of the error that occurred.  This is similar to @@ERROR except that it will return the same number for the duration of the CATCH block.
ERROR_MESSAGE:- The complete text of the error message is including any substitute parameters such as object names.

ERROR_LINE:- This is the line number of the batch or stored procedure where the error occurred.
ERROR_SEVERITY:- This is the severity of the error.  The CATCH block only fires for errors with severity 11 or higher.  Error severities from 11 to 16 are typically user or code errors. Severity levels from 17 to 25 are usually software or hardware errors where processing may not be able to continue.
ERROR_STATE:- This is sometimes used by the system to return more information about the error.
ERROR_PROCEDURE:-  If the error was generated inside a stored procedure this will hold the name of the procedure.

Syntax
BEGIN TRY
    SELECT [First] = 1/0
END TRY
BEGIN CATCH
    SELECT [Error_Line] = ERROR_LINE(),
           [Error_Number] = ERROR_NUMBER(),
           [Error_Severity] = ERROR_SEVERITY(),
           [Error_State] = ERROR_STATE()

    SELECT [Error_Message] = ERROR_MESSAGE()
END CATCH


Out-Put:-
Error Functions in Sql Server

Error Functions in Sql Server Error Functions in Sql Server Reviewed by NEERAJ SRIVASTAVA on 2:29:00 PM Rating: 5

No comments:

Powered by Blogger.