首页 > 数据库技术 > 详细

oracle--函数--bai

时间:2017-01-07 00:02:20      阅读:245      评论:0      收藏:0      [点我收藏+]
--1 没有入参的函数.返回字符串
create or replace function get_time
return varchar2
as
 v_time varchar2(20);  --声明1个局部变量
begin
  select to_char(sysdate,‘hh24:mi:ss‘) into v_time from dual;  
  return v_time;
end;


--调用函数

select get_time() from dual;


--2 有入参的函数
--经典例子:获得大的数
create or replace function get_max_func
(
   i number,
   j number
)return  number
as
begin
   if(i>j) then
    return i;
      else
      return j;
   end if;
end;

select get_max_func(‘3‘,‘2‘)+get_max_func(‘1‘,‘2‘) as 结果 from dual;


--经典案例 。创建函数,获得工资最高的员工所在的部门名
 
create or replace function get_max_sal_dname_func
return varchar2
as
 v_dname varchar2(20);
begin
  select dname into v_dname from scott.dept where deptno  in
 ( 
  select distinct deptno from scott.emp where sal  
  = (select max(sal) from scott.emp)
 );
 return v_dname;
 exception
 when too_many_rows then
  return ‘超过1个部门‘;  --在异常的分支也要有返回值
end;  
 

--练习3
/*
创建并调用函数 get_comm_num_func ,入参为部门号
返回该部门 福利(comm)不为空的员工人数
提示:只要查询scott.emp表
*/
create or replace function get_comm_num_func 
(
  v_deptno scott.emp.deptno%type
)return number
as
 v_num number; --局部变量,用于返回
begin
   select count(1) into v_num from scott.emp where deptno = v_deptno 
   and comm is not null;
   return v_num;
end;

  

oracle--函数--bai

原文:http://www.cnblogs.com/ipetergo/p/6257560.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!