Oracle存储过程的增、删、改、查的简单举例(精) 下载本文

Oracle存储过程的增、删、改、查: Packages: create or replace package emp_pags is type emp_cour is ref cursor;--定义游标类型的变量emp_cour-- --添加员工-- function saveEmp(e_ename emp.ename%TYPE, e_job emp.job%TYPE, e_sal emp.sal%TYPE, e_deptno emp.deptno%TYPE return integer;--定义返回类型-- --修改员工信息-- function updateEmp(e_empno emp.empno%TYPE, e_ename emp.ename%TYPE, e_job emp.job%TYPE, e_sal emp.sal%TYPE, e_deptno emp.deptno%TYPE return integer;--定义返回类型-- --删除员工-- function deleteEmp(e_empno emp.empno%TYPE return integer ; --查询全部-- procedure queryAll(emps out emp_cour;--定义一个输出类型的参数emps-- --查询单条-- procedure queryById(e_empno in number,emps out emp_cour;--定义-- --两表查询-- procedure queryJoin(empJoin out emp_cour; end emp_pags; Package bodies: create or replace package body emp_pags is --添加员工-- function saveEmp(e_ename emp.ename%TYPE, e_job emp.job%TYPE, e_sal emp.sal%TYPE, e_deptno

emp.deptno%TYPE return integer is begin insert into emp (empno,ename,job,sal,deptno values (emp_sequence.nextval,e_ename,e_job,e_sal,e_deptno; commit; return 1; exception when others then return 0; end saveEmp; --修改员工信息-- function updateEmp(e_empno emp.empno%TYPE, e_ename emp.ename%TYPE, e_job emp.job%TYPE, e_sal emp.sal%TYPE, e_deptno emp.deptno%TYPE return integer is begin update emp set ename = e_ename, job = e_job, sal = e_sal, deptno = e_deptno where empno = e_empno; commit; return 1; exception when others then return 0; end updateEmp; --删除员工-- function deleteEmp(e_empno emp.empno%TYPE return integer is begin delete from emp where empno = e_empno; commit; return 1;

exception when others then return 0; end deleteEmp; --查询全部-- procedure queryAll(emps out emp_cour is begin open emps for --打开游标-- select

empno,ename,job,sal,deptno from emp; end queryAll; --查询单条-- 定义两个参数,第一个输入,第二个输出 procedure queryById(e_empno in number,emps out emp_cour is begin open emps for--打开游标-- select empno,ename,job,sal,deptno from emp where empno = e_empno; end queryById; --量表查询-- procedure queryJoin(empJoin out

emp_couris begin open empJoin for select e.empno,e.ename,e.job,e.sal,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno; end queryJoin; end emp_pags;