Trigger to fire only if a condition is met in SQL Server

后端 未结 7 948
夕颜
夕颜 2020-12-03 07:14

I hope this is a simple enough question for any SQL people out there...

We have a table which hold system configuration data, and this is tied to a history table via

7条回答
  •  半阙折子戏
    2020-12-03 07:17

    Given that a WHERE clause did not work, maybe this will:

    CREATE TRIGGER 
        [dbo].[SystemParameterInsertUpdate]
    ON 
        [dbo].[SystemParameter]
    FOR INSERT, UPDATE 
    AS
      BEGIN
        SET NOCOUNT ON
    
          If (SELECT Attribute FROM INSERTED) LIKE 'NoHist_%'
          Begin
              Return
          End
    
          INSERT INTO SystemParameterHistory 
          (
            Attribute,
            ParameterValue,
            ParameterDescription,
            ChangeDate
          )
        SELECT
          Attribute,
          ParameterValue,
          ParameterDescription,
          ChangeDate
        FROM Inserted AS I
    END
    

提交回复
热议问题