更新时间:2023-02-07 09:14:11
首先,CASE
语句必须是表达式的一部分,而不是表达式本身.
First off, the CASE
statement must be part of the expression, not the expression itself.
换句话说,你可以:
WHERE co.DTEntered = CASE
WHEN LEN('blah') = 0
THEN co.DTEntered
ELSE '2011-01-01'
END
但它不会像你写的那样工作,例如:
But it won't work the way you have written them eg:
WHERE
CASE LEN('TestPerson')
WHEN 0 THEN co.personentered = co.personentered
ELSE co.personentered LIKE '%TestPerson'
END
使用这样的组合 OR 语句可能会更好:
You may have better luck using combined OR statements like this:
WHERE (
(LEN('TestPerson') = 0
AND co.personentered = co.personentered
)
OR
(LEN('TestPerson') <> 0
AND co.personentered LIKE '%TestPerson')
)
虽然,无论哪种方式,我都不确定您会获得多棒的查询计划.WHERE
子句中的这些类型的恶作剧通常会阻止查询优化器使用索引.
Although, either way I'm not sure how great of a query plan you'll get. These types of shenanigans in a WHERE
clause will often prevent the query optimizer from utilizing indexes.