Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

417


Chapter 16: Programming with T-SQL


16


Logging the Error
Another advantage to using the RAISERROR command is that it can log the error to the
Windows Application event log and the SQL Server event log.

To specify that an event should be logged from the RAISERROR command, add the
WITH LOG option:

RAISERROR ('Unable to update %s.', 14, 1, 'Customer')
WITH LOG

Result:

Server: Msg 50000, Level 14, State 1, Line 1
Unable to update Customer.

To view errors in the Application event log (see Figure 16-1), select Control Panel ➪ System
and Security ➪ Administrative Tools ➪ Event Viewer. An Event Viewer is also located in
Control Panel ➪ Administrative Tools.

FIGURE 16-1
A SQL Server RAISERROR error in the Windows Application event log. Notice that the server
and database name are embedded in the error data.

c16.indd 417c16.indd 417 7/30/2012 5:38:10 PM7/30/2012 5:38:10 PM


http://www.it-ebooks.info
Free download pdf