简体   繁体   中英

CASE clause inside WHERE clause

can anyone tell me why I can not run this code in SQL Management studio? Thanks in advance.

SELECT  * FROM TabKontaktJednani
where
TabKontaktJednani.Typ IN (
CASE 'ERP'--(SELECT ALIAS FROM TabCisZam where LoginId = SUSER_NAME())
WHEN 'ERP'
THEN ('HeO','OST')
WHEN 'TO'
THEN ('SW','OST')
END)

The reason you can't run that code is that CASE expressions can only return one scalar value, not a list of values.

Your CASE expression returns a tuple. That is not allowed in SQL. But as MySQL supports a boolean data type, a CASE expression may result in a boolean:

SELECT *
FROM tabkontaktjednani
WHERE 
  CASE (SELECT alias FROM tabciszam WHERE loginid = suser_name())
    WHEN 'ERP' THEN typ IN ('HeO', 'OST')
    WHEN 'TO' THEN typ IN ('SW', 'OST')
  END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM