【问题标题】:Adding LEFT JOIN causes "multi-part identifier not bound"添加 LEFT JOIN 导致“未绑定多部分标识符”
【发布时间】:2014-03-01 03:54:55
【问题描述】:

当我添加第二个 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                   

【问题讨论】:

    标签: sql left-join


    【解决方案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 条件来解决此问题。

    【讨论】:

    • 您建议在定义 T3 后将连接移至。我知道 T3 是在最后的第 4 行定义的。所以我应该在那之后放置 LEFT JOIN?
    • 我把它放在最后一个 ON 之后。它似乎工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多