Why is DDL command not executing stored procedure

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
Mel BrooksCITOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Are you getting any error ? or your sp <demo.dbo.SendInternetEmail> is not getting called ?

Pls see if this works -

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   
BEGIN

   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
rollback
exec demo.dbo.SendInternetEmail 'RECIPIENT@gmail.com', 'Dropped database', @msg, 'SENDER@gmail.com'

END

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

ENABLE TRIGGER [ddl_trig_database] ON ALL SERVER
GO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mel BrooksCITOAuthor Commented:
I had been trying to delete the test database from the management studio window and didn't get any errors.  I ran a Drop Database command from the query window and received an error about ANSI settings.  I removed the lines:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
I then started receiving blank email messages and was able to correct that by changing the @otext line to:

select   @otext=@eventData.value('data(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'VARCHAR(MAX)')
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.