一、相关概念
1、存储过程和存储函数
~指存储在数据库中供所有用户程序调用的子程序
~存储过程和存储函数的相同点:完成特定功能的程序
~存储过程和存储函数区别:是否用return语句返回值
2、创建和使用存储过程
~用CREATE PROCEDURE命令建立存储过程和存储函数
~语法:
create [or replace] PROCEDURE 过程名(参数列表) AS PLSQL子程序体;
~打印hello world存储过程
create or replace procedure sayhelloworld as --说明部分 begin dbms_output.put_line('hello world'); end; /
调用存储过程方法:
exec sayhelloworld(); begin sayhelloworld(); end; /
~创建和使用带参数存储过程
为指定的员工涨100元的工资,并且打印涨前和涨后的工资
create or procedure raisesalary(eno in number) --desc dbms_output as psal emp.sal%type; begin select sal into psal from emp where empno=eno; update emp set sal=sal+100 where empno=eno; --注意:一般不在存储过程或者存储函数中,commit和rollback. dbms_output.put_line('涨前:'||psal|| '涨后'||(psal+100)||); end; /