Blog Archives

Logon Triggers aka How To Lock That Annoying User Out During Lunch

Earlier this week, I chose a few SQL Server 2008 topics for our #SQLHelp MCITP study group to discuss here on the blog.  Thus, today I will discuss logon triggers, one of the new features available in SQL Server 2008 (well actually if I remember correctly they were snuck in 2005 SP2…shhhh).  Logon triggers are a special form of trigger that the database administrator can use in order to monitor logon events. 

Logon triggers can be used for the following purposes:

  1. To audit and control server sessions (such as tracking logon activity)
  2. Limiting the number of sessions allowed for a particular logon
  3. Restricting logons to SQL Server (similar to AD snap-in that lets you set the hours that a user may logon)

Here is an example from books online showing how to set up a logon trigger that limits the number of concurrent sessions for a particular logon, logon_test in this case.  This example, in particular, will limit the concurrent sessions to three.  You must create this logon in order to test this code, or change the logon name to match your requirements.

CREATE TRIGGER connection_limit_trigger
ON ALL SERVER WITH EXECUTE AS ‘logon_test’
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN()= ‘logon_test’ AND
    (SELECT COUNT(*) FROM sys.dm_exec_sessions
            WHERE is_user_process = 1 AND
                original_login_name = ‘logon_test’) > 3
    ROLLBACK;
END;

Here is some sample code for restricting the user based on logon time (between 12Pm and 1pm in this example for all users except the SA\SQLAdministrator):

CREATE TRIGGER Restrict_Logon_Hours
ON ALL SERVER
FOR LOGON
AS
BEGIN
    IF (DATEPART(HOUR, GETDATE()) BETWEEN 12 AND 13) AND
          (SUSER_SNAME() != ‘AD\SQLAdministrator’)
    BEGIN
        ROLLBACK;
    END;
END;

Once you have created logon triggers, you can view them by a simple query to the sys.server_triggers metadata.

Saturday SQL Schoolhouse

In our third installment of the Saturday SQL Schoolhouse, I wanted to share with you a neat little beginner’s resource for learning ANSI SQL.

The site is called SQLCourse.com, Interactive Online SQL Training.

There is also a “sequel” to this course which is a little more advanced called SQLCourse2.com.  Give them a try, and enjoy!

Sunday Funday

Sunday is a great day for SQL funday, so every Sunday I will post some fun, SQL related links for your enjoyment….

Everyone has seen some form of a mathematical Mandelbrot set, but have you seen it on your SQL Server in T-SQL?  Check this out!

Enjoy this #SQLAwesomeness!

Migrating SQL Server Database Logins with sp_help_revlogin

You’ve migrated your database to a new box, with no problems.  You created the login that goes with this database, and all is right in the world.  However, when your user goes to connect to it they get the dreaded error 18456, login failed.  But wait, I created the login, right?

Not so fast there, accidental DBA.  You are going to need the help of a nifty stored procedure called sp_help_revlogin.  This stored procedure, provided by Microsoft in the link below, will generate a T-SQL script that will copy the logins, their passwords and their corresponding SIDs (or security identification number, which is usually the culprit here).  This is also a great utility when the database has numerous users with various security levels and passwords.  Really now, who wants to write all of that down and recreate it?  Even with screenshots, it can turn into a large, fat-fingering and time consuming event.  Mind-numbing, I would say.  Why do something manually when you can do it automagically?   Just think of all those happy logins that will not be orphaned by the dark side?

How to transfer the logins and the passwords between instances of SQL Server 2005 and SQL Server 2008.

Here is also a code snippet to check for orphaned logins, for your enjoyment.  Enjoy!

USE <database_name>;
GO
sp_change_users_login @Action='Report';
GO