类似的问题已被问到here https://stackoverflow.com/questions/22001787/using-if-condition-in-where-clause
无论如何,我遇到了语法错误,我无法弄清楚。
这是我的代码:
declare @MyParameter integer
se @MyParameter = Set At Runtime (could be -1 or any value >=1)
SELECT manyfields FROM manyjoinedtables
where
case when @MyParameter> -1 then
(FIELD1 **=** @MyParameter AND ANOTHERFIELD = Value**)**
end -- note: in case @MyParameter = -1 i do not want to add where condition
不管怎样,管理工作室用红色强调了上面被 ** 包围的 2 个字符。
为什么?语法错误在哪里?
尝试一下吧;
DECLARE @MyParameter INT
SET @MyParameter = Set At Runtime (could be -1 or any value >=1)
SELECT manyfields
FROM manyjoinedtables
WHERE
@MyParameter <= -1
OR
(
@MyParameter > -1
AND FIELD1 = MyParameter
AND AnotherField = Value
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)