【发布时间】:2015-09-20 00:46:59
【问题描述】:
我相信这是解决问题的正确方法,但如果还有其他方法,请告诉我。
对于我的报告,用户输入开始日期、结束日期和供应商 ID。 在我的报告中,我还有四个受用户输入影响的变量。这就是问题所在,目前这些变量的日期和供应商 ID 是硬编码的,下面是一个示例:
(
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-04-24 00:00:00' } AND (ICPL.ProductionDate <= { ts '2015-04-24 00:00:00' } OR ICPL.ProductionDate Is Null))
AND ((1=1) AND AR_Customers.CustomerKey IN (124) )
) - @Cost
我知道我可以添加占位符,例如:
declare @d1 datetime,
@d2 datetime,
@sID int;
set @d1 = 'user's input start date';
set @d2 = 'user's input end date';
set @sID = 'user's selected supplier ID';
然后在变量的WHERE语句中填写@d1、@d2、@sID。
目前整个报告的 WHERE 语句如下所示:
WHERE
(IC_ProductLots.ProductionDate >= { ts '2015-04-24 00:00:00' }
AND (IC_ProductLots.ProductionDate <= { ts '2015-04-24 00:00:00' }
OR IC_ProductLots.ProductionDate IS NULL))
AND ((1=1)
AND AR_Customers.CustomerKey IN (123) )
因此,当用户单击“打印”时,会弹出一个对话框,他们选择开始日期、结束日期和供应商键,这就是填充主要 WHERE 语句的内容,但是我不确定如何使其相关到我的变量中的 WHERE 语句。
我使用的是 Microsoft SQL 2005,除了 SQL 之外没有其他语言。但是,这是一款具有预建功能和用户输入信息的 GUI。
如果您需要更多信息,请告诉我。
完整代码:
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);
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 ((1=1) AND AR_Customers.CustomerKey IN (124))
);
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)
;
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]
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))
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
, IC_ProductLots.UnitOfMeasure_Alt
, IC_ProductLots.UnitOfMeasure_Stk
, [ICP].UnitCost
, IC_ProductLots.ProductionDate
, AR_Customers.CustomerKey
ORDER BY
CAST([AR_Customers].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([AR_Customers].[Name] AS NVARCHAR(40))
** 包围的 WHERE 代码是由用户输入填充的代码,其他 WHERE 代码是硬编码的,但我需要将硬编码的代码更改为主要的 WHERE 代码。
【问题讨论】:
-
您是否使用 SSRS 进行报告?
-
@LDMJoe 我不相信,我该如何解决这个问题?我运行的软件叫Canopy,aspen-systems.com/solutions/canopy-core-system/overview
-
运气好吗?只是好奇你是否发现出了什么问题?
-
@Dasman 是的,我最终没有制作数学函数变量,只是将它们作为标准表达式,所以我不必担心 WHERE 语句。
标签: sql variables sql-server-2005 placeholder