syntax error when using CASE in where clause

会有一股神秘感。 提交于 2019-12-12 13:27:59

问题


A similar question has been asked here

Anyway i get a syntax error i cannot figure.

This is my code:

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

Anyway Management studio underlines in red the 2 chars surrounded by ** above.

Why? Where is the syntax error?


回答1:


Give this a go;

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
)


来源:https://stackoverflow.com/questions/37815761/syntax-error-when-using-case-in-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!