|
函数
|
|
创建函数
语法格式:
Create[or replace] function fun_name[ (parameter1[,parameter2]….)]return data_type is
|
[inner_variable]
Begin
|
Plsql_sentence;----sql语句
[exception]
[dowith_sentences;]-----异常处理代码
End[fun_name];
例如:
Create orreplace function get_avg_pay(num_deptno number) return number is
Num_avg_paynumber;----保存平均工资的内部变量
Begin
Selectavg(sal) into num_avg_pay from emp where deptno=num_deptno;---某个部门的平均工资
Return(round(num_avg_pay,2));---返回平均工资
Exception
Whenno_data_found then
Dbms_output.put_line(‘该部门编号不存在’);
Return(0);---返回工资0
End;
/
调用:
Declare
Avg_pay number;
Begin
Avg_pay:=get_avg_pay(10);
Dbms_output.put_line(‘平均工资是:’||avg_pay);
End;
/
删除函数
Dropfunction ‘函数名’;