更新临时表

时间:2018-01-28 11:35:20

标签: sql sql-server

我需要更新sql 2014中的临时表,但这段代码似乎有误。 首先,我从发票中选择所有发票及其数量,并将库存数量设置为0.第二个语句我需要按库存数量更新每一行,以便稍后进行比较并使用此代码。

declare @DifferenceTable  table (
        DataAreaId nvarchar(10) ,
        SalesId nvarchar(50) ,
        InvoiceId nvarchar(50) ,
        ItemId nvarchar(50),
        InvoiceQty decimal(16,4) ,
        InventoryQty decimal(16,4)
    )

insert into @DifferenceTable (DataAreaId , SalesId , InvoiceId , ItemId , InvoiceQty , InventoryQty ) select CustInvoiceTrans.DATAAREAID , CustInvoiceTrans.SALESID , CustInvoiceTrans.INVOICEID , CustInvoiceTrans.ITEMID , sum(CustInvoiceTrans.QTY) , 0. as InventoryQty from CustInvoiceTrans with(nolock) group by CustInvoiceTrans.DATAAREAID , CustInvoiceTrans.SALESID , CustInvoiceTrans.INVOICEID , CustInvoiceTrans.ITEMID update @DifferenceTable set InventoryQty = tt.Qty from (select sum(InventTrans.QTY)*-1 as Qty , InventTransOrigin.DATAAREAID , REFERENCEID , InventTrans.INVOICEID , InventTrans.ITEMID from InventTransOrigin left join InventTrans on InventTrans.INVENTTRANSORIGIN = InventTransOrigin.RECID and InventTrans.DATAAREAID = InventTransOrigin.DATAAREAID and InventTrans.ITEMID = InventTransOrigin.ITEMID where REFERENCECATEGORY = 0 group by InventTransOrigin.DATAAREAID , REFERENCEID , InventTrans.INVOICEID , InventTrans.ITEMID ) tt where @DifferenceTable.DataAreaId = tt.DATAAREAID and @DifferenceTable.SalesId = tt.REFERENCEID and tt.INVOICEID = @DifferenceTable.InvoiceId and tt.ITEMID = @DifferenceTable.ItemId select * from @DifferenceTable`

0 个答案:

没有答案