SQL Server:创建仅在创建特定表时运行的触发器

时间:2016-03-14 18:37:05

标签: sql-server triggers

如果我知道正在创建的特定表格。我是否可以将触发器配置为仅在创建特定表时执行?

 CREATE TRIGGER tr_trigger
 ON ALL SERVER
 FOR CREATE_TABLE
 AS
 ....
 GO

3 个答案:

答案 0 :(得分:1)

确实有可能。请参阅有关stackoverflow的以下文章:

SQL SERVER 2008 TRIGGER ON CREATE TABLE

答案 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