Oracle 自定义过程与函数

来源:互联网 发布:网络霸气情侣名 编辑:程序博客网 时间:2024/06/17 05:42

Oracle 自定义过程与函数

1、自定义过程

1、过程定义

create or replace procedure 名字 ([参数1] [in|out] [type])[is|as]变量声明部分;begin执行部分;exception异常部分;end;

2、实例

1、写一个过程,删除emp表中指定员工记录

 create or replace procedure p_delete(empno_ in number) is begin delete from emp where empno = empno_; end;

2、写一个过程,用于计算emp表中指定部门的工资总和,并统计其中的员工数量。

//采用变量输出的方式完成,也可以采用如上方式create or replace procedure p_total_emp(dept_no in number,sum_sal out number,count_emp out number)isbeginselect count(*) into count_emp from emp group by deptno having deptno = dept_no;select sum(sal) into sum_sal from emp group by deptno having deptno = dept_no;end;//声明两个变量SQL> var count_emp number;SQL> var sum_sal number;//执行过程exec p_total_emp(10,:sum_sal,:count_emp);//打印变量当前值SQL> print sum_sal count_emp;   SUM_SAL----------      8750 COUNT_EMP----------         3

2、自定义函数

1、函数定义

create or replace function [f_name] ([参数1] [in|out] [type])return [return_type][is|as]变量声明begin函数体exception ……return %type;end;

2、实例

1、写一个函数,用于计算emp表中指定部门的工资总和,并统计其中的员工数量。

//函数体create or replace function f_total_emp(dept_no in number,sum_sal out number,count_emp out number)return numberisbeginselect sum(sal) into sum_sal from emp group by deptno having deptno = dept_no;select count(*) into count_emp from emp group by deptno having deptno = dept_no;return 1;end;//变量声明SQL> var sum_sal number;SQL> var count_emp number;SQL> var result number;//执行函数SQL> exec :result := f_total_emp(10,:sum_sal,:count_emp);PL/SQL 过程已成功完成。//打印结果SQL> print sum_sal count_emp;   SUM_SAL----------      8750 COUNT_EMP----------         3

总结:

1、过程的使用

2、函数的使用

3、系统函数的实现

例如:
select sum(sal) from emp;
关于sum函数传参问题,返回值问题
以及emp表的结果集的传递问题。

0 0
原创粉丝点击