使用占位符

时间:2015-07-02 18:43:29

标签: sql variables sql-server-2005 placeholder

我正在尝试用用户输入替换占位符。

当用户开始运行报告时,会弹出一个GUI对话框,要求他们指定开始日期,结束日期和客户ID。 我有几个变量存储表达式来计算某些结果。但是,变量的WHERE语句中的数据是硬编码的,例如:

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

这是有问题的,因为日期并不总是6月24日,客户代码可能并不总是904。

我创建了一个名为@sID的变量,并将其显示在GUI对话框中。然后,我设置AR_Customers.CustomerCode = @sID。所以在sID字段的对话框中,如果我输入904,它将显示正确的数据。

问题是,当我尝试用变量的@sID语句中的WHERE替换904时,它不会显示该字段的任何数据。 这是它的外观:

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

当我有第一个例子中的变量表达式时,它显示正确,但是当我为@sID换出904时,该字段中没有任何内容。我做错了什么,它停止显示必要的价值?我在正确的轨道上吗?

我正在使用Microsoft SQL Sever 2005。

整个代码:

SET NOCOUNT ON; 
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);
DECLARE @Shrink Decimal(19,8);
DECLARE @Prod_CostLBS Decimal(19,8);
DECLARE @Cost Decimal(19,8);
DECLARE @Profit Decimal(19,8);
DECLARE @Proj Decimal(19,8);
DECLARE @sID Decimal(19,8);

SET @PurchaseCost = 2.58;
SET @InputWeight = 18100;
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 
 ([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) 
);
SET @Prod_CostLBS  = .15;
SET @Cost  = ROUND((@PurchaseCost + @Prod_CostLBS) * (
 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 ((1=1) AND AR_Customers.CustomerKey IN (124)) 
), 2);
SET @Profit  = (
 SELECT SUM(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2))
 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)) 
) - @Cost;
SET @Proj  = ROUND((@Profit) / (
 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 ((1=1) AND AR_Customers.CustomerKey IN (124)) 
), 2)
;
SET @sID = 904;

SELECT DISTINCT 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40)) AS [Supplier]
   , [PC].ProductCode
   , [PC].Description1
   , Count(IC_ProductLots.OriginalQuantity_Alt) AS [Boxes]
   , IC_ProductLots.UnitOfMeasure_Alt
   , Sum(IC_ProductLots.OriginalQuantity_Stk) AS [Weight]
   , IC_ProductLots.UnitOfMeasure_Stk
   , [ICP].UnitCost AS [Unit Cost]
   , Sum(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) AS [Total Sales]
   , Avg(([IC_ProductLots].[OriginalQuantity_Stk] / [IC_ProductLots].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
   , Sum([IC_ProductLots].[OriginalQuantity_Stk] / @InputWeight) AS [Yield]
   , @Shrink AS [Shrink]
   , @Cost AS [Cost]
   , @Profit AS [Profit]
   , @Proj AS [Proj]
   , @sID AS [sID]
 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 
     ON [DCT].LotKey = IC_ProductLots.LotKey)
    LEFT OUTER JOIN  IC_ProductCosts [ICP] 
     ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
 WHERE 
    (IC_ProductLots.ProductionDate >= { ts '2015-06-24 00:00:00' }   AND (IC_ProductLots.ProductionDate <= { ts '2015-06-24 00:00:00' } OR IC_ProductLots.ProductionDate Is Null)) 
 GROUP BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))
   , [PC].ProductCode
   , [PC].Description1
   , IC_ProductLots.UnitOfMeasure_Alt
   , IC_ProductLots.UnitOfMeasure_Stk
   , [ICP].UnitCost
   , IC_ProductLots.ProductionDate
   , AR_Customers.CustomerCode
 HAVING 
    (AR_Customers.CustomerCode = @sID)
 ORDER BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))

1 个答案:

答案 0 :(得分:0)

我尝试稍微清理一下你的查询,但它仍然可能有错误,因为我没有你的桌子。但是这样的事情可能有用:

&#13;
&#13;
SET NOCOUNT ON; 
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);
DECLARE @Shrink Decimal(19,8);
DECLARE @Prod_CostLBS Decimal(19,8);
DECLARE @Cost Decimal(19,8);
DECLARE @Profit Decimal(19,8);
DECLARE @Proj Decimal(19,8);
DECLARE @sID Decimal(19,8);

SET @PurchaseCost = 2.58;
SET @InputWeight = 18100;
SET @Prod_CostLBS  = .15;
SET @sID = 904;

SELECT DISTINCT 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40)) AS [Supplier]
   , [PC].ProductCode
   , [PC].Description1
   , Count(IC_ProductLots.OriginalQuantity_Alt) AS [Boxes]
   , IC_ProductLots.UnitOfMeasure_Alt
   , Sum(IC_ProductLots.OriginalQuantity_Stk) AS [Weight]
   , IC_ProductLots.UnitOfMeasure_Stk
   , [ICP].UnitCost AS [Unit Cost]
   , Sum(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) AS [Total Sales]
   , Avg(([IC_ProductLots].[OriginalQuantity_Stk] / [IC_ProductLots].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
   , Sum([IC_ProductLots].[OriginalQuantity_Stk] / @InputWeight) AS [Yield]
   , @InputWeight - OriginalQuantitySum AS [Shrink]
   , ROUND((@PurchaseCost + @Prod_CostLBS) * OriginalQuantitySum, 2) AS [Cost]
   , Profit AS [Profit]
   , Proj AS [Proj]
   , @sID AS [sID]
 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 
     ON [DCT].LotKey = IC_ProductLots.LotKey)
    LEFT OUTER JOIN  IC_ProductCosts [ICP] 
     ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
	left join (SELECT Sum([ICPL].[OriginalQuantity_Stk] as OriginalQuantitySum, SUM(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) as Profit,
				Sum([ICPL].[OriginalQuantity_Stk] as OriginalQuantitySum / SUM(ROUND([DCT].[Quantity_Stk] *[ICP].[UnitCost], 2)) as Proj)
			 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)) GB
		on GB.CustomerCode = AR_Customers.CustomerCode
 WHERE 
    (IC_ProductLots.ProductionDate >= { ts '2015-06-24 00:00:00' }   AND (IC_ProductLots.ProductionDate <= { ts '2015-06-24 00:00:00' } OR IC_ProductLots.ProductionDate Is Null)
	and AR_Customers.CustomerCode = @sID) 
 GROUP BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))
   , [PC].ProductCode
   , [PC].Description1
   , IC_ProductLots.UnitOfMeasure_Alt
   , IC_ProductLots.UnitOfMeasure_Stk
   , [ICP].UnitCost
   , IC_ProductLots.ProductionDate
   , AR_Customers.CustomerCode
 ORDER BY 
     CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))
&#13;
&#13;
&#13;