1. 存储过程简介
我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。
一个存储过程是一个可编程的函数,它在数据库中创建并保存。它可以有SQL语句和一些特殊的控制结构组成。当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的。数据库中的存储过程可以看做是对编程中面向对象方法的模拟。它允许控制数据的访问方式。
存储过程通常有以下优点:
(1).存储过程增强了SQL语言的功能和灵活性。存储过程可以用流控制语句编写,有很强的灵活性,可以完成复杂的判断和较复杂的运算。
(2).存储过程允许标准组件是编程。存储过程被创建后,可以在程序中被多次调用,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,对应用程序源代码毫无影响。
(3).存储过程能实现较快的执行速度。如果某一操作包含大量的Transaction-SQL代码或分别被多次执行,那么存储过程要比批处理的执行速度快很多。因为存储过程是预编译的。在首次运行一个存储过程时查询,优化器对其进行分析优化,并且给出最终被存储在系统表中的执行计划。而批处理的Transaction-SQL语句在每次运行时都要进行编译和优化,速度相对要慢一些。
(4).存储过程能过减少网络流量。针对同一个数据库对象的操作(如查询、修改),如果这一操作所涉及的Transaction-SQL语句被组织程存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的只是该调用语句,从而大大增加了网络流量并降低了网络负载。
(5).存储过程可被作为一种安全机制来充分利用。系统管理员通过执行某一存储过程的权限进行限制,能够实现对相应的数据的访问权限的限制,避免了非授权用户对数据的访问,保证了数据的安全。
二、MySQL存储过程
(1). 格式
MySQL存储过程创建的格式:CREATE PROCEDURE过程名 ([过程参数[,...]])
[特性 ...]过程体
案例分析:
参数
MySQL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:
CREATE PROCEDURE([[IN |OUT |INOUT ] 参数名数据类形...])
IN 输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值
OUT 输出参数:该值可在存储过程内部被改变,并可返回
INOUT 输入输出参数:调用时指定,并且可被改变和返回
1)不带参数:
1
2
3
4
5
6
7
8
9
10
|
mysql>delimiter
//
mysql> create procedure p1()
-> begin
-> declare v_ename varchar(10);
-> declare v_sal
int
;
-> select ename,sal into v_ename,v_sal from emp where empno=7788; -> select v_ename ;
-> select v_sal;
-> end;
->
//
Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;
|
mysql> call p1();
1
2
3
4
5
6
7
8
9
10
11
12
13
|
+---------+
| v_ename |
+---------+
| SCOTT |
+---------+
1
row
in
set (
0.00
sec)
+-------+
| v_sal |
+-------+
|
3000
|
+-------+
1
row
in
set (
0.00
sec)
Query OK,
0
rows affected (
0.00
sec)
|
2)in参数:
1
2
3
4
5
6
7
8
9
10
|
mysql> delimiter
//
mysql> create procedure p2(
in
v_empno int)
-> begin
-> declare v_ename varchar(
10
);
-> declare v_sal int;
-> select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno; -> select v_ename ;
-> select v_sal;
-> end;
->
//
Query OK,
0
rows affected (
0.00
sec)mysql> delimiter ;
|
执行:
mysql> call p2(7788);
1
2
3
4
5
6
7
8
9
10
11
12
13
|
+---------+
| v_ename |
+---------+
| SCOTT |
+---------+
1
row
in
set (
0.00
sec)
+-------+
| v_sal |
+-------+
|
3000
|
+-------+
1
row
in
set (
0.00
sec)
Query OK,
0
rows affected (
0.00
sec)
|
3)out参数:
1
2
3
4
5
6
7
8
9
|
mysql> delimiter
//
mysql> create procedure p3(
in
v_empno int,out v_sal int)
-> begin
-> declare v_ename varchar(
10
);
-> select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno; -> select v_ename ;
-> select v_sal;
-> end;
->
//
Query OK,
0
rows affected (
0.00
sec)mysql> delimiter ;
|
执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
mysql> call p3(
7788
,@v_sal);
+---------+
| v_ename |
+---------+
| SCOTT |
+---------+
1
row
in
set (
0.00
sec)
+-------+
| v_sal |
+-------+
|
3000
|
+-------+
1
row
in
set (
0.00
sec)
|
4)存储过程循环
while 循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
mysql> delimiter
//
mysql> create procedure p_while()
begin
declare v_empno int;
declare v_count int;
set v_empno=
7788
;
set v_count=
0
;
while
v_count <
10
do
insert
into
emp1 select *
from
emp
where
empno=v_empno;
set v_count=v_count +
1
;
end
while
;
end;
//
mysql> delimiter ;
|
repeat 循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> delimiter
//
mysql> create procedure p_repeat()
begin
declare v_empno int;
declare v_count int;
set v_empno=
7788
;
set v_count=
0
;
repeat
insert
into
emp1 select *
from
emp
where
empno=v_empno;
set v_count=v_count +
1
;
until v_count >
10
end repeat;
end;
//
mysql> delimiter ;
|
loop 循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
mysql> delimiter
//
mysql> create procedure p_loop()
begin
declare v_empno int;
declare v_count int;
set v_empno=
7788
;
set v_count=
0
;
loop_label:loop
insert
into
emp1 select *
from
emp
where
empno=v_empno;
set v_count=v_count +
1
;
if
v_count >
10
then
leave loop_label;
end
if
;
end loop;
end;
//
mysql> delimiter ;
|
三、Oracle 存储过程
不带参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
SQL> create
or
replace
procedure proc1 is
3
v_ename emp.ename%type;
4
v_sal emp.sal%type ;
5
6
begin
7
select ename,sal
into
v_ename,v_sal
from
emp
where
empno=&no;
8
dbms_output.put_line(
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
9
exception
10
when no_data_found
then
11
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
12
13
end;
14
/
Procedure created
|
执行:
SQL> exec proc1;
PL/SQL procedure successfully completed
SQL> set serverout on
SQL> /
Name is : SCOTT , Salary is : 3000
PL/SQL procedure successfully completed
带参数的存储过程:
参数定义中,IN、OUT和IN OUT代表参数的三种不同模式:
IN:当调用存储过程时,该模式的形参接收对应实参的值,并且该是只读的,即不能被修改。默认为IN。
OUT:该形参被认为只能写,既只能为其赋值。在存储过程中不能读它的值。返回时,将该形参值传给相应的实参。
IN OUT:都允许
in 参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
SQL> create
or
replace
procedure proc2 ( v_empno emp.empno%type) is
-- 如果不指明参数的模式,默认为
in
2
v_ename emp.ename%type;
3
v_sal emp.sal%type;
4
5
begin
6
select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno;
7
dbms_output.put_line(
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
8
exception
9
when no_data_found
then
10
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
11
12
end;
13
/
Procedure created
|
执行:
SQL> exec proc2(7369);
Name is : SMITH , Salary is : 8000
PL/SQL procedure successfully completed
1
2
3
4
5
6
7
8
9
10
11
12
13
|
SQL> create
or
replace
procedure proc2 ( v_empno
in
emp.empno%type) is
2
v_ename emp.ename%type;
3
v_sal emp.sal%type;
4
5
begin
6
select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno;
7
dbms_output.put_line(
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
8
exception
9
when no_data_found
then
10
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
11
12
end;
13
/
|
out 参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
SQL> create
or
replace
procedure proc2 ( v_empno
in
emp.empno%type, v_ename out emp.ename%type,v_sal out emp.sal%type)
2
is
3
4
begin
5
select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno;
6
dbms_output.put_line(
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
7
exception
8
when no_data_found
then
9
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
10
11
end;
12
/
Procedure created
|
执行:
SQL> var v_ename varchar2(10);
SQL> var v_sal number; //注意:对于number类型不能指明长度
SQL> exec proc2(7788,:v_ename,:v_sal );
Name is : SCOTT , Salary is : 3000
PL/SQL procedure successfully completed
v_ename
---------
SCOTT
v_sal
---------
3000
通过匿名块执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
SQL>
var
v_ename varchar2
SQL>
var
v_sal number
SQL> declare
2
v_empno emp.empno%type :=
7788
;
3
begin
4
proc2(v_empno ,:v_ename,:v_sal);
5
end;
6
/
Name is : SCOTT , Salary is :
3000
PL/SQL procedure successfully completed
v_ename
---------
SCOTT
v_sal
---------
3000
|
1
2
3
4
5
6
7
8
|
SQL> declare
2
v_empno emp.empno%type :=
7836
;
3
v_ename emp.ename%type ;
4
v_sal emp.sal%type;
5
begin
6
proc2(v_empno ,v_ename,v_sal);
7
end;
8
/
|
in out 参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
SQL>
create
or
replace
procedure proc3( v_empno
in
out emp.empno%type)
is
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal
into
v_ename,v_sal
from
emp
where
empno=v_empno;
dbms_output.put_line(
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
exception
when no_data_found
then
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
end;
/
|
执行:
SQL> var v_empno number;
SQL> exec :v_empno:=7788;
PL/SQL procedure successfully completed
v_empno
---------
7788
SQL> exec proc3(:v_empno);
Employees id is: 7788 , Name is : SCOTT , Salary is : 3000
PL/SQL procedure successfully completed
v_empno
---------
7788
1
2
3
4
5
6
7
8
9
10
11
|
SQL> declare
2
v_empno emp.empno%type;
3
begin
4
v_empno:=
7788
;
5
proc3(v_empno );
6
end;
7
/
Name is : SCOTT , Salary is :
3000
PL/SQL procedure successfully completed
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
SQL> create
or
replace
procedure proc3( v_empno
in
out emp.empno%type)
2
3
is
4
5
v_ename emp.ename%type;
6
v_sal emp.sal%type;
7
8
begin
9
select empno,ename,sal
into
v_empno, v_ename,v_sal
from
emp
where
empno=v_empno;
10
dbms_output.put_line(
'Employee number is: '
||v_empno||
' , '
||
'Name is : '
||v_ename||
' , '
||
'Salary is : '
||v_sal);
11
exception
12
when no_data_found
then
13
dbms_output.put_line(
'you number is not crrect ,please input again !'
);
14
15
end;
16
/
Procedure created
|
执行:
1
2
3
4
5
6
7
8
9
10
11
|
SQL> declare
2
v_empno emp.empno%type;
3
begin
4
v_empno :=
7788
;
5
proc3(v_empno);
6
end;
7
/
Employee number is:
7788
, Name is : SCOTT , Salary is :
3000
PL/SQL procedure successfully completed
|
Oracle 存储过程循环:
loop 基本循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
SQL> declare
2
v_empno emp.empno%type :=
7788
;
3
v_count number :=
0
;
4
5
begin
6
7
loop
8
insert
into
emp1 select *
from
emp
where
empno=v_empno;
9
v_count := v_count +
1
;
10
exit when v_count >=
10
;
11
end loop;
12
13
end;
14
/
PL/SQL procedure successfully completed
|
for 循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
SQL> declare
2
3
v_empno emp.empno%type;
4
5
begin
6
v_empno :=
7788
;
7
8
for
i
in
1.
.10
loop
9
insert
into
emp1 select *
from
emp
where
empno=v_empno;
10
end loop;
11
12
end;
13
14
15
16
/
PL/SQL procedure successfully completed
|
while 循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
SQL> declare
2
v_empno emp.empno%type;
3
v_count number :=
0
;
4
5
begin
6
v_empno :=
7788
;
7
8
while
v_count <
10
loop
9
insert
into
emp1 select *
from
emp
where
empno=v_empno;
10
v_count := v_count +
1
;
11
12
end loop;
13
end;
14
/
PL/SQL procedure successfully completed
|