我有一个参数,可以是3个可能值0、2或null中的1个(这些是枚举的值0处于挂起状态,1处于审核中,2处于关闭状态)。如果为空,则应检索所有状态,但如果为2,则应检索所有已关闭的状态;如果为0,则应检索挂起或正在审阅的状态。最后一个意味着我要检索状态为0或1的记录。我有以下代码到目前为止,并尝试与案件开关,但它不工作。
SELECT * FROM PurchaseOrder
WHERE (CreationDate >= @StartDate AND CreationDate <= @EndDate)
AND (@POStatus IS NULL OR [Status] = @POStatus)
AND (@POStatus IS NULL OR [Status] = CASE @POStatus WHEN 0 THEN 1 END)
AND (@PurchaseOrderIdSearch IS NULL OR PurchaseOrderId LIKE @PurchaseOrderIdSearch)
AND EmployeeId = @EmployeeId
ORDER BY CreationDate DESC, PurchaseOrderId DESC
我通过添加if语句解决了这个问题,我在寻找一种更简单的方法来避免重复,现在是这样的:
IF @POStatus = 0
BEGIN
SELECT * FROM PurchaseOrder
WHERE (CreationDate >= @StartDate AND CreationDate <= @EndDate)
AND (([Status] = @POStatus)
OR ([Status] = 1))
AND (@PurchaseOrderIdSearch IS NULL OR PurchaseOrderId LIKE @PurchaseOrderIdSearch)
AND EmployeeId = @EmployeeId
ORDER BY CreationDate DESC, PurchaseOrderId DESC
END
ELSE
BEGIN
SELECT * FROM PurchaseOrder
WHERE (CreationDate >= @StartDate AND CreationDate <= @EndDate)
AND (@POStatus IS NULL OR [Status] = @POStatus)
AND (@PurchaseOrderIdSearch IS NULL OR PurchaseOrderId LIKE @PurchaseOrderIdSearch)
AND EmployeeId = @EmployeeId
ORDER BY CreationDate DESC, PurchaseOrderId DESC
END
1条答案
按热度按时间ocebsuys1#
我想你是在找
这里有一个db<>小提琴来看看它是如何工作的。
更新:
好像你在找
这是一把小提琴