更新触发器中的多行更新

时间:2016-10-28 09:06:57

标签: sql sql-server triggers

如何更改此代码以使用多行更新。@ id为int,order_id为主键,但我想检查每行有status_b =' bagged'

select @id=inserted.order_id from inserted;
 if update(status_b)
  begin
  if (select status_b from inserted)='bagged'
     begin
       if (select o.id 
                 from [order] o 
                 left join [print] p on o.id=p.order_id
                 left join [laminate] l on l.order_id=o.id
                 left join [slitting] s on s.order_id=o.id
                 left join [bagging] b on b.order_id=o.id
                 where                           
                             1=(case when (o.printing='دارد' and status_p='printed') or o.printing='ندارد' then 1 end)
                         and 1=(case when (o.lamination='دارد' and status_l1='laminated') or o.lamination='ندارد' then 1 end)
                         and 1=(case when (o.lamination='دارد' and l.layer2<>'' and status_l2='laminated2') or o.lamination='ندارد' then 1 end)
                         and 1=(case when (o.coldseal='دارد' and status_c='coldsealed') or o.coldseal='ندارد' then 1 end)
                         and 1=(case when (o.slitting='دارد' and status_s='slitted') or o.slitting='ندارد' then 1 end)
                         and 1=(case when (o.bagging='دارد' and status_b='bagged') or o.bagging='ندارد' then 1 end)
                         and o.id=@id) is not null
        begin
        update [order] set [status]='finished' where id=@id
        end
     end
  end 

2 个答案:

答案 0 :(得分:2)

首先看一下你的触发器的目的(总线规则)。

如果不需要大查询的结果。你可以在直接更新站中绑定相同的连接和条件,这样可以提供多个orderid并且性能可以&gt;这个查询可能有bug,你要修复它。尝试这样的事情。

UPDATE A SET [STATUS]='FINISHED' FROM [ORDER] A 
INNER JOIN INSERTED C 
ON  A.ID=B.OID
LEFT JOIN [PRINT] P ON O.ID=P.ORDER_ID
LEFT JOIN [LAMINATE] L ON L.ORDER_ID=O.ID
LEFT JOIN [SLITTING] S ON S.ORDER_ID=O.ID
LEFT JOIN [BAGGING] B ON B.ORDER_ID=O.ID
WHERE                           
1=(CASE WHEN (O.PRINTING='دارد' AND STATUS_P='PRINTED') OR O.PRINTING='ندارد' THEN 1 END)
AND 1=(CASE WHEN (O.LAMINATION='دارد' AND STATUS_L1='LAMINATED') OR O.LAMINATION='ندارد' THEN 1 END)
AND 1=(CASE WHEN (O.LAMINATION='دارد' AND L.LAYER2<>'' AND STATUS_L2='LAMINATED2') OR O.LAMINATION='ندارد' THEN 1 END)
AND 1=(CASE WHEN (O.COLDSEAL='دارد' AND STATUS_C='COLDSEALED') OR O.COLDSEAL='ندارد' THEN 1 END)
AND 1=(CASE WHEN (O.SLITTING='دارد' AND STATUS_S='SLITTED') OR O.SLITTING='ندارد' THEN 1 END)
AND 1=(CASE WHEN (O.BAGGING='دارد' AND STATUS_B='BAGGED') OR O.BAGGING='ندارد' THEN 1 END)

答案 1 :(得分:0)

尝试下面从inserted获取所有订单ID并插入临时表并执行操作

SELECT INSERTED.ORDER_ID AS OID INTO #TEMP1 FROM INSERTED WHERE STATUS_B='bagged';
 IF UPDATE(STATUS_B)
  BEGIN

       IF (SELECT O.ID 
                 FROM [ORDER] O 
                 join #temp1 t
                 on t.OID=o.id
                 LEFT JOIN [PRINT] P ON O.ID=P.ORDER_ID
                 LEFT JOIN [LAMINATE] L ON L.ORDER_ID=O.ID
                 LEFT JOIN [SLITTING] S ON S.ORDER_ID=O.ID
                 LEFT JOIN [BAGGING] B ON B.ORDER_ID=O.ID

                 WHERE                           
                             1=(CASE WHEN (O.PRINTING='دارد' AND STATUS_P='PRINTED') OR O.PRINTING='ندارد' THEN 1 END)
                         AND 1=(CASE WHEN (O.LAMINATION='دارد' AND STATUS_L1='LAMINATED') OR O.LAMINATION='ندارد' THEN 1 END)
                         AND 1=(CASE WHEN (O.LAMINATION='دارد' AND L.LAYER2<>'' AND STATUS_L2='LAMINATED2') OR O.LAMINATION='ندارد' THEN 1 END)
                         AND 1=(CASE WHEN (O.COLDSEAL='دارد' AND STATUS_C='COLDSEALED') OR O.COLDSEAL='ندارد' THEN 1 END)
                         AND 1=(CASE WHEN (O.SLITTING='دارد' AND STATUS_S='SLITTED') OR O.SLITTING='ندارد' THEN 1 END)
                         AND 1=(CASE WHEN (O.BAGGING='دارد' AND STATUS_B='BAGGED') OR O.BAGGING='ندارد' THEN 1 END)
                         ) IS NOT NULL
        BEGIN
        UPDATE A SET [STATUS]='FINISHED' FROM [ORDER] A JOIN #TEMP1 C ON  A.ID=B.OID 
        END
     END