添加LEFT JOIN会导致“多部分标识符未绑定”

时间:2014-03-01 03:54:56

标签: sql left-join

为什么在添加第二个LEFT JOIN语句时,以下查询失败?错误状态“无法绑定多部分标识符T3.ConfigIDx”。在添加语句之前,结果中显示了列T3.ConfigIDx。我试着和T3一起尝试。

为简洁起见,我在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 个答案:

答案 0 :(得分: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子句中稍后定义它。

您可以通过在join定义后移动T3条件来解决此问题。