Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Logon trigger

Author  Topic 

chris_cs
Posting Yak Master

223 Posts

Posted - 2012-02-28 : 09:55:14
I'm looking to create a logon trigger to record anyone who uses a SQL login to log onto the server.

Can I just confirm that something like the following will work:

CREATE TRIGGER [AuditLogins]
ON ALL SERVER WITH EXECUTE AS ''
FOR LOGON
AS
BEGIN

SET NOCOUNT ON
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON

DECLARE @xml XML
DECLARE @loginType VARCHAR(255)
DECLARE @loginName VARCHAR(255)

SET @xml = EVENTDATA()
SET @loginType = CONVERT(VARCHAR(255), @xml.query('data(/EVENT_INSTANCE/LoginType)'))
SET @loginName = CONVERT(VARCHAR(255), @xml.query('data(/EVENT_INSTANCE/LoginName)'))

IF @loginType = 2 -- This should be a SQL login
INSERT INTO DBAdmin.dbo.LogonAudit (LoginType, LoginName, Logindate)
SELECT @loginType, @loginName, GETDATE()

END



My main concern is determining if using a LoginType value of 2 is correct?

----------------------------
Junior DBA learning the ropes

chris_cs
Posting Yak Master

223 Posts

Posted - 2012-02-28 : 12:10:14
Looks like this should have been LoginType = 'SQL Login' as apposed to LoginType = 2.

----------------------------
Junior DBA learning the ropes
Go to Top of Page
   

- Advertisement -