Trigger to avoid drop online database

  • 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

  • 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

  • I can understand, But I have to implement trigger as its kind of environment requirement.

  • 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
  • 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

  • Well.... one easy issue to fix is that the IF clause is commented out.

  • 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.

  • 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

  • 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
  • Thank you so much guys, @gail- you are right, now clear my confusion.

  • Guys,
    After Restrict the Drop databse through DDL trigger, 
    I need to restrict all db users to take OFFLINE and DETACH the database from the SQL server. 
    1.  I created the ddl trigger for ALTER_DATABASE. But when I take the database offline, the trigger is fired and error also is shown but database goes OFFLINE also. Please help me to achieve this.
    2.  If I detach a database- DDL trigger for Alter_Database is not fired.(Please suggest a way so that I can trigger a mail if anyone detaches the database).

  • I don't know about detach at this point, but with some "fancy footwork" you can DROP a database only if it was OFFLINE.  You have to rollback the DROP, of course, in order to check it, then re-do the DROP if the db was OFFLINE.  You may need to remove the PRINT statement, depending on your SQL version / settings.

    Btw, this leaves the physical files on disk.  Personally I'd add something to the code to schedule a clean-up of the old data files.  But maybe that's just me.


    USE master;
    SET ANSI_NULLS ON;
    SET QUOTED_IDENTIFIER ON;
    GO
    CREATE TRIGGER master__trg_ddl_drop_database
    ON ALL SERVER
    FOR DROP_DATABASE
    AS
    SET NOCOUNT ON;

    DECLARE @context_info varbinary(128)

    IF RIGHT(CONTEXT_INFO(), 2) = 0xDDDD
    BEGIN
      SET @context_info = SUBSTRING(CONTEXT_INFO(), 1, 126)
      SET CONTEXT_INFO @context_info
      RETURN;
    END /*IF*/

    DECLARE @DatabaseName nvarchar(128)
    DECLARE @DBState nvarchar(100)
    DECLARE @SQL nvarchar(4000)

    SELECT @DatabaseName = EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]','nvarchar(128)' )

    ROLLBACK;

    SELECT @DBState= state_desc
    FROM master.sys.databases
    WHERE name = @DatabaseName

    IF @DBState IN ('OFFLINE')
    BEGIN
      SET @context_info = SUBSTRING(CONTEXT_INFO(), 1, 126) + 0xDDDD
      SET CONTEXT_INFO @context_info
      SET @SQL = 'DROP DATABASE ' + @DatabaseName + ';'
      EXEC(@SQL)
      PRINT 'Ignore the error message, the db *was* actually dropped, since it was OFFLINE.'
    END /*IF*/
    GO

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply