函数

2.函数
  Oracle提供了一些内置函数,例如
字符串处理,日期处理等。开发者也
可以采用PL/SQL自定义函数。
 create or replace function 函数名
 (参数名 类型...)
 return 返回类型
 IS
  --变量定义
 begin
  --主处理区
 exception
  --异常处理区
 end;
 
 //编写一个mysign函数,模拟sign功能
 
select sign(10),sign(-5),sign(0)
from dual;
 
create or replace function mysign
(n number)
return number
is
begin
  if n>0 then
    return 1;
  elsif n<0 then
    return -1;
  else
    return 0;
  end if;
end;
 
select mysign(10),
       mysign(-5),
       mysign(0)
from dual;
 
编写函数注意事项:
 --格式要正确
 --必须有返回值
 --参数定义时用类型,不要指定大小
 --函数中不允许使用DML语句
 
案例:编写一个计算个税的函数,
传入工资值,返回纳税金额
create or replace function get_tax
(sal number)
return number
is
  v_tax number(7,2);--交税额
  v_tax_sal number(10);--交税工资
begin
  v_tax_sal := sal-3500;
  if v_tax_sal<=0 then
     v_tax := 0;
  elsif v_tax_sal<1500 then
     v_tax := v_tax_sal*0.03;
  elsif v_tax_sal<4500 then
     v_tax := v_tax_sal*0.1-105;
  elsif v_tax_sal<9000 then
     v_tax := v_tax_sal*0.2-555;
  else
     v_tax := v_tax_sal*0.25-1005;
  end if;
  return v_tax;
end;
原文地址:https://www.cnblogs.com/yunman/p/5497816.html