当前位置: 代码迷 >> Sql Server >> sql server触发器批量更新只对第一条记录起作用是咋回事
  详细解决方案

sql server触发器批量更新只对第一条记录起作用是咋回事

热度:55   发布时间:2016-04-24 10:47:27.0
sql server触发器批量更新只对第一条记录起作用是怎么回事
写了一个SQL SERVER触发器,对表中的记录进行单行修改没有任何问题,但是批量更新的时候发现触发器只对第一条记录进行了触发,其它行记录均没有进行更改是何原因?
ALTER trigger [tr2_SEOutStockEntry_Qty21] on [dbo].[SEOutStockEntry] --with encryption 
for update as
begin
if update (fcommitqty)
begin
        declare @IsBase int  

        select @IsBase=t3.FBase
        from inserted t0
        inner join SEOutStockEntry t1 on t0.finterid = t1.finterid and t0.fentryid = t1.fentryid
        inner join SEOutStock t2 on t1.finterid = t2.finterid
        inner join tb_seorderentry2 t3 on t1.FSourceEntryID = t3.FEntryID
        where t2.ftrantype = 83 and t1.fsourcetrantype = 220090331

        update t1 set t1.FEntrySelfS0238 =
         case when isnull(t1.fCommitQty,0) <= 0 then '' 
         else 
         case when isnull(t1.fCommitQty,0) < t1.fQty then '部分' 
         else '全部'  
         end 
         end
         from inserted t0 
         inner join SEOutStockEntry t1 on t0.finterid = t1.finterid and t0.fentryid = t1.fentryid
         inner join SEOutStock t2 on t1.finterid = t2.finterid
         --inner join tb_seorder t3 on t3.FBillNo = t1.FSourceBillNo
         inner join tb_seorderentry2 t4 on t4.FEntryID = t1.FSourceEntryID and t4.FBase = @IsBase
         where t2.ftrantype = 83 and t1.fsourcetrantype = 220090331

-- 2009-06-03 此处需要根据实际的单据自定义调整列名 FHeadSelfS0242
-- select ffieldname from ictemplate where fid = 's02' and fcaption = '执行状态:'
update t1 set t1.FHeadSelfS0242 = 
case when t2.fOverRowCount = t2.fRowCount then '全部' 
else case when t2.fSumCommitQty > 0 then '部分' else '' end end
from inserted t0
inner join SEOutStock t1 on t0.finterid = t1.finterid
inner join (
select fInterID,fsourcetrantype,count(1) fRowCount,sum(fCommitQty) fSumCommitQty,
            --FSourceBillNo,
sum(case when fCommitQty >= fQty then 1 else 0 end) fOverRowCount
from SEOutStockEntry group by fInterID,fsourcetrantype--,FSourceBillNo
) t2 on t1.finterid = t2.finterid
        --inner join tb_seorder t3 on t3.FBillNo = t2.FSourceBillNo
        --inner join tb_seorderentry2 t4 on t4.FEntryID = t2.FSourceEntryID and t4.FBase = 0
where t1.ftrantype = 83 and t2.fsourcetrantype = 220090331

end
end
------解决方案--------------------
引用:
Quote: 引用:

select @IsBase=t3.FBase
        from inserted t0
        inner join SEOutStockEntry t1 on t0.finterid = t1.finterid and t0.fentryid = t1.fentryid
        inner join SEOutStock t2 on t1.finterid = t2.finterid
        inner join tb_seorderentry2 t3 on t1.FSourceEntryID = t3.FEntryID
        where t2.ftrantype = 83 and t1.fsourcetrantype = 220090331

这个取的是inserted中最后一条记录的值

用游标的话一直是执行状态,无法完成更新

那就用临时表吧
  相关解决方案