Programmatically Enable Auditing on Logins in SQL Server

The xp_instance_regwrite extended stored procedure helps you alter registry entries. To enable both success and failure login auditing, use the following command:

USE master
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', _
N'Software\Microsoft\MSSQLServer\MSSQLServer',_
N'AuditLevel', REG_DWORD, 3

To disable login auditing

USE master
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', _
N'Software\Microsoft\MSSQLServer\MSSQLServer',_
N'AuditLevel', REG_DWORD, 0

Login auditing is important as it helps you identify the user who performed unwanted actions, when they occur.

Note: When I ran the query, I got the message “0 rows affected”. However when you open the registry, the entry is what is expected to be, after running the query.


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

No comments: