سؤال

Why does the below query fail when I add the second LEFT JOIN statement? The error states "Multi-part identifier T3.ConfigIDx could not be bound". Prior to adding the statement the column T3.ConfigIDx showed in the results. I tried with and with out T3.

I removed some code within the INNER JOINS for brevity.

    -- 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                   

لا يوجد حل صحيح

نصائح أخرى

The from clause in your query starts:

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

When a query is compiled, the from clause is interpreted in lexical ordering -- that is, in the same "left-to-right" "top-to-bottom" way that we read. When the symbol T3 is encountered, it is not defined. That is causing your error. SQL does not have "look-ahead" to see that it is defined later in the from clause.

You can fix this by moving the join condition after the definition of T3.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top