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