有没有办法识别 pl/sql 函数何时在 SQL 查询中执行以及何时在过程或 PL/SQL 匿名 block 中执行? (我不想传递任何参数进行手动识别)
我需要它的主要原因是,当在 SQL 查询中执行函数时,我不想在失败时引发异常,我只需要返回值 NULL 就满足了。但是在 pl/sql 脚本中执行相同的函数时我想引发异常。
提前谢谢您。
最佳答案
为什么不在函数中添加一个参数来指示是否抛出异常/返回null?当您调用该函数时,您可以选择您需要的行为。
create or replace function do_something(p_parameter1 < some_type >
,p_raise_exception varchar2 default 'Y') return < sometype > is
begin
--.. calculating .. .
return result;
exception
when others then
if p_raise_exception is 'Y'
then
raise;
else
return null;
end if;
end;
或者 owa_util 似乎提供了一些您可以使用的功能。
create or replace function do_something(p_parameter1 < some_type >) return < sometype > is
l_owner varchar2(100);
l_name varchar2(100);
l_lineno number;
l_caller_t varchar2(100);
begin
--.. calculating .. .
return result;
exception
when others then
owa_util.who_called_me(l_owner, l_name, l_lineno, l_caller_t)
-- who called me result seems empty when called from sql.
if l_owner is not null
then
raise;
else
return null;
end if;
end;
关于sql - 识别何时在 SQL 查询或 PL/SQL 过程中执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54569528/