简体   繁体   中英

The multi-part identifier could not be bound SQL Server error

I searched SO and Google for awhile but couldn't seem to find any that related to this error occurring in the set part of the code and if it did occur in that part, the solution didn't help me.

I am trying to use a user inputted weight and subtract it by the total weight. This equals the shrink

SET @Shrink  = @InputWeight -  [ICPL].[OriginalQuantity_Stk];

I also tried it this way and got the same error.

SET @Shrink  = @InputWeight - Sum([ICPL].[OriginalQuantity_Stk]);

The table is called IC_ProductLots and the alias is ICPL

The error I get is:

The multi-part identifier "ICPL.OriginalQuantity_Stk" could not be bound.

Here are my joins if that helps at all:

FROM 
    (((( IC_Products [PC] 
INNER JOIN  
    DC_Transactions [DCT] ON [PC].ProductKey = [DCT].ProductKey)
INNER JOIN  
    AR_Customers ON [DCT].CustomerKey = AR_Customers.CustomerKey)
INNER JOIN  
    IC_ProductLots [ICPL] ON [DCT].LotKey = [ICPL].LotKey)
LEFT OUTER JOIN  
    IC_ProductCosts [ICP] ON ICP.ProductKey = PC.ProductKey 
                          AND ICP.ProductCostCode = 5)

Let me know if seeing any more code would help.

SET NOCOUNT ON; 
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);
DECLARE @Shrink Decimal(19,8);

SET @PurchaseCost = 1;
SET @InputWeight = 20;
SET @Shrink  = @InputWeight;

SELECT DISTINCT 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40)) AS [Supplier]
   , [PC].ProductCode
   , [PC].Description1
   , Count([ICPL].OriginalQuantity_Alt) AS [Boxes]
   , [ICPL].UnitOfMeasure_Alt
   , Sum([ICPL].OriginalQuantity_Stk) AS [Weight]
   , [ICPL].UnitOfMeasure_Stk
   , [ICP].UnitCost AS [Unit Cost]
   , Sum(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) AS [Total Sales]
   , Avg(([ICPL].[OriginalQuantity_Stk] / [ICPL].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
   , @Shrink AS [Shrink]
 FROM (((( IC_Products [PC] 
    INNER JOIN  DC_Transactions [DCT] 
     ON [PC].ProductKey = [DCT].ProductKey)
    INNER JOIN  AR_Customers 
     ON [DCT].CustomerKey = AR_Customers.CustomerKey)
    INNER JOIN  IC_ProductLots [ICPL] 
     ON [DCT].LotKey = [ICPL].LotKey)
    LEFT OUTER JOIN  IC_ProductCosts [ICP] 
     ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
 WHERE 
    ([ICPL].ProductionDate >= { ts '2015-06-24 00:00:00' }   AND ([ICPL].ProductionDate <= { ts '2015-06-24 00:00:00' } OR [ICPL].ProductionDate Is Null)) 
AND ((1=1)  AND AR_Customers.CustomerKey IN (124) ) 
 GROUP BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))
   , [PC].ProductCode
   , [PC].Description1
   , [ICPL].UnitOfMeasure_Alt
   , [ICPL].UnitOfMeasure_Stk
   , [ICP].UnitCost
   , [ICPL].ProductionDate
   , AR_Customers.CustomerKey
 ORDER BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))

Would it be easier if I created another variable that calculated the sum of the original stock and then did @InputWeight - @TotalSum ?

Sample Output: Input weight 1,000

Product Code | Boxes | Weight | Shrink
1234 | 2 | 250 |
2324 | 5 | 225 |
3234 | 4 | 175 |
4234 | 1 | 250 |
______________________________________
Total: | 12 | 900 | 100

The shrink is the difference of the input weight and the total weight. If it appears in every row that is okay, but the number should appear as 100 in every row if it follows this example.

Referencing a field in a SET statement won't work - you need to actually SELECT the data at some point. Without seeing more of your code, I can't offer any guarantees, but I believe that you are trying to do something like the following:

SET @Shrink  = @InputWeight - 
  (
    SELECT Sum([ICPL].[OriginalQuantity_Stk])
    FROM IC_Products [PC] 
        INNER JOIN  DC_Transactions [DCT] 
         ON [PC].ProductKey = [DCT].ProductKey
        INNER JOIN  AR_Customers 
         ON [DCT].CustomerKey = AR_Customers.CustomerKey
        INNER JOIN  IC_ProductLots [ICPL] 
         ON [DCT].LotKey = [ICPL].LotKey
        LEFT OUTER JOIN  IC_ProductCosts [ICP] 
         ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5
    WHERE ... -- add in your WHERE clause here
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM