如何在插入到其他表之前的存储过程中的Sum和Group By

时间:2017-03-15 01:11:55

标签: sql-server stored-procedures

我有表Purchasing.tblGoodsReceive(tableA)和Accounting.tblGeneralLedger(tableB)。

我想编写一个存储过程来将数据插入tableA然后插入tableB,但是在tableB中我想只插入Sum和Group By。

如需说明,请参阅屏幕截图。

INSERT INTO Purchasing.tblGoodsReceive (GoodsReceiveID, DateReceived, DeliveryOrder, VendorID, Warehouse, PostingToGJ, ReceivedBy, CheckedBy, CorrectionBy, ApprovedBy, AcceptedBy, CreatedBy)
OUTPUT inserted.ID INTO @NewID(ID)
VALUES (@Param1, @Param2, @Param3, @Param4, @Param5, @Param5a, @Param6, @Param6a, @Param6b, @Param6c, @Param6d, @Param7);

MERGE Purchasing.tblGoodsRecieveDetail AS old
USING (SELECT 
           x.value('ID[1]', 'int') AS ID,
           x.value('GoodsRecieveID[1]', 'int') AS GoodsRecieveID,
           x.value('NoInvoice[1]', 'int') AS NoInvoice,
           x.value('NoPO[1]', 'int') AS OrderID,
           x.value('ItemID[1]', 'int') AS ItemID,
           x.value('ReceivedQty[1]', 'int') AS ReceivedQty,
           x.value('UnitPrice[1]', 'money') AS UnitPrice,
           x.value('Complete[1]', 'bit') AS Complete,
           @ParamRateItem AS RateID
       FROM 
           @ParamXml1.nodes('/NewDataSet/vItemsReceivingItem') XmlData(x)) AS new ON (new.ID = old.ID)

    WHEN NOT MATCHED AND new.ItemID IS NOT NULL 
       THEN 
        INSERT (GoodsReceiveID, NoInvoice, OrderID, ItemCode, ReceivedQty, UnitPrice, Complete, RateID)
        Values((SELECT ID FROM @NewID), new.NoInvoice, new.OrderID, new.ItemID, new.ReceivedQty, new.UnitPrice, new.Complete, new.RateID);

如需说明,请参阅屏幕截图

screenshot

0 个答案:

没有答案