This is the DDL trigger for security purposes as determined by company polices. Basically a logon trigger fires Stored Procedures in response to a LOGON event. When a user session is established with an instance of SQL Server the event is raised, that was introduced in SQL Server 2005 SP2.

It can be used for audit and control server sessions. You can also track the login activity, restricting logins to SQL Server, or limiting or denying the number of login sessions for a specific user. You can define more than one Logon trigger on the server.

It works when the user has access to the instance, then the trigger fires.

For example:

  1. USE DBATest
  2. GO
  3. -- Creating audit table for login details
  4. CREATE TABLE Auditing
  5. (
  6. SessionId INT,
  7. LogonTime DATETIME,
  8. HostName VARCHAR(50),
  9. ProgramName VARCHAR(500),
  10. LoginName VARCHAR(50),
  11. ClientHost VARCHAR(50))
  12. GO
  13. USE [MASTER]
  14. GO
  15. -- Creating Audit trigger for logon
  16. CREATE TRIGGER Audit_TR
  17. ON ALL SERVER WITH EXECUTE AS 'sa'
  18. FOR LOGON
  19. AS
  20. BEGIN
  21. DECLARE @LogonTRData XML,
  22. @EventTime datetime,
  23. @LoginName varchar(50),
  24. @ClientHost varchar(50),
  25. @LoginType varchar(50),
  26. @HostName varchar(50),
  27. @AppName varchar(500)
  28. SET @LogonTRData = EVENTDATA()
  29. SET @EventTime = @LogonTRData.value('(/EVENT_INSTANCE/PostTime)[1]', 'datetime')
  30. SET @LoginName = @LogonTRData.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(50)')
  31. SET @ClientHost = @LogonTRData.value('(/EVENT_INSTANCE/ClientHost)[1]', 'varchar(50)')
  32. SET @HostName = HOST_NAME()
  33. SET @AppName = APP_NAME()--,program_name()
  34. INSERT INTO DBATest.dbo.Auditing
  35. (
  36. SessionId,
  37. LogonTime,
  38. HostName,
  39. ProgramName,
  40. LoginName,
  41. ClientHost
  42. )
  43. SELECT
  44. @@SPID,
  45. @EventTime,
  46. @HostName,
  47. @AppName,
  48. @LoginName,
  49. @ClientHost
  50. END
  51. GO

The EVENTDATA() is an XML document that is only available within the context of the DDL Trigger. It has the following schema:

  1. <EVENT_INSTANCE>
  2. <EventType>event_type</EventType>
  3. <PostTime>post_time</PostTime>
  4. <SPID>spid</SPID>
  5. <ServerName>server_name</ServerName>
  6. <LoginName>login_name</LoginName>
  7. <LoginType>login_type</LoginType>
  8. <SID>sid</SID>
  9. <ClientHost>client_host</ClientHost>
  10. <IsPooled>is_pooled</IsPooled>
  11. </EVENT_INSTANCE>

In the following screen you can see the audit table details:

table

You can restrict a user from opening more than 5 connections with the server at the same time.

For example:

  1. CREATE TRIGGER Connection_Limit_tr
  2. ON ALL SERVER WITH EXECUTE AS 'sa'
  3. FOR LOGON
  4. AS
  5. BEGIN
  6. IF ORIGINAL_LOGIN() <> 'sa'
  7. AND
  8. ( SELECT COUNT(*)
  9. FROM sys.dm_exec_sessions
  10. WHERE Is_User_Process = 1 AND
  11. Original_Login_Name = ORIGINAL_LOGIN()
  12. ) > 1
  13. ROLLBACK
  14. END

"When" limits the connections for all Logins except 'sa'. Once the connection limit is reached the user will not be able to create a new connection. Then an error message will be issued like this:

error message

You can place restrictions on a user outside of office hours.

For example:

  1. CREATE TRIGGER Connnection_TimeLimit_TR
  2. ON ALL SERVER WITH EXECUTE AS 'sa'
  3. FOR LOGON
  4. AS
  5. BEGIN
  6. IF ( (ORIGINAL_LOGIN() <> 'sa')
  7. AND
  8. (DATEPART(HOUR, GETDATE()) BETWEEN 16 AND 17)
  9. )
  10. ROLLBACK
  11. END

I have defined here the out-of-office hours are 8 AM to 6 PM.

SQl

If you want to connect during out of office hours, then the following error will be shown.

You can DROP a LOGON Trigger using the following script:

  1. DROP TRIGGER triggerName ON ALL SERVER