Arbaaz - 1 year ago 39

SQL Question

I want to select such that if

`@reporteetype`

`'immediate'`

`select a, b, c, level`

from table1

where col1 ='x' and

CASE @reporteetype WHEN 'immediate' THEN level = '2'

WHEN 'other' THEN level > 2

else 1 = 1

END

This is a simplified version of my scenario. I do not want to use dynamic query. I want to know if this could be done using case?

Answer

You can do it using combinations of `AND`

and `OR`

conditions:

```
SELECT
a, b, c, level
FROM table1
WHERE
col1 = 'x'
AND (
(@reporteetype = 'immediate' AND level = 2)
OR (@reporteetype = 'other' AND level > 2)
)
```