2017-07-28 22 views
0
 @dt TVP_API_DELIVERY_ORDER READONLY  
      UPDATE Stkm SET 
        Stkm.Itm_Qty_OnHand = CASE 
              WHEN tvp.Flag='I' THEN (Itm_Qty_OnHand- (SELECT SUM(T1.OrderQty) FROM @dt T1 WHERE T1.ItemCode=tvp.ItemCode)) 
              WHEN tvp.Flag='U' THEN ((Itm_Qty_OnHand+(SELECT SUM(T2.Dod_Qty) FROM DoD T2 WHERE T2.Itm_cd=tvp.ItemCode AND [email protected] AND [email protected]))-(SELECT SUM(T3.OrderQty) FROM @dt T3 WHERE T3.ItemCode=tvp.ItemCode)) 
              WHEN tvp.Flag='D' THEN (Itm_Qty_OnHand + (SELECT SUM(T4.Dod_Qty) FROM DoD T4 WHERE T4.Itm_cd=tvp.ItemCode AND [email protected] AND [email protected]))           
              ELSE 0 
              END  
      FROM  @dt tvp 
      INNER JOIN Stkm ON tvp.ItemCode=Stkm.Itm_Cd 

上記のクエリを簡略化する方法。上記のクエリの実行を高速化する方法はありますか?テーブル値パラメータ(tvp)からテーブルに値を更新するSQL ServerのSelectステートメントの合計からの値の更新

答えて

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 [email protected] AND [email protected] 
        )AS TmpDodQunatity 
関連する問題