Logging sysadmin logins in SQL Server

Our yearly cabin opening “men’s weekend” was last weekend. The fishing was a bit below average, but we still had a great time. I brought my six year old son up, and two of my six year old nephews were brought up by their dad’s as well. The first day of fishing we went over to the rainbow flowage, which was a bit of a bust. We ended up hooking five or six northern pike, but only landed one because they kept biting off the line. The boys in the other boat caught some bluegill and bass, but overall the fishing wasn’t as hot as in years past. This was probably due to all the rain they got up there. There were places where the rivers were over the road. Below is the one pike we managed to land.
Rainbow Flowage NP.jpg
Security these days is as important as ever. SQL Server provides lots of ways to improve security of your data and environment. One small way I like to keep an eye on my system is to log sysadmin logins to the server. Sysadmin is able to do anything in SQL Server, and by reviewing my log from time to time I can be sure that no one is using this type of login to compromise the system.
The first thing to do is to create a table to hold the log data:


CREATE TABLE [dbo].[sysadminLogging](
   [Login] [VARCHAR](255) NOT NULL,
   [LoginDate] [DATETIME2](7) NOT NULL,
   [SAL_id] ASC


All that is left is to create a server level login trigger to record any sysadmin logins to that table:

CREATE TRIGGER [servertrigger_CheckForSysAdminLogin] ON ALL SERVER
        IF IS_SRVROLEMEMBER('sysadmin') = 1
                INSERT  INTO DBA_DB.dbo.sysadminLogging
                        ( [Login] , LoginDate )
                VALUES  ( ORIGINAL_LOGIN() , GETDATE() );

Check this table regularly to get a good idea of who is logging into your SQL Server with these higher privileges.


james youkhanis points out a problem that can occur in the comments.  This problem could make logging in quite difficult.  I have posted a follow-up where I explain the problem, provide a workaround to allow logging in (as james demonstrates below), and provide an updated trigger to minimize the risk of this occurring.


3 thoughts on “Logging sysadmin logins in SQL Server

  1. Please be very careful implementing this script. The issue I ran into was
    “Login Failed due to trigger execution
    Change database context to master
    Change Language settings to us_english Error: 17892

    It will not be possible to logon to the database using SSMS. The only way to fix it is using DAC or SQLCMD.
    Solution is to use SQLCMD

    C:\Users\youkhanis>sqlcmd -S LocalHost -d master -A
    1> DROP TRIGGER servertrigger_CheckForSysAdminLogin ON ALL SERVER
    2> GO


    • Hi James
      This error could occur if the trigger was not able to successfully execute. One way this would occur is if you are trying to log to a table that no longer exists. Can you explain what caused this in your case?
      Thank you


  2. Pingback: Addressing login trigger failures in SQL Server | The Fisherman DBA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s