gpt4 book ai didi

Oracle 和 where 子句中的可选存储过程参数

转载 作者:行者123 更新时间:2023-12-01 22:48:07 25 4
gpt4 key购买 nike

我有一个带有这样的可选参数的存储过程

create or replace 
PROCEDURE "my_stored_procedure" (param1 int, param2 int default null)
IS

BEGIN

[select some fields from some tables]
...

我需要一个在默认参数上带有 if 的 where 子句(如果已设置),但我什至不知道这是否可能...

类似于

where param1=123 and (if param2 is not null then some_value != param2)

select 子句相当长且复杂,所以我更喜欢有一个“灵活的”WHERE 而不是像这样的结构

if param2 is not null then
[long and complex select] where param1=123 and some_value != param2
else
[long and complex select] where param1=123

这可能吗?

最佳答案

在这种情况下你可以这样做:

where param1=123 and (param2 is null or param2 != some_value)

如果 param2 不为 null - 则仅当 param2 != some_value 时才为 true - 正如预期的
如果 param2 为 null - 那么无论 some_value 是什么,它都会返回 true

关于Oracle 和 where 子句中的可选存储过程参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com