我有一个需要在WHERE子句中带有或不带有条件/谓词执行的SQL。如果将某些值传递到PL / SQL存储过程中,则该谓词恰好是一个子查询,需要将其包含在ansible_become_user=root
子句中:
WHERE
我尝试使用CASE / WHEN / THEN / ELSE,但无法使用'H'选项,例如:
SELECT /* several columns */
FROM X JOIN Y ON X.A = Y.A
WHERE COL1 = 'a'
AND COL2 IS NULL
AND /* Several other predicates */
AND /* This condition should be included only if a value of parameter is 'G'. If it is 'H', it should NOT consider this condition at all */
NOT EXISTS (SELECT 1 FROM Z WHERE Z.A = X.A)
答案 0 :(得分:1)
您可以这样做:
AND
(P_SP_PARAM <> 'G' OR
NOT EXISTS (SELECT 1 FROM Z WHERE Z.A = X.A)
)
如果您特别希望它H
,那么:
AND
(P_SP_PARAM = 'H' OR
NOT EXISTS (SELECT 1 FROM Z WHERE Z.A = X.A)
)
这两个版本均假设P_SP_PARAM
不是NULL
。逻辑上可以很容易地修改以考虑到这一点。