在SQL查询中何时执行pl/sql函数以及何时在过程或PL/SQL匿名块中执行?(我不想传递任何参数进行手动识别)
我需要的主要原因是,当一个函数在SQL查询中执行时--我不想在失败时引发异常--我只需要返回一个NULL值就可以了。但是当在pl/sql脚本中执行相同的函数时,我想引发异常。
提前谢谢你。
发布于 2019-02-07 09:07:16
为什么不向函数中添加一个参数以指示是否抛出异常/返回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;https://stackoverflow.com/questions/54569528
复制相似问题