一、存储过程
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的 SQL 语句集,经
编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来
执行它。
存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
存储过程:存储过程就是提前已经编译好的一段pl/sql语言,放置在数据库端
可以直接被调用,这一段pl/sql一般都是固定步骤的业务。
创建过程语法:
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
AS
begin
PLSQL 子程序体;
End;
或者:
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
is
begin
PLSQL 子程序体;
End 过程名;
- 创建一个输出 helloword 的存储过程
- 范例 2:给指定的员工涨 100 工资
分析:我们需要使用带有参数的存储过程
create or replace procedure p1(eno emp.empno%type)
is
begin
update emp set sal=sal+100 where empno = eno;
commit;
end;
select * from emp where empno = 7788;
----测试p1
declare
begin
p1(7788);
end;
二、存储函数
存储过程和存储函数的区别:
一般来讲,过程和函数的区别在于函数可以有一个返回值;而过程没有返回值。
但过程和函数都可以通过 out 指定一个或多个输出参数。我们可以利用 out 参数,在过程和函数中实现返回多个值。
- 范例:使用存储函数来查询指定员工的年薪
使用存储过程来替换上面的例子
create or replace procedure empincomep(eno in emp.empno%type,
income out number) is
psal emp.sal%type;
pcomm emp.comm%type;
begin
select t.sal, t.comm into psal, pcomm from emp t where t.empno = eno;
income := psal*12+nvl(pcomm,0);
end empincomep;
调用:
declare
income number;
begin
empincomep(7369, income);
dbms_output.put_line(income);
end;
----通过存储函数实现计算指定员工的年薪
----存储过程和存储函数的参数都不能带长度
----存储函数的返回值类型不能带长度
create or replace function f_yearsal(eno emp.empno%type) return number
is
s number(10);
begin
select sal*12+nvl(comm, 0) into s from emp where empno = eno;
return s;
end;
----测试f_yearsal
----存储函数在调用的时候,返回值需要接收。
declare
s number(10);
begin
s := f_yearsal(7788);
dbms_output.put_line(s);
end;
三、in 和 out 类型参数
凡是涉及到 into 查询语句赋值或者:=
赋值操作的参数,都必须使用out来修饰
--使用存储过程来算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
s number(10);
c emp.comm%type;
begin
select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
yearsal := s+c;
end;
---测试p_yearsal
declare
yearsal number(10);
begin
p_yearsal(7788, yearsal);
dbms_output.put_line(yearsal);
end;
四、存储过程和存储函数的区别
-
语法区别:关键字不一样,
存储函数比存储过程多了两个return。 -
本质区别:存储函数有返回值,而存储过程没有返回值。
如果存储过程想实现有返回值的业务,我们就必须使用out类型的参数。
即便是存储过程使用了out类型的参数,起本质也不是真的有了返回值,
而是在存储过程内部给out类型参数赋值,在执行完毕后,我们直接拿到输出类型参数的值。
----我们可以使用存储函数有返回值的特性,来自定义函数。
----而存储过程不能用来自定义函数。
----案例需求:查询出员工姓名,员工所在部门名称。
----案例准备工作:把scott用户下的dept表复制到当前用户下。
create table dept as select * from scott.dept;
----使用传统方式来实现案例需求
select e.ename, d.dname
from emp e, dept d
where e.deptno=d.deptno;
----使用存储函数来实现提供一个部门编号,输出一个部门名称。
create or replace function fdna(dno dept.deptno%type) return dept.dname%type
is
dna dept.dname%type;
begin
select dname into dna from dept where deptno = dno;
return dna;
end;
---使用 fdna 存储函数来实现案例需求:查询出员工姓名,员工所在部门名称。
select e.ename, fdna(e.deptno) from emp e;