Does the title of this post trigger your mind? If you all remember, a few days back I had written an article on my interesting observation regarding logon triggers. I would advise you to first read SQL SERVER – Interesting Observation of Logon Trigger On All Servers before continuing with this article further to have a complete idea of the subject.
The question I put forth in my previous article was – In single login why the trigger fires multiple times; it should be fired only once. I received numerous answers in thread as well as in my MVP private news group. Now, let us discuss the answer for the same.
The answer is – It happens because multiple SQL Server services are running as well as intellisense is turned on.
Let us verify the above answer.
First, run the following script to create database and logon Audit table.
/* Create Audit Database */
CREATEÂ DATABASE AuditDb
GO
USE AuditDb
GO
/* Create Audit Table */
CREATEÂ TABLE ServerLogonHistory
(SystemUser VARCHAR(512),
DBUser VARCHAR(512),
SPID INT,
LogonTime DATETIME)
GO
/* Create Logon Trigger */
CREATEÂ TRIGGER Tr_ServerLogon
ON ALL SERVER FOR LOGON
AS
BEGIN
INSERTÂ INTO AuditDb.dbo.ServerLogonHistory
SELECT SYSTEM_USER,USER,@@SPID,GETDATE()
END
GO
After that, disable all the other SQL Server Services as delineated in the image below using SQL Server Configuration Manager.
Next, try to logon to the system only one more time.
Check the audit table again to verify if there is a single entry for single login.
I would once again like to thank all of you for active participation and coming up with wonderful suggestions and answers. Let me have your opinion on this observation.
Reference : Pinal Dave (https://blog.sqlauthority.com)