簡體   English   中英

確定函數何時在 SQL 查詢或 PL/SQL 過程中執行

[英]Identify when a function is executed in a SQL Query or in a PL/SQL procedure

有什么方法可以識別何時在 SQL 查詢中執行 pl/sql 函數以及何時在過程或 PL/SQL 匿名塊中執行? (我不想傳遞任何參數進行手動識別)

我需要它的主要原因是當在 SQL 查詢中執行函數時,我不想在失敗的情況下引發異常,我只會對返回值 NULL 感到滿意。 但是在 pl/sql 腳本中執行相同的函數時我想引發異常。

先感謝您。

為什么不給函數添加一個參數來指示是否拋出異常/返回空值? 當您調用該函數時,您可以選擇所需的行為。

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;

當然: 隱藏所有錯誤是不好的做法

好吧,環顧四周,我發現有一個 hack 可用:

在 SQL 中調用 PL/SQL 時,不會傳播異常NO_DATA_FOUND 因此,當從 SQL 調用它時,您可以使用它來“返回空值”而不是獲取異常:

create or replace function f 
       return int as
    begin
       raise no_data_found;
       return 1;
    end f;
    /

    select f from dual;
    F     
        null;
    declare
        v integer;
    begin
        v := f;
    end;

Error report -
ORA-01403: no data found

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM