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
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

Group: General Forum Members
Points: 367 Visits: 303
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
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5815 Visits: 9098
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
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

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

Group: General Forum Members
Points: 919430 Visits: 48849
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 (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6384 Visits: 3437
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
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26694 Visits: 17605
Well.... one easy issue to fix is that the IF clause is commented out.
anujkumar.mca
anujkumar.mca
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

Group: General Forum Members
Points: 367 Visits: 303
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 (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)

Group: General Forum Members
Points: 77386 Visits: 15517
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 (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)

Group: General Forum Members
Points: 919430 Visits: 48849
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
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

Group: General Forum Members
Points: 367 Visits: 303
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