【问题标题】:SQL column data won't display when hard-coded value is replaced with placeholder将硬编码值替换为占位符时,不会显示 SQL 列数据
【发布时间】:2015-07-03 04:03:58
【问题描述】:

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

当用户开始运行报告时,会弹出一个 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,它将显示正确的数据。

问题是,当我尝试在变量的 WHERE 语句中将 904 替换为 @sID 时,它不会显示该字段的任何数据。 看起来是这样的:

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

当我拥有第一个示例中的变量表达式时,它可以正确显示,但是当我将 904 换成 @sID 时,该字段中什么也没有出现。我做错了什么,它停止显示必要的值?我在正确的轨道上吗?

我使用的是 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))

【问题讨论】:

  • 。 .据推测,@sID 未设置为您认为设置的值。
  • 是什么让你如此确定@sID = 904
  • 尝试在你的代码中直接在上面添加一行 @sID = 904。看看它是否运行。如果是这样,那么您的问题不在 SQL 中。
  • 打开配置文件或只获取您的应用程序生成的动态 SQL 并手动运行它以确保正在使用哪些值
  • @Amit 因为当我在 sID 字段的对话框中输入 904 时,它会显示所有其他列的正确信息,就好像我只在客户 ID 字段中输入了 904 一样。

标签: sql variables sql-server-2005 placeholder


【解决方案1】:

我尝试稍微清理一下您的查询,但它仍然可能有错误,因为我没有您的表格。但这样的事情可能会奏效:

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

【讨论】:

  • 这不起作用,因为采购成本、输入重量、产品成本和供应商 ID 是动态值,并不总是这些数字。这些数字因用户输入的内容而异。
  • 您应该将这些作为参数传递,这应该可以工作。
  • 传递什么作为参数?每个供应商 ID?我该怎么做呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多