Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
While DML and DDL triggers work based on changes to the data or objects in a database, logon triggers detect when users log on to the server and execute stored procedures in response to this. Typical uses for logon triggers include keeping track of user activity, restricting access to SQL Server and maintaining a limit to the number of concurrent users.
The basic syntax for a logon trigger is shown in the example below:
CREATE TRIGGER myLogonTrigger
ON ALL SERVER
FOR LOGON
AS
BEGIN
--Your trigger code here
END
You can find logon triggers in the same place as server-scoped DDL triggers, as shown in the diagram below:
Expand the Server Objects folder followed by the Triggers folder to find your Logon triggers.
You can drop, alter, disable and enable logon triggers using the same methods as for other types of trigger. You can also change the order in which logon triggers fire using the sp_settriggerorder system stored procedure.
The answer to this question seems obvious: when a user logs on! It's worth considering exactly what this means in terms of what you can do within a logon trigger. According to Microsoft's TechNet site a logon trigger fires when the authentication stage of logging on has finished but before the user session is actually established. This means that if your trigger is designed to show messages to the user (using the PRINT statement for example) the user won't ever see them.
To make effective use of logon triggers it helps to be able to find information on the sessions that are currently established. The example code below shows how to do this:
--Return data on the current sessions
SELECT * FROM sys.dm_exec_sessions
An example of the output from this query is shown in the diagram below:
There are many more columns than the ones shown here
Two of the more useful columns from this output are login_name and is_user_process.
The example shown below prevents a certain user from establishing any more connections to the server when they already have three connections open.
CREATE TRIGGER trgNoMoreConnections
ON ALL SERVER
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN() = 'WiseOwl' AND
(SELECT COUNT(*) FROM sys.dm_exec_sessions
WHERE is_user_process = 1 AND
original_login_name = 'WiseOwl') > 3
ROLLBACK
END
The ORIGINAL_LOGIN function shown in the code above returns the name of the user who is logging in. In the WHERE clause of the query we're testing if the is_user_process column contains a value of 1 (i.e. it is a user process), and if the original_login_name column is equal to the user whose sessions we're trying to limit.
The result of attempting to open a new session when the user already has three open is an error message similar to the one shown below:
This is part of the error message that will appear.
Take care when creating logon triggers as you can fairly easily find yourself unable to connect to a SQL Server instance! If this happens you'll need somebody who is part of the sysadmin role to connect and disable or delete the trigger.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.