Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

all! I want to know how wrote this code of SQL, I already trying but i get message error. I wrote two way different this SQL. See you below

SELECT *
  FROM tabela
 WHERE coluna IN (CASE WHEN 1 = :parametro THEN 1 WHEN 0 = :parametro THEN (2, 3) END)

And

SELECT *
  FROM tabela
 WHERE (CASE WHEN 1 = :parametro THEN coluna = 1 WHEN 0 = :parametro THEN coluna <> 1 END)

I tried to run but it get error. Please! you help to tick or fix this code.

Thank a lot.

Regards, Jeison Pereira

share|improve this question
    
It's nonsense. CASE WHEN can return only a scalar value - not a colletion like (10, 20). What exactly do you want to achieve? Look at funtion LNNVL maybe it does what you need. –  ibre5041 Aug 26 '13 at 16:28

2 Answers 2

One option is

SELECT *
  FROM tabela
 WHERE  'OK' =
         CASE WHEN   1 = :parametro and coluna = 1 then 'OK'
              WHEN   0 = :parametro and coluna in (2, 3) THEN 'OK'
         END
share|improve this answer

You can do it without a CASE

SELECT *
  FROM tabela
 WHERE (:parametro = 1 AND coluna = 1)
    OR (:parametro = 0 AND coluna <> 1 )
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.