如果我知道正在创建的特定表格。我是否可以将触发器配置为仅在创建特定表时执行?
CREATE TRIGGER tr_trigger
ON ALL SERVER
FOR CREATE_TABLE
AS
....
GO
答案 0 :(得分:1)
确实有可能。请参阅有关stackoverflow的以下文章:
答案 1 :(得分:0)
您正在寻找的答案可以在本文中找到: https://technet.microsoft.com/en-us/library/ms186406(v=sql.105).aspx
CREATE TRIGGER safety
ON DATABASE
FOR CREATE_TABLE
AS
PRINT 'You are not allowed to create a table'
ROLLBACK
;
答案 2 :(得分:0)
IF EXISTS (SELECT * FROM sys.server_triggers
WHERE name = 'ddl_trig_database')
DROP TRIGGER ddl_trig_database
ON ALL SERVER;
GO
CREATE TRIGGER ddl_trig_database
ON ALL SERVER
FOR CREATE_DATABASE
AS
PRINT 'Database Created.'
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText) [1]','nvarchar(max)')
GO
DROP TRIGGER ddl_trig_database
ON ALL SERVER;
GO