1、存储过程
- 什么是存储过程
- 存储过程是完成特定功能的 sql 语句集合。通过编译后存储在数据库中,通过指定的存储过程名称调用执行它
- 存储过程 = sql 语句集合 + 控制语句
- 使用存储过程的优点
- 存储过程创建可以多次调用,不需要重新编写存储过程语句
- 存储过程支持接收参数,返回输出值
- 存储过程加快程序的运行速度
- 存储过程增加sql语句的功能和灵活性
- 创建存储过程
# 创建存储过程
delimiter //
create procedure 存储过程名称([in|out|inout]参数名,数据类型)
begin 存储过程体
end
//
delimiter ;call 存储过程名称(参数) # 调用存储过程
-
- delimiter:分隔符,分界符,这里指定的分隔符是 //,可自定义
- 创建一个存储过程
- create procedure 存储过程名称()
- begin...end 代表存储过程体的开始和结束
- 删除一个存储过程
- drop procedure if exists 存储过程名称 # 加强代码的健壮性
- 调用一个存储过程
- call 存储过程名称()
- in 参数值在调用时必须指定
- out 参数可以在调用后被返回
- inout 参数调用时指定,并且可以被返回
# in 输入参数,b 后面括号中接收参数
delimiter //
drop procedure if exists b;create procedure b(in n int)
beginselect n;set n = 2;select n;
end
//
delimiter ;set @n = 1;
call b(@n); # 调用存储过程 b+------+
| n |
+------+
| 1 |
+------++------+
| n |
+------+
| 2 |
+------+
# out 输出参数
# out 是向调用者输出参数,不接收输入的参数
delimiter //
drop procedure if exists b;create procedure b(out n int)
beginselect n;set n = 2;select n;
end
//
delimiter ;set @n = 1;
call b(@n); # 调用存储过程 b+-------+
| n |
+-------+
| NULL | # 因为 out 是向调用者输出参数,不接收输入的参数,所以存储过程里的 n 为 null
+-------+ +-------+
| n |
+-------+
| 2 |
+-------+
# inout 输入参数
delimiter //
drop procedure if exists b;create procedure b(inout n int)
beginselect n;set n = 2;select n;
end
//
delimiter ;set @n = 1;
call b(@n); # 调用存储过程 b+---------+
| n |
+---------+
| 1 |
+---------++---------+
| n |
+---------+
| 2 |
+---------+
2、实例
# 创建表 t 且往表 t 中循环插入数据
delimiter //
drop procedure if exists a;create procedure a(in n int)
begindeclare x int default 1;declare y int default 10;drop table if exists t;create table t(id int(3),age int(3));while n <20 doinsert into t values(x,y);set x &#61; x &#43; 1;set y &#61; y &#43; 10;set n &#61; n &#43; 1;end while;
select * from t;
end
//
delimiter ;call a(5) # 调用存储过程
delimiter //
drop PROCEDURE if EXISTS i;
CREATE PROCEDURE i(n int)
BEGIN
DECLARE x int DEFAULT 1;
DECLARE y varchar(20) DEFAULT "";
DECLARE z int DEFAULT 22;while n <&#61;100 DOset y &#61; CONCAT("zhangsan",x);INSERT into t VALUES(x,y,z);set x &#61; x &#43; 1;set n &#61; n &#43; 1;
end WHILE;
SELECT COUNT(*) from t;
end
//
delimiter ;call i(1);
delimiter //
drop procedure if exists b;create procedure b(in n int)
begindeclare x int default 1;while n <10 do delete from t where id &#61; x;set n &#61; n &#43; 1;set x &#61; x &#43; 1;end while;
select * from t;
end
//
delimiter ;call b(0) # 调用存储过程
delimiter //
drop procedure if exists c;
create procedure c(n int)BEGIN
drop table if exists dcs;
create table dcs(id int(1),name varchar(10),sex char(2));
alter table dcs change id id int(1) primary key auto_increment;
insert into dcs(name,sex)values(&#39;zhangsan1&#39;,&#39;m&#39;),(&#39;lisi1&#39;,&#39;m&#39;);
insert into dcs(name,sex)values(&#39;zhangsan2&#39;,&#39;m&#39;),(&#39;lisi2&#39;,&#39;m&#39;);
insert into dcs(name,sex)values(&#39;zhangsan3&#39;,&#39;m&#39;),(&#39;lisi3&#39;,&#39;m&#39;);
insert into dcs(name,sex)values(&#39;zhangsan4&#39;,&#39;m&#39;),(&#39;lisi4&#39;,&#39;m&#39;);
insert into dcs(name,sex)values(&#39;zhangsan5&#39;,&#39;m&#39;),(&#39;lisi5&#39;,&#39;m&#39;);if (n<>0) thenselect * from dcs where id elseselect * from dcs;
end if;
end
//
delimiter ;call c(6) # 调用存储过程