1、定义:
将提前编译好的一段plsql,存放到数据库段,供其他程序员调用
2、语法:
create [or replace] procedure 过程名称(参数1 in|out 类型)
as|is
--声明一些变量
begin
2、案例:
(1)声明无返回值pro_add_sal存储过程 作用:给指定员工涨100工资,并打印涨前和涨后工资
create or replace procedure pro_add_sal(eno in number)
as
psal number;
begin
select sal into psal from emp where empno = eno;
dbms_output.put_line('涨前工资'||psal);
update emp set sal = sal + 100 where empno = eno;
commit;
dbms_output.put_line('涨后工资'||(psal+100));
end;
调用该存储过程:
begin
pro_add_sal(7788);
end;
(2)声明有返回值pro_emp_sal存储过程 作用:查询指定员工的年薪
create or replace procedure pro_emp_totalsal(eno number,totalsal out number)
as
begin
select sal*12+nvl(comm,0) into totalsal from emp where empno = eno;
end;
declare
total number;
begin
pro_emp_totalsal(7788,total);
dbms_output.put_line(total);
end;
说明:
一般来讲,存储过程没有返回值,但是我们可以利用out参数,在过程中实现返回多个值。
(3)声明输出参数为游标类型pro_emplist 作用:输入部门编号,将此部门下的所有员工信息输出
create or replace procedure pro_emplist(dno number,emplist out sys_refcursor)
as
begin
open emplist for select * from emp where deptno = dno;
end;
调用该存储过程:
declare
emplist sys_refcursor;
pemp emp%rowtype;
begin
pro_emplist(10,emplist);
loop
fetch emplist into pemp;
exit when emplist%notfound;
dbms_output.put_line(pemp.empno||pemp.ename);
end loop;
close emplist;
end;