1 存储过程概念
存储过程封装若干条语句,调用时,这些封装体执行,
存储过程是没有返回值的函数
存储过程-------把若干条sql封装起来,起个名字----过程
把此过程存储在数据库中----------存储过程
存储过程是可以编程的
意味着可以使用变量,表达式,控制结构
来完成复杂的功能
1.1 存储过程的创建语法
create procedure prodedureName() ---(存储过程的名称)
begin
---Sql语句;
end;
查看已有的存储过程:show procedure status;
删除已有的存储过程:drop procedure prodedureName ---(存储过程的名称)
使用方式:call prodedureName() ---(存储过程的名称)
1.2 引入变量
格式: declare 变量名 变量类型 [default 默认值]
1.3 存储过程中,变量可以在sql语句中合法的运算
如 + - * / 或逻辑判断
注意的是,运算的结果,如何复制给变量
set 变量名 = expression
1.4 引入表达式
If condtion then
Statement;
else
Statement;
end if;
1.5 存储过程引入参数
存储过程的括号里,可以声明参数
语法是 【in/out/inout】参数名 参数类型
“ in ”argu is input type,which can get the value we input;
1.6 控制结构
顺序,选择,循坏
1》分支结构==================
---- if 用法
if (condition,Statement_1,Statement_2)
Statement: sql语句,或运算,字段,变量
实现if-else的效果,有点类似于三目运算符
---- if 分支结构
If condtion then
Statement;
elseif condition then
Statement;
else
Statement;
end if;
----- Case分支结构用法
case <value 变量>
when condition1 then
sql语句,或运算;
when condition2 then
sql2语句,或运算;
else
sql3语句,或运算;
end case;
2》循环结构==================
----- Repeat 循坏结构用法
Repeat
Sql statement;
Sql statement;
Until condition #注意此处条件是,达到该条件就退出循环
End repeat;
----- while循坏结构用法
while condition do
Sql statement;
Sql statement;
end while;
----- loop 循坏结构用法【一般不建议用 毕竟是死循环】
loop
Sql statement;
Sql statement;
end loop;
创建数据表:
#创建表
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL DEFAULT '' COMMENT '用户名'
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
demo1【创建存储过程】:
#创建存储过程
create procedure p1()
BEGIN
select * from test;
end;
#显示存储过程
show procedure status;
#调用存储过程
call p1();
#删除存储过程
drop procedure p1;
执行结果(截图):
demo2【引入变量】:
#创建存储过程
create procedure p2()
BEGIN
declare ids int default 1;
select * from test where id=ids;
end;
#执行存储过程
call p2();
执行结果(截图):
demo3【引入运算,控制】:
#创建存储过程
create procedure p3()
BEGIN
declare ids int default 1;
set ids=ids*2;
select * from test where id=ids;
end;
#执行存储过程
call p3()
执行结果(截图):
demo4【引入表达式】:
#创建存储过程
create procedure p4()
BEGIN
declare id_num int;
select id into id_num from test where id=5;
if id_num>8 THEN
select '大于8 的数据';
elseif id_num>=2 && id_num<=4 then
select '2到4之间的数据';
elseif id_num=5 THEN
select '5的数据';
else
select '其他数据';
end if;
end;
#执行存储过程
call p4();
执行结果(截图):
demo5【引入参数】:
普通方式传入变量:
#创建存储过程
create procedure p5(ids int,name_value varchar(30))
BEGIN
if ids>100 THEN
select '没有这样的数据';
elseif ids<=5 THEN
select * from test where id=ids and name=name_value;
ELSE
select '请自行插入数据';
end if;
end;
#执行存储过程
call p5(1,'aa')
执行结果(截图):
in 方式传入变量
#创建存储过程[in 方式传入变量]
create procedure p5_1(in ids int)
begin
select * from test where id=ids;
end;
#执行存储过程
call p5_1(4);
执行结果(截图):
out传出变量:
#创建存储过程[out 传出变量]
create procedure p5_2(in ids int,out name_values varchar(30))
BEGIN
select name into name_values from test where id=ids;
end;
#执行存储过程
call p5_2(2,@name_values); #传出变量一定要加@符号
select @name_values as '变量内容'; #查询传出的变量值
执行结果(截图):
inout 传入传出变量:
#创建存储过程[inout 传入传出变量]
create procedure p5_3(inout ids int)
BEGIN
select id into ids from test where id=ids;
set ids=ids+1000;
end;
#执行存储过程
set @ids=3; #先设置变量值
call p5_3(@ids); #运行过程
select @ids as '执行后的值'; #查询传出变量值
执行结果(截图):
#创建存储过程[inout 传入传出变量]
create procedure p5_4(inout ids int,out name_values varchar(30))
BEGIN
select name into name_values from test where id=ids;
set ids=ids+1000;
end;
#执行存储过程
set @ids=4; #先设置变量值
call p5_4(@ids,@name_values); #运行过程
select @ids,@name_values; #查询传出变量值
执行结果(截图):
demo6.【控制结构,顺序-选择-循坏】
while循坏:
#方式1:
#创建存储过程计算1到100的和
create procedure p6(in n int)
BEGIN
declare total int default 0;
declare num int default 0;
while num<n DO
set num=num+1;
set total=total+num;
end while;
select total;
end;
#执行存储过程
call p6(100);
#方式2:
#创建存储过程求和
create procedure p6_1(in n int,in num int,inout total int)
BEGIN
while num<n DO
set num=num+1;
set total=total+num;
end while;
end;
#执行存储过程
set @total=0; #先设置变量值
call p6_1(100,0,@total); #运行过程
select @total; #查询传出变量值
执行结果(截图):
repeat循环
#创建存储过程
create procedure p6_2(in num int,in n int,in total int)
BEGIN
REPEAT
set num=num+1;
set total=total+num;
UNTIL num>n
end repeat;
select total;
end;
#执行存储过程
call p6_2(0,100,0);
执行结果(截图):
if 流程判断
#创建存储过程
create procedure p6_3(in num int,in n int)
BEGIN
select if(num>n,'num>n','num<n') as complete;
end;
#执行存储过程
call p6_3(10,100)
执行结果(截图):
case流程控制
#创建存储过程
create procedure p6_4(in n int)
BEGIN
case n
when 1 THEN
select '第一';
when 2 THEN
select '第二';
when 3 THEN
select '第三';
ELSE
select '没上排名';
end case;
end;
#执行存储过程
call p6_4(2);
执行结果(截图):