| Today I came across the situation where I need to following scenario. I need to make entry in Event Viewer when there is an error in Stored Procedure. I wondered to do this, but finally I come up with the solution. SQL is much powerful. SQL provides us to make an entry in Event Viewer by two ways: 1. using XP_LogEvent 2. By Raiserror WITH LOG Lets first see the way using XP_LogEvent:Here I have created one SP which will raise an error “Divide by zero error encountered.” as I tried to do “10 / 0”. CREATE PROCEDURE TestEventViewer EXEC xp_logevent 60000, @msg, informational END CATCH Lets Execute this SP: EXEC TestEventViewer This will write entry in Event Viewer. Now open Event Viewer. You can find Event Viewer at Control Panel –> Administrative Tools –> Event Viewer. You will get en entry of Error there. So we can do this by Extended Stored Procedure: “xp_logevent”. Let see the parameters of this Procedure. First Parameter: “60000” is the Error Number Second Parameter: “@msg” is the message to be displayed in Event Viewer. Third Parameter: “informational” is the Error Level. It could be “informational”, “Error”, “Warning”. Now, Lets see by another way By Raiserror WITH LOG: It is the same way as we used Raiserror to Raise an Error. CREATE PROCEDURE TestEventViewer RAISERROR(@msg, 11, 1) WITH LOG END CATCH Lets Execute this SP: EXEC TestEventViewer. So By these ways we can make an entry to Event Viewer. Let me know if it helps you in any way |
Learn SQL and database management at SQLYoga for articles, tutorials, and tips to improve your skills and streamline data operations. Join our community!
Showing posts with label Event Viewer. Show all posts
Showing posts with label Event Viewer. Show all posts
April 2, 2009
SQL SEVER: How to make an Entry in Event Viewer using SQL SERVER
Labels:
DBA,
Event Viewer,
Log,
SQL Server 2005,
Tejas Shah
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
Subscribe to:
Posts (Atom)