使用IF语句的SQL Server触发器

时间:2015-10-10 10:17:02

标签: sql-server database triggers

如何编写触发器,当插入特定表时,它会根据条件检查属性,然后将文本附加到另一个属性。我将如何实现IF THEN逻辑呢?

    CREATE TABLE SomeTable (
      table_id NUMBER(5) NOT NULL
      paragraph varchar(50)
    );



 CREATE TRIGGER Append_Text ON SomeTable
    AFTER INSERT 
    AS
    BEGIN 

    /*pseudocode: if table_id = 5 then append text "mark watney" to paragraph*/ 

    END 
    GO

1 个答案:

答案 0 :(得分:0)

您需要创建instead of insert trigger

CREATE TRIGGER Append_Text ON SomeTable
INSTEAD OF INSERT 
AS
BEGIN 
    INSERT INTO SomeTable
    SELECT table_id, paragraph + case when table_id = 5 then ' mark watney' else '' end 
    FROM INSERTED
END 
GO