自定义函数

*自定义函数

1:实现一个输出系统时间的函数


2:实现为空处理nvl,与nvl2功能函数
create or replace function mynvl (p1 nvarchar2,p2 nvarchar2)
return nvarchar2
as
begin
if (p1 is null or p1='') then
return p2;
else
return p1;
end if;
end;

Nvl2:
create or replace function my Nvl2 (p1 nvarchar2,p2 nvarchar2,p3 nvarchar2)
return nvarchar2
as
begin
if (p1 is null or p1='') then
return p3;
else
return p2;
end if;
end;








3:函数 实现 1 ,1, 2, 3,5, 8
create or replace function mydata(p1 int)
return int

as

begin
if p1=1 or p1=2 then
return 1;
else
return mydata(p1-1)+mydata(p1-2);
end if;

end;


4:查询指定用户名工资的函数
select (
case
when sal=800 then '土豪'
end
),ename from scott.emp

create or replace function mydata(p1 nvarchar2)
return int
as
str int;
begin
select sal into str from scott.emp where ename=p1;
return str;
end;
5:实现的四舍五入函数
create or replace function myround (p number,poi int default 0)
return number
is
pt int ;
begin
pt:=substr(p,length(trunc(p))+2+poi,1);

if pt >=5 then
return trunc(p+1/power(10,poi),poi);
else
return trunc(p,poi);
end if;


return pt;
end;

select myround(15.364,2) from dual


猜你喜欢

转载自blog.csdn.net/zhangxin97/article/details/78792366