简体   繁体   中英

Using case statement or IF inside a WHERE clause

I have several stored procedures that are almost identical but have some different AND parts inside a WHERE clause.

Based on a variable deptname , I want to add additional AND/OR conditions to my already existing WHERE clause. So kind of like IF/CASE WHEN on the part that is different.

Think about it as string concatenation

query_string = 'WHERE a= XYZ AND B= 123"
if deptname = a:  query_string + "AND additional conditions for dept a"
else if deptname = b:query_string + "AND additional conditions for dept b"

What is the appropriate way to use a variable?

here is some pseudo code of what I am trying to do

SELECT 
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
--shared parts
personcode = 'C' 
AND
persondeptcode = 'MAJ'
--- NOW the different part
IF @deptname = "deptA"
AND 
(
PROGRAM_LDESCR IN 
(
    'prog1',
    'prog2',
    'prog3'
)
OR
aprogram IN ('aprogram1') 
OR 
(aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))
); 
--- THIS IS A DIFFERENT DEPT SO WE HAVE DIFFERENT AND PART 
ELSE IF @deptname = "deptB"

(
PROGRAM_LDESCR IN 
(
    '1234'
)
OR
aprogram IN ('a1234') 
);

You seem to want something like:

AND
(@deptname = 'dept123' AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234')) OR
 @deptname <> 'dept123'
)

To combine the last part of the WHERE clause (if I'm understanding your commented-code correctly), you could do something like the following:

SELECT 
    personID AS pid,
    personcode,
    persondeptcode,
    more_fields AS fields
FROM
    TABLE_XYZ
WHERE
    personcode = 'C' 
    AND persondeptcode = 'MAJ'
    AND (
        (@deptname="deptA" AND (PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3') OR aprogram IN ('aprogram1') OR (aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))))
        OR
        (@deptname="deptB" AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234'))
    )

Normally you would use the WHERE clause to filter out unnecessary rows of data and a CASE statement if you wanted to actually change the value in the SELECT statement (I rarely see CASE statements outside a SELECT clause, unless it is doing something like a complex sort).

You can use a CASE expression in this case, the important thing is to make sure you have an ELSE clause to ensure the expression remains true if @deptname is not one of the two values with extra conditions:

WHERE personcode = 'C' 
  AND persondeptcode = 'MAJ'
  AND (CASE @deptname 
       WHEN "deptA" THEN PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3')
                      OR aprogram IN ('aprogram1') 
                      OR aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123')
       WHEN "deptB" THEN PROGRAM_LDESCR IN ('1234')
                      OR aprogram IN ('a1234')
       ELSE 1
       END)

Here is a simple demo of a CASE expression used in this fashion.

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