sql - 识别何时在 SQL 查询或 PL/SQL 过程中执行函数

标签 sql oracle function plsql execution

有没有办法识别 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;

当然: Hiding all errors is bad practise

关于sql - 识别何时在 SQL 查询或 PL/SQL 过程中执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54569528/

相关文章:

android - 如何从 SQLite 中检索特定列并将其显示在 Android 中的 ListView 表单中?

sql - 如何优化此 LIKE JOIN 查询?

oracle - 无法在 macOS Big Sur 上安装 DBD::Oracle => 未找到依赖的 dylib '@rpath/libclntsh.dylib.19.1'

Java Hibernate EntityManager merge() 总是想为所有字段插入空值

python - 有什么办法可以恢复到原来的功能吗?

sql - 2 个错误 : The multi-part identifier "inserted.name" could not be bound

sql - 联合表 SQL 的更快方法

sql - 从 oracle 存储过程发送邮件,Oracle 11g

javascript - 输入 JavaScript 函数

javascript - python : How to count all objects in an array that match a condition?