PL/SQL基础之函数
/*
函数:可以有返回值得命名的PL/SQL子程序,必须有返回值
关键字:function return
*/
--函数1
create or replace function helloworld
return varchar2--指定返回类型,不能给定长度
as
v_hello varchar2(50);
begin
v_hello :='helloworld!';
return v_hello;--不可少的return
end;
--函数调用方式:
select helloworld from dual;
select helloworld() from dual;
declare
v_re varchar2(50);
begin
v_re := helloworld();--记住必须得接收函数的返回值哦
dbms_output.put_line(v_re);
end;
--函数2
create or replace function my_func(v_sal number)
return varchar2--一定注意,此处不能加';'
as
v_sql varchar2(200);
v_msg varchar2(50);
v_min number;
v_max number;
begin
v_sql :='select max(sal),min(sal) from emp';
execute immediate v_sql into v_max,v_min;
if v_sal > v_min and v_sal < v_max then
v_msg :='工资在正常范围内!';
else
v_msg :='不正常';--不写的话,在java中相当于初始化为''
end if;
return v_msg;
end;
--
调用函数2
select my_func(1500) from dual;
declare
v_sal number :='&薪水:';
v_result varchar2(50);
begin
v_result := my_func(v_sal);
dbms_output.put_line(v_result);
end;