2014-03-01 28 views
0

爲什麼添加第二個LEFT JOIN語句時以下查詢失敗?錯誤狀態爲「多部分標識符T3.ConfigIDx無法綁定」。在添加語句之前,T3.ConfigIDx列顯示在結果中。我嘗試過,但沒有T3。添加LEFT JOIN導致「多部分標識符未綁定」

爲了簡潔起見,我在INNER JOINS中刪除了一些代碼。

-- Add CfgDescription to ComponentID include QuantityWH for all components 'Used' or blank found in the order. 
    -- Attach Line information like Quantity and DiscountRate from the Order using Configuration ID. 
    SELECT 
     BDCComponentAttributes.componentID AS ComponentID, 
     BDCComponentAttributes.Value AS CfgDescription, 
     CAST (BDC10.Value AS INT) AS QuantityWH, 
     CfgIDx, 
     T3.ConfigIDx 

     FROM BDCComponentAttributes 

     Left join BDCComponentAttributes BDC10 on BDC10.ComponentID = BDCComponentAttributes.componentID and BDC10.ComponentAttributeName = 'QuantityWH' 
     Left join OrderDetails on OrderDetails.ConfigurationID = T3.ConfigIDx 


     INNER JOIN 
     (
      -- Select ComponentID's and their respective CfgDescription for components found in order. This creates derived table T3. 

       INNER JOIN 
       (

       -- Select Components in the order NOT 'NotUsed'. This creates derived table T2. 
        INNER JOIN 
         (
         -- Select ConfigurationID's for components of the order. This creates derived table T1. 
         ) AS T1  
         ON BDCComponents.CfgID = T1.CfgIDx 
        ) AS T2 
       ON BDCComponentAttributes.ComponentID = T2.ComponentID  
       WHERE BDCComponentAttributes.ComponentAttributeName = 'PartInSystem' AND ('Used' = IsNull(BDCComponentAttributes.Value,'Used') OR BDCComponentAttributes.Value='Used') 
     ) AS T3 
     ON BDCComponentAttributes.componentID = T3.ComponentID 
     WHERE BDCComponentAttributes.ComponentAttributeName = 'CfgDescription' 
     ORDER BY ComponentID     

回答

1

查詢中的from子句開始:

 FROM BDCComponentAttributes Left join 
      BDCComponentAttributes BDC10 
     on BDC10.ComponentID = BDCComponentAttributes.componentID and 
      BDC10.ComponentAttributeName = 'QuantityWH' Left join 
     OrderDetails 
     on OrderDetails.ConfigurationID = T3.ConfigIDx 

當一個查詢被編譯時,from條款解釋的詞彙順序 - 也就是說,在同「左向右「我們閱讀的」從上到下「的方式。遇到符號T3時,未定義。這是導致你的錯誤。 SQL沒有「前瞻」來查看它是在from子句中稍後定義的。

您可以通過在T3的定義後移動join條件來解決此問題。

+0

您建議在定義T3之後移動連接。我知道T3從第四行開始定義。所以我應該在那之後加入LEFT JOIN? – user3367179

+0

我把它放在最後一個ON之後。它似乎工作。謝謝! – user3367179