oracle函数

oracle函数创建及调用

CREATE [OR REPLACE] FUNCTION function_name
[ (argment [ { IN | OUT | IN OUT } ] Type ,
argment [ { IN | OUT | IN OUT } ] Type ]
RETURN return_type 
{ IS | AS }
<类型.变量的说明> 
BEGIN
FUNCTION_body
EXCEPTION
其它语句
END;

示例:

1) 在scott.emp表上创建一个函数deptsal,输入参数为员工编号,返回该员工所在部门的平均工资。

(1)创建函数

create or replace function deptsal(f_deptno in number) return number

as

avgsal number;

begin

select avg(sal) into avgsal from emp where deptno=f_deptno;

return avgsal;

end;

/

(2)调用函数

declare

avgsal number(10,2);

begin

avgsal:=deptsal(10);

DBMS_OUTPUT.PUT_LINE('平均工资:'||avgsal);

end;

/

(3)效果图

 

2) 在scott.emp表上创建一个函数deptnum,输入部门编号,返回该部门的员工人数,并调用该函数。

(1)创建函数

create or replace function deptnum(f_deptno in number) return int

as

dept_count int;

begin

select count(1) into dept_count from emp where deptno=f_deptno;

return dept_count;

end;

/

(2)调用函数

declare

dept_count int;

begin

dept_count:=deptnum(10);

DBMS_OUTPUT.PUT_LINE('部门人数:'||dept_count);

end;

/

(3)效果图

 

posted @ 2018-11-21 00:21  凭栏倚窗  阅读(215)  评论(0编辑  收藏  举报