从Select语句的总和更新值

时间:2017-07-28 06:51:11

标签: sql sql-server

var eventEmitter = new event.EventEmitter();
var fs = require("fs");
var data = '';

var read = fs.createReadStream("demo.txt");
read.setEncoding('UTF8');

read.on('data', function(resp) {  
    console.log(resp);
});

data='some data';
var writeStream = fs.createWriteStream("demo.txt");
writeStream.write(data,'UTF8');
writeStream.end();
writeStream.on('finish',function(){
    console.log("finish");
});

如何简化上述查询?有没有办法更快地执行此查询?我正在将表值参数(TVP)中的值更新为表格。

1 个答案:

答案 0 :(得分:0)

 Try this way this would be helpful to you:
 Also if found any difficulty let me know. 

 @dt TVP_API_DELIVERY_ORDER  READONLY    
                    UPDATE  Stkm SET
                            Stkm.Itm_Qty_OnHand = CASE 
                                                    WHEN tvp.Flag='I' THEN (Itm_Qty_OnHand- TmporderQunatity.orderQuantity) 
                                                    WHEN tvp.Flag='U' THEN ((Itm_Qty_OnHand+TmpDodQunatity.DodQuantity)-TmporderQunatity.orderQuantity)
                                                    WHEN tvp.Flag='D' THEN (Itm_Qty_OnHand + TmpDodQunatity.DodQuantity)                                          
                                                    ELSE 0
                                                  END       
                    FROM     @dt tvp 
                    INNER JOIN Stkm ON tvp.ItemCode=Stkm.Itm_Cd
                    CROSS APPLY (
                        SELECT SUM(T1.OrderQty) AS orderQuantity FROM @dt T1 
                        WHERE T1.ItemCode=tvp.ItemCode
                    )AS TmporderQunatity
                    CROSS APPLY (
                        SELECT SUM(T2.Dod_Qty) AS DodQuantity FROM @dt T2
                        WHERE T2.ItemCode=tvp.ItemCode AND T2.Do_No=@OrderNo AND T2.Do_Year=@Year
                    )AS TmpDodQunatity