我相信这是解决问题的正确方法,但如果有其他方法请告诉我。
对于我的报告,用户输入开始日期,结束日期和供应商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) )
因此,当用户点击&#39;打印&#39;弹出一个对话框,它们选择开始日期,结束日期和供应商密钥,这就是填充主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代码。
答案 0 :(得分:0)
WHERE
(IC_ProductLots.ProductionDate >= @d1
AND (IC_ProductLots.ProductionDate <= @d2
OR IC_ProductLots.ProductionDate IS NULL))
AND ( AR_Customers.CustomerKey =@sID )
听起来你需要做的就是将这些变量放在你的where子句而不是你正在使用的文字中。
答案 1 :(得分:0)
只要在Where子句中声明变量BEFORE,就可以将变量放入where子句中:
来自来源的示例:
DECLARE @EmpIDVar INT
SET @EmpIDVar = 1234
SELECT *
FROM Employees
WHERE EmployeeID = @EmpIDVar
来源:https://technet.microsoft.com/en-us/library/aa212846(v=sql.80).aspx
祝你好运!