troubleshooting Question

Why is DDL command not executing stored procedure

Avatar of Mel Brooks
Mel Brooks asked on
DatabasesMicrosoft SQL Server* triggers
2 Comments1 Solution89 ViewsLast Modified:
I'm trying to write a DDL trigger that will send an email if someone tries to a drop a database.  I have dropped my sample code below.  The trigger works in that the Rollback prevents the database from being dropped, but the stored procedure does not appear to run at all and so a message isn't sent.  The same store procedure does work on a similar DDL script for DROP TABLE.

USE [master]
GO

/****** Object:  DdlTrigger [ddl_trig_database]    Script Date: 10/29/2017 11:13:57 PM ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

alter TRIGGER [ddl_trig_database]  
ON ALL SERVER  
FOR Drop_DATABASE  
AS  
   DECLARE @eventData XML,
        @otext VARCHAR(MAX),
        @edate DATETIME,
            @Msg varchar(max)

set @edate=GETDATE()
SET @eventData = EVENTDATA()      
select  @otext=@eventData.value('data(/EVENT_INSTANCE/ObjectName)[1]', 'SYSNAME')      

set @msg = ' Delete attempt on database ' + convert(varchar(26),@edate,100) + '  ' + @otext

exec demo.dbo.SendInternetEmail 'RECIPIENT@gmail.com', 'Dropped database', @msg, 'SENDER@gmail.com'
rollback

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

ENABLE TRIGGER [ddl_trig_database] ON ALL SERVER
GO
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros