Hamed Zakery Miab
Hamed Zakery Miab

Reputation: 758

How does Error_Message() work?

How does Error_Message() function works and where is the raised error data stored?

I have to write some code to read all errors currently raised but not handled yet (CATCH block is not reached end, in our DB it takes some minutes for a CATCH block to finish). Now the question is:

Where can I get data related to raised errors?

Improve (UPDATE): suppose that I have a code like this:

BEGIN TRY
  DECLARE @a INT = 1 / 0
END TRY
BEGIN CATCH
  PRINT ERROR_MESSAGE() 
  -- What can I write instead of ERROR_MESSAGE() to get the same result?
END CATCH

Upvotes: 0

Views: 1025

Answers (1)

Dhaval
Dhaval

Reputation: 2379

it is depend on Error Severity Level that you get in Error

Msg 547, Level 16, State 0, Line 11

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_ Categories".

  1. As a general rule, errors with a severity level of 16 or higher are logged automatically to the SQL Server log and the Windows Application log.
  2. Errors with a severity level from 19 through 25 can be specified only by members of the sysadmin fixed server role.
  3. Errors with a severity level from 20 through 25 are considered fatal and cause the connection to be terminated and any open transactions to be rolled back.
  4. Errors with severity level 0 through 10 are informational only.

asp er my First point you can get Error at SQL Sever Log and Window Application Log

for SQL Server log

Object Explorer in the Management ➪ SQL Server Logs node lists the logs. Double-clicking a log opens SQL Server’s cool Log File Viewer

enter image description here

For Window Application Log

Control Panel ➪ System and Security ➪ Administrative Tools ➪ Event Viewer.

enter image description here

Upvotes: 1

Related Questions