HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlModerate

Write to Error Log

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
errorwritelog

Problem

Using SQL Server 2008 R2:

How can I write to the SQL Server Error Log? I have a ROLLBACK statement that I'd like to couple with a statement written to the error log for external monitoring.

Example:

BEGIN TRAN

INSERT INTO table1
SELECT * 
FROM table2

IF @@ERROR <> 0
BEGIN
    ROLLBACK TRAN
    --Write to log
    RETURN
END

COMMIT TRAN


EDIT:

I'd like to clarify - I want to write to the "SQL Server Logs", "Current" log, under the "Management" folder in the object explorer.

Solution

AFAIK, the only way to do this is to use the WITH LOG option of the RAISERROR function. Note from that MSDN page that there are certain security requirements that must be met for you to do this.

That said, the SQL Server error log really isn't meant for application-based logging. If you need to add monitoring/logging to your application, this should probably be implemented as a table in your database (for example).

Context

StackExchange Database Administrators Q#20455, answer score: 10

Revisions (0)

No revisions yet.