How to Prevent DROP Table using SQL Server DDL Trigger
T-SQL developers and SQL Server administrators can prevent unauthorized users to drop sql tables using sql DROP TABLE command on their databases with the use of SQL Server DDL trigger.
Using DDL Trigger is respectively a new method of preventing database tables compared to using Create View With SchemaBinding in SQL Server.
DDL Trigger are new with Microsoft SQL Server 2005.
Since then the use of DDL trigger in SQL Server 2005 or ddl trigger in SQL Server 2008 is increasing parallel with the info distributed among SQL Server professionals.
Here is the basic drop table DDL trigger in SQL Server.
This will help SQL professionals to secure their database tables against sql injections, etc.
And t-sql developers can find a more ehnanced version of the above DDL trigger which also returns meta data about the event causing the trigger to fire.
SQL security administrators can use the information from EVENTDATA for audit and security purposes.
Let's create with a sql example.
First create a sql database table.
Second create the drop table DDL trigger.
Third, execute the following sql command to test how t-sql ddl trigger behave.
After sql DROP Table command is executed, the message displayed from the DDL trigger execution is as follows :
You can not drop table in this database
Msg 3609, Level 16, State 2, Line 1
The transaction ended in the trigger. The batch has been aborted.
If you want to take a closer look at the EVENTDATA(), here is the output of the above DDL trigger event data.