oracle函数中的Nullable返回类型

前端之家收集整理的这篇文章主要介绍了oracle函数中的Nullable返回类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以从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;

当select语句返回零行时,它会引发以下异常:
ora-01403:未找到数据.

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

解决方法

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;

猜你在找的Oracle相关文章