oracle--函数--bai

--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;

  

转载于:https://www.cnblogs.com/ipetergo/p/6257560.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值