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

后端 未结 7 865
夕颜
夕颜 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:39

    For triggers in general, you need to use a cursor to handle inserts or updates of multiple rows. For example:

    DECLARE @Attribute;
    DECLARE @ParameterValue;
    DECLARE mycursor CURSOR FOR SELECT Attribute, ParameterValue FROM inserted;
    OPEN mycursor;
    FETCH NEXT FROM mycursor into @Attribute, @ParameterValue;
    WHILE @@FETCH_STATUS = 0
    BEGIN
    
    If @Attribute LIKE 'NoHist_%'
          Begin
              Return
          End
    
    etc.
    
    FETCH NEXT FROM mycursor into @Attribute, @ParameterValue;
    END
    

    Triggers, at least in SQL Server, are a big pain and I avoid using them at all.

    0 讨论(0)
提交回复
热议问题