Created
February 25, 2020 19:51
-
-
Save marcusholmgren/0b89eec65eff3fd4ddff24d6cf1068ca to your computer and use it in GitHub Desktop.
Trigger firing when users log on to the server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create a trigger firing when users log on to the server | |
CREATE TRIGGER LogonAudit | |
-- Use ALL SERVER to create a server-level trigger | |
ON ALL SERVER WITH EXECUTE AS 'sa' | |
-- The trigger should fire after a logon | |
AFTER LOGON | |
AS | |
-- Save user details in the audit table | |
INSERT INTO ServerLogonLog (LoginName, LoginDate, SessionID, SourceIPAddress) | |
SELECT ORIGINAL_LOGIN(), GETDATE(), @@SPID, client_net_address | |
FROM SYS.DM_EXEC_CONNECTIONS WHERE session_id = @@SPID; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment