Is it possible to use IN
in CASE..THEN
?
WHERE
record.field =
CASE
WHEN @flag = 1 THEN a
WHEN @flag = 2 THEN IN (b, c)
END
Or how to rew开发者_开发知识库rite such condition?
Sorry, no:
WHERE
(@flag = 1 AND record.field = a)
OR
(@flag = 2 AND record.field IN (b, c))
try:
WHERE (@flag = 1 AND record.field = a )
OR (@flag = 2 AND record.field IN (b, c))
it might be better to try to JOIN in the value and use an index:
DECLARE @Table (ValueOf int)
INSERT INTO @Table ((ValueOf)
SELECT a WHERE @flag = 1
UNION SELECT b WHERE @flag = 2
UNION SELECT c WHERE @flag = 2
SELECT
....
FROM ... x
INNER JOIN @Table t ON x...=t.ValueOf
精彩评论