with 的使用

 begin tran
               declare @count int
               select @count = count(1) from [@FSCXFEMPREQITM] where DocEntry = @Doc
               while(@count > 0)
                 begin
             if(object_id('tempdb..#tempFSCXFEMPREQITM') > 0)
       drop table #tempFSCXFEMPREQITM
       select * into #tempFSCXFEMPREQITM from
       (
       select b.* from  [@FSCXFEMPREQITM] b
        where b.DocEntry >= @Doc and b.U_Stts = '0'  and exists(
          select 1 from  [@FSCXFEMPREQITM] d
          where isnull(b.U_DealCode,'') = isnull(d.U_DealCode,'') and b.U_ItemCode = d.U_ItemCode
                               and b.U_FromCode = d.U_FromCode
          and b.U_ItemAge = d.U_ItemAge
                               and isnull(b.U_LottoCode,'') = isnull(d.U_LottoCode,'') and b.U_SWhsEntry = d.U_SWhsEntry
          and b.U_PosCode = d.U_PosCode and b.U_DocType = d.U_DocType and d.DocEntry = @Doc 
       )) a
       ;with Hong as (
          select DocEntry,cast(U_Num as decimal(19,6)) U_Num,cast(U_Numed as decimal(19,6)) U_Numed,cast(U_Qty as decimal(19,6)) U_Qty,cast(U_Qtyed as decimal(19,6)) U_Qtyed,parentEntry=(select min(DocEntry) from #tempFSCXFEMPREQITM where DocEntry > @Doc) from #tempFSCXFEMPREQITM where DocEntry = @Doc
          union all
          select a.DocEntry,cast(b.U_Num-b.U_Numed as decimal(19,6)),cast(a.U_Numed as decimal(19,6)) ,cast(b.U_Qty-b.U_Qtyed as decimal(19,6)),cast(a.U_Qtyed as decimal(19,6)),parentEntry = (select  DocEntry from #tempFSCXFEMPREQITM b where b.DocEntry > a.DocEntry and not exists(select * from #tempFSCXFEMPREQITM c where c.DocEntry > a.DocEntry and b.DocEntry > c.DocEntry) )
          from #tempFSCXFEMPREQITM a inner join Hong b on a.DocEntry = b.parentEntry
       )
       update a set a.U_Num = b.U_Num,a.U_Qty = b.U_Qty from [@FSCXFEMPREQITM] a inner join Hong b on a.DocEntry = b.DocEntry
                            and b.U_Num-b.U_Numed > 0 and b.U_Qty - b.U_Qtyed > 0
                            set @count = @count-1
                    end
                  if(@@error <> 0)
                      rollback tran
                  commit tran
                  if(object_id('tempdb..#tempFSCXFEMPREQITM') > 0)
       drop table #tempFSCXFEMPREQITM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值