復制代碼 代碼如下:
-- Create a log table
CREATE TABLE TriggerLog (LogInfo xml)
-- Create a dummy table to delete later on
CREATE TABLE TableToDelete (Id int PRIMARY KEY)
-- 新建一個新表,作為刪除實驗表
INSERT INTO TableToDelete VALUES(1)
GO
-- 創建一個Drop Table 的 DDL
CREATE TRIGGER StopTableDrop ON DATABASE AFTER DROP_TABLE
AS
DECLARE @EventData AS xml
SET @EventData = EVENTDATA() -- 必須要在rollback之前截獲DDL信息
ROLLBACK
PRINT 'DROP TABLE attempt in database ' + DB_NAME() + '.'
INSERT INTO TriggerLog VALUES(@EventData)
執行如下刪除操作:
DROP TABLE TableToDelete
會觸發上面的DDL,從而回滾操作。
執行下面的Sql:
SELECT * FROM TriggerLog
查看剛才截獲的DDL信息。