问题
I'm trying to create a searched case sql statement based on a number of boolean expressions.
For example
select CASE a = b OR c = d OR e = f OR g = h
THEN 'x' ELSE 'y' END
from table_name
I keep getting the following error: Incorrect syntax near '='.
Am I doing something that's inherently wrong/illegal in sql, or is this something I can fix? If it is fixable, how can I do it?
Thanks!
回答1:
select CASE when ( a = b OR c = d OR e = f OR g = h )
THEN 'x' ELSE 'y' END
from table_name
The format is CASE..WHEN..THEN..ELSE..END. You were just missing the WHEN.
回答2:
I feel really stupid. I'm missing a when after the case...
Should be
select CASE when a = b OR c = d OR e = f OR g = h
THEN 'x' ELSE 'y' END
from table_name
Then it works...
回答3:
You missed WHEN keyword
SELECT
CASE WHEN a = b OR c = d OR e = f OR g = h
THEN 'x'
ELSE 'y'
END
FROM table_name
来源:https://stackoverflow.com/questions/6877292/can-i-have-multiple-boolean-expressions-in-a-searched-case-sql-statement