我正在处理一个存储过程,该存储过程用于根据用户输入的条件过滤网格。在可能的标准中,他们可能会选择查看在某个开始日期和结束日期之间完成的交易。我将4个输入参数传递给proc - @ClientKey,请求信息的客户端@FilterBy(这是ASP.NET中下拉列表中的选定值,实际上告诉proc用户是否选择按名称进行过滤,地址,日期等。在这种情况下,日期的@FilterBy值是5),@ Value1,在这个例子中是开始日期,@ Value2,在这个例子中是结束日期。
在伪代码中,我想要的是:
SELECT ABunchOfColumns FROM SomeJoinedTables 某些标准 AND CASE @FilterBy 5那么d.TransactionTime> = CAST(@ Value1 AS Date)和d.TransactionTime< = CAST(@ Value2 AS Date)
以下是完整查询。我希望那个比我更精通SQL的人可以理清我想做的事情并提供解决方案。
提前致谢!
@ClientKey int,
@FilterBy int,
@Value1 varchar(150),
@Value2 varchar(150)
AS BEGIN
SELECT d.pKey AS PaymentKey, d.CaseKey, d.InvoiceID, d.AuthorizationCode, d.TransactionID, d.PaymentType, d.Amount, d.ExpirationDate, d.CardType, d.BankName, d.AccountNumber,
d.AccountType, c.Name, c.Address1, c.City, c.State, c.Zip, cs.PAmount, cs.TranCode, cs.TranDate, cs.[Desc] AS PaymentDescription, cc.[Desc] AS ChargeCodeDescription,
d.TransactionTime, a.Name AS AssociationName, d.PaymentType, c.ClientPaymentID
FROM DebtorPayment d INNER JOIN Cases c
ON d.CaseKey = c.pKey
AND d.ClientKey = c.ClientKey
INNER JOIN CaseSumm cs
ON d.CaseKey = cs.CaseKey
AND d.pKey = cs.Batch
AND d.ClientKey = cs.ClientKey
INNER JOIN ChargeCodes cc
ON c.ClientKey = cc.ClientKey
AND cs.TranCode = cc.RefNum
INNER JOIN AssnCtrl a
ON c.AssnKey = a.pKey
WHERE c.ClientKey = @ClientKey
AND d.AmountAllocated > 0
AND d.TransactionStatus = 'Successful'
AND c.Address1 LIKE CASE
WHEN @FilterBy = 1 THEN '%' + @Value1 + '%'
ELSE c.Address1
END
AND d.Amount = CASE
WHEN @FilterBy = 2 THEN @Value1
ELSE d.Amount
END
AND a.pKey = CASE
WHEN @FilterBy = 3 THEN CAST(@Value1 AS INT)
ELSE a.pKey
END
AND c.ClientPaymentID = CASE
WHEN @FilterBy = 4 THEN @Value1
ELSE c.ClientPaymentID
END
<ProblemArea>
AND CASE @FilterBy
WHEN 5 THEN d.TransactionTime >= CAST(@Value1 AS Date) AND d.TransactionTime <= CAST(@Value2 AS Date)
END
</ProblemArea>
AND c.LName LIKE CASE
WHEN @FilterBy = 6 THEN '%' + @Value1 + '%'
ELSE c.LName
END
ORDER BY d.TransactionTime DESC
END
答案 0 :(得分:2)
CASE
WHEN @FilterBy = 5 AND d.TransactionTime >= CAST(@Value1 AS DATE)
AND d.TransactionTime <= CAST(@Value2 AS DATE) THEN 1
ELSE 0
END = 1
答案 1 :(得分:0)
你的意思是如下吗? 您可能会发现此link也很有用。 没有测试过代码,只提出想法 - 编写这样的代码对我来说总是感觉“错误” - 也许有人可以提供更好的。
CREATE PROC MyProc
@ClientKey int,
@FilterBy int,
@Value1 varchar(150),
@Value2 varchar(150)
-- NB WITH RECOMPILE, see link (above)
WITH RECOMPILE AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM ...
WHERE
Col1 = CASE
WHEN @FilterBy = 1 THEN @Value1
ELSE Col1
END
AND
1 = CASE
WHEN @FilterBy = 2 AND Col2 > @Value1 AND Col3 < @Value2 THEN 1
WHEN @FilterBy = 2 AND NOT (Col2 > @Value1 AND Col3 < @Value2) THEN 0
ELSE 1
END
END