更新后触发:如何对更新的每一行执行查询

时间:2019-03-27 16:07:17

标签: triggers sql-server-2017

我写了触发器以更新表中的列(retard)后执行查询,但是有时候有很多行更新了如何解决呢?

CREATE OR ALTER TRIGGER notifRetard
  ON Taches
  AFTER UPDATE
  AS
  BEGIN
      SET NOCOUNT ON;

      DECLARE @value INT

      IF UPDATE(retard)
         -- How to make this for every row updated???
         SELECT
             @value = inserted.retard
         FROM
             inserted;

         IF @value = 1
            -- run SQL query 
  END



1 个答案:

答案 0 :(得分:0)

如果其他人需要的解决方案是使用CURSOR。

    CREATE or alter TRIGGER notifRetard
    ON  Taches
    AFTER UPDATE

    AS
      BEGIN
        SET NOCOUNT ON;
         IF UPDATE(retard)
         begin
            DECLARE @RefTache varchar(50),@RefPhase numeric(4,0),@IDprojet varchar(50),@IDressource varchar(50) @retard bit;

             DECLARE  TrigTempUpdate_Cursor CURSOR FOR

         SELECt RefTache,RefPhase,IDprojet,IDressource,retard
    FROM
      inserted;

          begin
        OPEN TrigTempUpdate_Cursor;

        FETCH NEXT FROM TrigTempUpdate_Cursor INTO @RefTache, @RefPhase,@IDprojet,@IDressource,@retard

        WHILE @@FETCH_STATUS = 0

        BEGIN
         if @retard=1
        --DO QUERY HERE
        FETCH NEXT FROM TrigTempUpdate_Cursor INTO @RefTache, @RefPhase,@IDprojet,@IDressource,@retard
        END;
        end;
        CLOSE TrigTempUpdate_Cursor;

        DEALLOCATE TrigTempUpdate_Cursor;

        end;
        end;