Friday 28 October 2011

SQL Server - prevent some action on tables by using Trigger

Sometimes, were're tired after a long working day.... and by inadvertance, the risk of executing some disasters commands - like delete - becomes higher.
For prevention, we can rollback those operations in all our server object or only on some specifics objects by configuring a DDL trigger.
In the following examples, let's prevent all 'drop table' on the whole objects of our server :

CREATE TRIGGER tddl_droptableprevent
ON ALL SERVER
FOR DROP_TABLE
AS
    BEGIN
        ROLLBACK;
    END
if we try to drop one table, we will receive the message : 
The transaction ended in the trigger. The batch has been aborted.

In other words, the DDL trigger rollbacks all DROP TABLE command

No comments:

Post a Comment