The error "Logon failed due to trigger execution" occurs when a logon trigger in SQL Server prevents a user from logging in. Logon triggers are special stored procedures that execute in response to a LOGON
event, and they can be used to enforce security policies, restrict access, or log login activity.
Steps to Resolve the Issue
Identify the Logon Trigger:
- Determine which logon trigger is causing the issue by querying the system catalog.
SELECT name, create_date, modify_date, is_disabled
FROM sys.server_triggers
WHERE type = 'TR' AND parent_class_desc = 'SERVER';
- Check the code of the logon trigger to understand its logic and why it might be causing the logon to fail.
-- Check the trigger defination
EXEC sp_helptext 'TriggerName';
- If the trigger is incorrectly preventing logons, you can disable it.
-- Disable the trigger
DISABLE TRIGGER TriggerName ON ALL SERVER;
- If the trigger logic needs adjustment, modify the trigger code accordingly.
ALTER TRIGGER TriggerName ON ALL SERVER
FOR LOGON
AS
BEGIN
-- Your trigger logic here
END;
- After fixing the issue, re-enable the trigger.
ENABLE TRIGGER TriggerName ON ALL SERVER;
Example Scenario and Solutions
Scenario: A Logon Trigger Prevents All Logins
Problematic Trigger:
CREATE TRIGGER PreventLogons
ON ALL SERVER
FOR LOGON
AS
BEGIN
ROLLBACK;
END;
Solution:
Disable the Trigger:
DISABLE TRIGGER PreventLogons ON ALL SERVER;
ALTER TRIGGER PreventLogons ON ALL SERVER
FOR LOGON
AS
BEGIN
IF DATEPART(HOUR, GETDATE()) NOT BETWEEN 9 AND 17
BEGIN
ROLLBACK;
END;
END;
ENABLE TRIGGER PreventLogons ON ALL SERVER;
Identify Active Logon Triggers:
SELECT name, create_date, modify_date, is_disabled
FROM sys.server_triggers
WHERE type = 'TR' AND parent_class_desc = 'SERVER';
-- Replace 'TriggerName' with the name of the trigger
EXEC sp_helptext 'TriggerName';
-- Replace 'TriggerName' with the name of the trigger
DISABLE TRIGGER TriggerName ON ALL SERVER;
ALTER TRIGGER TriggerName ON ALL SERVER
FOR LOGON
AS
BEGIN
-- Adjust the logic to prevent inappropriate logon failures
END;
ENABLE TRIGGER TriggerName ON ALL SERVER;