我有一个由其他人创建的存储过程。在WHERE
子句中,有一个CASE表达式,我很难理解:
DECLARE
@DateFrom datetime = '01-01-2016',
@DateTo datetime = '12-31-2016'
@EffDateFrom datetime = NULL,
@EffDateTo datetime = NULL,
/* SOME SELECT statement here */
WHERE
CASE WHEN @EffDateFrom IS NULL THEN 1
ELSE CASE WHEN dateDiff(d, '01-01-2016', dbo.tblQuotes.EffectiveDate) >= 0
AND dateDiff(d, EOMONTH (GETDATE()), dbo.tblQuotes.EffectiveDate) <= 0 Then 1
else 0
end
END = 1
--------------/* This is where I am confused */--------------------------------
AND CASE WHEN @DateFrom IS NULL THEN 1 ELSE
CASE WHEN INV.InvoiceDate > INV.EffectiveDate THEN
CASE WHEN dateDiff(d, '01-01-2016', INV.InvoiceDate) >= 0
AND dateDiff(d, '12-31-2016', INV.InvoiceDate) <= 0 Then 1 else 0 end
ELSE
CASE WHEN dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0
AND dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0 Then 1 else 0 end
END
END = 1
所以我们在这里说的是:
当参数@DateFrom为null时,使用参数@EffDateFrom,是吗?
但如果它不为空,那么
1.检查InvoiceDate是否大于EffectiveDate,如果是 - 那么
2.如果01-01-2016
和InvoiceDate之间的天数小于或等于0,那么检查AND
和InvoiceDate之间的天数是否大于或等于0 12-31-2016
!!什么是1?意思是记录有效吗?记录会在表格中吗?正确的吗?
ELSE 0
意味着它不会选择记录,对吗?
之后我很困惑。 更新了解(如果正确):
CASE WHEN @DateFrom IS NULL THEN 1 ELSE
CASE WHEN INV.InvoiceDate > INV.EffectiveDate THEN
-----------------------------/*then check the below conditions and if its 1 then display the record if its 0 then do NOT */
CASE WHEN dateDiff(d, '01-01-2016', INV.InvoiceDate) >= 0
AND dateDiff(d, '12-31-2016', INV.InvoiceDate) <= 0 THEN 1 ELSE 0
END
----------------------------/* and this statement will only be working if parameter @EffDateFrom is not null. Correct? */
ELSE
CASE WHEN dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0
AND dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0 Then 1 else 0 end
END
END = 1
答案 0 :(得分:1)
这是(你问过的部分)的逻辑:
IF (@DateFrom IS NULL)
OR ((INV.InvoiceDate > INV.EffectiveDate) AND (dateDiff(d, '01-01-2016', INV.InvoiceDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.InvoiceDate) <= 0) )
OR ((INV.InvoiceDate <= INV.EffectiveDate) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
OR ((INV.InvoiceDate IS NULL) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
OR ((INV.EffectiveDate IS NULL) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
THEN TRUE
ELSE FALSE
整个陈述:
IF (@EffDateFrom IS NULL)
OR ((dateDiff(d, '01-01-2016', dbo.tblQuotes.EffectiveDate) >= 0 ) AND (dateDiff(d, EOMONTH (GETDATE()), dbo.tblQuotes.EffectiveDate) <= 0) )
THEN TRUE
ELSE FALSE
AND
IF (@DateFrom IS NULL)
OR ((INV.InvoiceDate > INV.EffectiveDate) AND (dateDiff(d, '01-01-2016', INV.InvoiceDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.InvoiceDate) <= 0) )
OR ((INV.InvoiceDate <= INV.EffectiveDate) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
OR ((INV.InvoiceDate IS NULL) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
OR ((INV.EffectiveDate IS NULL) AND (dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0) AND (dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0))
THEN TRUE
ELSE FALSE
答案 1 :(得分:1)
您不知道的部分可以在不使用case语句的情况下解释为以下内容:
WHERE
@DateFrom IS NULL
OR ( @DateFrom IS NOT NULL
AND INV.InvoiceDate > INV.EffectiveDate
AND dateDiff(d, '01-01-2016', INV.InvoiceDate) >= 0
AND dateDiff(d, '12-31-2016', INV.InvoiceDate) <= 0
)
OR ( @DateFrom IS NOT NULL
AND dateDiff(d, '01-01-2016', INV.EffectiveDate) >= 0
AND dateDiff(d, '12-31-2016', INV.EffectiveDate) <= 0
)