3

Is it possible to have conditional where clause, based on declared variable?

Please note: my query is much more complicated than this, I am just using this example to simplify things.

Something like:

DECLARE @ITEST INT = 1

SELECT NAME, LNAME, CADDRESS
FROM JEEVEN
WHERE
CASE WHEN @ITEST = 1 THEN
(
    (EVEN_KEY > 5 AND EVEN_KEY < 10)
)
CASE WHEN @TEST = 2 THEN
(
    (EVEN_KEY > 20 AND EVEN_KEY < 30)
) 
3

1 Answer 1

3

You are looking for this

DECLARE @ITEST INT = 1

SELECT NAME, LNAME, CADDRESS
FROM JEEVEN
WHERE (@ITEST = 1 AND EVEN_KEY > 5 AND EVEN_KEY < 10)
   OR (@ITEST = 2 AND EVEN_KEY > 20 AND EVEN_KEY < 30)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.