SQL SERVER 2008 TRIGGER ON CREATE TABLE

后端 未结 2 536
感情败类
感情败类 2021-01-23 00:12

Is there a way to run some function like a trigger when a table is created in the database in SQL SERVER 2008?

2条回答
  •  野的像风
    2021-01-23 00:39

    Yes a DDL Trigger. For example, here is some code I written to prevent some tables from being modified:

    PRINT N'Create DDL trigger to prevent changes to various tables'
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TRIGGER NoEditCertainTables ON DATABASE 
        FOR DROP_TABLE, ALTER_TABLE, CREATE_TABLE
    AS 
    
    SET CONCAT_NULL_YIELDS_NULL ON
    
    DECLARE @AffectedTable varchar(255) 
    SELECT  @AffectedTable = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(100)') 
    
    --This is the name of the table(s) you dont want messed with
    IF (@AffectedTable IN ('Table1','Table2','Table3'))
    BEGIN
        ROLLBACK;
    END 
    SET CONCAT_NULL_YIELDS_NULL OFF      
    
    GO
    

提交回复
热议问题