SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger to avoid drop online database


Trigger to avoid drop online database

Author
Message
anujkumar.mca
anujkumar.mca
SSC Veteran
SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)

Group: General Forum Members
Points: 251 Visits: 223
Hey guys,

I need to create a trigger which can prevent to drop an online database. Trigger is working with normal condition but I am not able to apply database state condition (ONLINE). I am using below code but not able to fetch state_desc. Please help !
Alter TRIGGER StopDBDrop 
ON ALL SERVER
FOR DROP_DATABASE
AS
DECLARE @DatabaseName NVARCHAR(100),
@DBState NVARCHAR(100),

@SQL nvarchar(800)

--set the EVENTDATA() functions value to our xml so we can parse it

SELECT @DatabaseName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','nvarchar(128)' )
select @DBState= state_desc from master.sys.databases where name=@DatabaseName

--if(@DBState!='OFFLINE')
--BEGIN
RAISERROR('Only Offline database can be dropped. For more clarification please contact SQL Run team', 16,1);
ROLLBACK;
--END
GO

NorthernSoul
NorthernSoul
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4686 Visits: 7758
anujkumar.mca - Wednesday, January 3, 2018 7:10 AM
Hey guys,

I need to create a trigger which can prevent to drop an online database. Trigger is working with normal condition but I am not able to apply database state condition (ONLINE). I am using below code but not able to fetch state_desc. Please help !
Alter TRIGGER StopDBDrop 
ON ALL SERVER
FOR DROP_DATABASE
AS
DECLARE @DatabaseName NVARCHAR(100),
@DBState NVARCHAR(100),

@SQL nvarchar(800)

--set the EVENTDATA() functions value to our xml so we can parse it

SELECT @DatabaseName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','nvarchar(128)' )
select @DBState= state_desc from master.sys.databases where name=@DatabaseName

--if(@DBState!='OFFLINE')
--BEGIN
RAISERROR('Only Offline database can be dropped. For more clarification please contact SQL Run team', 16,1);
ROLLBACK;
--END
GO

No users should have permission to drop databases so I would recommend restricting the user permissions.

Thanks

anujkumar.mca
anujkumar.mca
SSC Veteran
SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)

Group: General Forum Members
Points: 251 Visits: 223
I can understand, But I have to implement trigger as its kind of environment requirement.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)

Group: General Forum Members
Points: 842630 Visits: 48504
What do you mean 'it can't fetch state_desc'?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


FridayNightGiant
FridayNightGiant
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5940 Visits: 3408
anujkumar.mca - Wednesday, January 3, 2018 7:10 AM
Hey guys,

I need to create a trigger which can prevent to drop an online database. Trigger is working with normal condition but I am not able to apply database state condition (ONLINE). I am using below code but not able to fetch state_desc. Please help !
Alter TRIGGER StopDBDrop 
ON ALL SERVER
FOR DROP_DATABASE
AS
DECLARE @DatabaseName NVARCHAR(100),
@DBState NVARCHAR(100),

@SQL nvarchar(800)

--set the EVENTDATA() functions value to our xml so we can parse it

SELECT @DatabaseName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','nvarchar(128)' )
select @DBState= state_desc from master.sys.databases where name=@DatabaseName

--if(@DBState!='OFFLINE')
--BEGIN
RAISERROR('Only Offline database can be dropped. For more clarification please contact SQL Run team', 16,1);
ROLLBACK;
--END
GO

DDL Triggers fire after the DDL event. Therefore you are trying to query sys.databases but there is no longer an entry in sys.databases for that database

ZZartin
ZZartin
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22285 Visits: 15937
Well.... one easy issue to fix is that the IF clause is commented out.
anujkumar.mca
anujkumar.mca
SSC Veteran
SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)

Group: General Forum Members
Points: 251 Visits: 223
Dear Folks,
I know if condition is commented, because I was trying to put a condition with db state ONLINE, but unfortunately I cant capture the same with the help of below statement
select @DBState= state_desc from master.sys.databases where name=@DatabaseName
. can someone help me to fix this problem,
I just want to create a trigger which can prevent drop database (ONLINE) statement accidentally.

Sue_H
Sue_H
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59437 Visits: 12859
anujkumar.mca - Wednesday, January 3, 2018 6:32 PM
Dear Folks,
I know if condition is commented, because I was trying to put a condition with db state ONLINE, but unfortunately I cant capture the same with the help of below statement
select @DBState= state_desc from master.sys.databases where name=@DatabaseName
. can someone help me to fix this problem,
I just want to create a trigger which can prevent drop database (ONLINE) statement accidentally.


I don't think you can do it based on querying sys.databases as FridayNightGiant said.
I think you can only reference what is available in eventdata which would be database name. Or you could just not allow any drops - the trigger would need to be disabled to drop any database.

Sue



GilaMonster
GilaMonster
SSC Guru
SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)SSC Guru (842K reputation)

Group: General Forum Members
Points: 842630 Visits: 48504
DDL triggers are AFTER triggers, meaning they fire within the transaction *after* the changes have been made. So at that point, there is no record in sys.databases for the DB being dropped.
You'll have to work with the data that's in EVENTDATA, without going to sys.databases, for your trigger.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


anujkumar.mca
anujkumar.mca
SSC Veteran
SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)

Group: General Forum Members
Points: 251 Visits: 223
Thank you so much guys, @gail- you are right, now clear my confusion.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search