oracle函数中的Nullable返回类型

Ren*_*ene 5 oracle function plsqldeveloper

是否可以从oracle函数返回null?

我有以下oracle函数:

create or replace function vta.GetAmount(p_month NUMBER)
  return number is
  v_amount number(9);
begin
  select amount
    into v_amount
    from salary
   where salary.month = p_month;
  return v_amount;
end GetAmount;
Run Code Online (Sandbox Code Playgroud)

当select语句返回零行时,它会引发以下异常: ora-01403: no data found.

在这种情况下,我希望该函数返回null.

anu*_*pks 5

create or replace function vta.GetAmount(p_month NUMBER)
  return number is
  v_amount number(9);
begin
  select amount
    into v_amount
    from salary
   where salary.month = p_month;
  return v_amount;
  exception   -- code to handle no data
  when no_data_found then
  return null;
  end GetAmount;
Run Code Online (Sandbox Code Playgroud)