jeudi 2 juillet 2015

SQL column data won't display when hard-coded value is replaced with placeholder

I am trying to replace place holders with user input.

When the user goes to run the report, a GUI dialog pops up asking them to specify the start date, end date, and customer ID. I have several variables that store expressions to calculate certain outcomes. However, the data in the WHERE statements for the variables is hardcoded in, for example:

@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 
    ([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 (AR_Customers.CustomerCode = 904) 
)

This is problematic because the date will not always be the 24th of June and the customer code may not always be 904.

I created a variable called @sID and made it appear in the GUI dialog box. Then, I set AR_Customers.CustomerCode = @sID. So in the dialog box in the sID field if I enter 904 it will display the proper data.

The problem is, when I attempt to replace 904 with @sID in the WHERE statement for the variables, it doesn't display any data for that field. This is how it looks:

@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 
    ([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 (AR_Customers.CustomerCode = @sID) 
)

When I have the variable expression as it is in my first example it displays correctly, but when I swap out 904 for @sID, nothing appears in that field. What am I doing wrong that it stops displaying the value necessary? Am I on the correct track?

I am using Microsoft SQL Sever 2005.

Aucun commentaire:

Enregistrer un commentaire