设为首页 加入收藏

TOP

Oracle实践--PL/SQL基础之函数
2015-07-24 11:49:05 来源: 作者: 【 】 浏览:4
Tags:Oracle 实践 --PL/SQL 基础 函数

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;
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇oraclecluster物理配置要求及限制 下一篇Oracle模糊查询方法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)