[英]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.我有几个几乎相同的存储过程,但在 WHERE 子句中有一些不同的 AND 部分。
Based on a variable deptname
, I want to add additional AND/OR conditions to my already existing WHERE clause.基于变量
deptname
,我想向我已经存在的 WHERE 子句添加额外的 AND/OR 条件。 So kind of like IF/CASE WHEN on the part that is different.所以有点像 IF/CASE WHEN 在不同的部分。
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:要结合
WHERE
子句的最后一部分(如果我正确理解您的注释代码),您可以执行以下操作:
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).通常,如果您想实际更改
SELECT
语句中的值,您将使用WHERE
子句过滤掉不必要的数据行和CASE
语句(我很少看到SELECT
子句之外的CASE
语句,除非它正在执行类似复杂排序的操作) )。
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:在这种情况下,您可以使用
CASE
表达式,重要的是确保您有一个ELSE
子句,以确保如果@deptname
不是具有额外条件的两个值之一,则表达式仍然为真:
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. 这是以这种方式使用的
CASE
表达式的简单演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.