目录
8.1 存储过程
存储过程是有参数的
存储过程中的参数分为以下三种类型
1.in:input 输入 表示这个参数是传递进存储过程的数据
2.out:output 输出 表示这个参数 是有数据返回出来
3.inout:表示这个参数可以传递参数 也可以返回数据
create procedure 存储过程名(参数列表...)
begin
逻辑SQL语句
end;
# 参数的定义
参数类型 参数名 数据类型
案例:查询火云邪神的信息....
create procedure getInfo()
begin
select * from studentinfo where stuName = "火云邪神";
end;
call getInfo();
案例:根据一个人的名字 返回一个身份证信息
drop procedure returninfo;
create procedure returnInfo(in stu_name char(10), out stu_idcard char(20))
BEGIN
#2. stucard是从表中查询出来的 并且通过into赋值给了stu_idcard参数
select stucard into stu_idcard from studentinfo where stuName = stu_name;
END;
# 1.调用 存储过程 传递树下参数
call returnInfo("树下", @card);
# 3. stu_idcard参数把数据返回给了card变量
# 只有在调用函数的时候 接收数据时需要定义参数接收
变量的定义
@变量名
select @card;
Incorrect integer value: '火云邪神' for column 'args' at row 1
# inout案例 : 传递一个id并返回一个名字
# into语法
查询结果 into 参数
结果1, 结果2 into 参数1,参数2
案例:通过学生的id去查询学生的姓名和年龄 并返回出来
create procedure getStuInfo(in id int, out `name` char(10), out age int)
begin
select stuName, stuAge into `name`, age from studentinfo where stuid = id;
end;
call getStuInfo(5, @stu_name, @stu_age);
select @stu_name, @stu_age;
8.2 变量
需要通过set关键字给变量赋值
set @变量名 = 值;
set 变量名 = 值;
1.用户变量
指的是用户连接了数据库 并创建的变量 叫做用户变量 只限于当前连接只用
别的用户不能使用 @变量名
2.局部变量
指的是在存储过程中 使用declare关键字声明的变量叫做局部变量
在存储过程中声明的变量叫做局部变量
# 变量的定义一定是在存储过程中的最上面
# 语法: declare 变量名 数据类型 [default 默认值]; default可以省略不写
declare a int default 0;
set a = 10;
select a;
3.全局变量
4.会话变量
8.3 分支语句
8.3.1 if语句
1.if分支
关系表达式:由关系运算符构成
< <= > >= = !=
# 语法1:if(关系表达式, 结果1, 结果2) 如果关系表达式的结果为true 则返回结果1 否则返回结果2
select id, `name`, if(sex = 1, "男", "女") as sex from userinfo;
# 语法2:ifnull(结果1, 结果2): 如果当前的数据不为空 则显示结果1 否则显示结果2
select id , ifnull(`name`, "未知") as `name` from userinfo;
# 语法3:这个语法只能写在存储过程中
if 关系表达式 then
执行sql语句
elseif 关系表达式2 then
sql语句2
else
sql语句3
end if;
案例:如果学生是男孩子 则查询姓名 年龄和身份证号码
否则查询姓名 性别 和入学时间
create procedure showMage(in id int)
begin
declare sex char(1);
select stusex into sex from studentinfo where stuid = id;
if sex = "男" then
select stuname, stuage, stucard from studentinfo where stuid = id;
else
select stuname, stusex, stujointime from studentinfo where stuid = id;
end if;
end;
call showMage(4);
8.3.2 case分支
2.case分支
# 语法1:不能再存储过程中执行 只能在sql语句中
case 列名
when 数据1 then
sql语句1
when 数据2 then
sql语句2
else
sql语句3
end
案例:
select `name`,
case sex
when 1 then
"男"
when 0 then
"女"
else
"你的性别有误"
end as 性别 from userinfo; end的这个位置 不能写end case
# 语法2: 这个case做等值判断
case 常量
when 数据1 then
sql语句
when 数据2 then
sql语句2
end case;
# 语法3:这个case做范围值判断
case
when 关系表达式 then
sql语句
when 关系表达式2 then
sql语句2
else
sql语句3
end case;
create procedure showMage1(in id int)
begin
declare sex char(1);
select stusex into sex from studentinfo where stuid = id;
case sex
when "男" then
select stuname, stuage, stucard from studentinfo where stuid = id;
when "女" then
select stuname, stusex, stujointime from studentinfo where stuid = id;
else
select "你是人吗?" as 异常;
end case;
end;
call showMage1(6);
8.4 循环
leave:跳出循环 等同于break
iterate:结束当前次循环 进入下一次循环 等同于 continue
这两个关键字 所有循环都能使用
while
语法:
while 关系表达式 do
循环操作
end while
案例:添加100条测试数据
create procedure addData()
begin
declare i int default 1;
while i <= 100 do
insert into user(name, age) values(concat("demo", i), i);
set i = i + 1;
end while;
end
loop
语法:死循环 需要借助条件判断结束
标签:loop
循环操作
end loop;
案例:添加100条测试数据 loop版本
create procedure addData1()
begin
declare i int default 0;
xunHuan:loop
insert into user(name, age) values(concat("demo", 1), 1);
set i = i + 1;
if i = 100 then
leave xunHuan;
end if;
end loop;
end;
drop procedure addData1;
call addData1();
repeat until
语法:repeat
循环操作
until 条件 条件的后面不需要打分号
end repeat;
until循环满足 就循环退出
案例:添加100条测试数据 repeat版本
create procedure addData2()
begin
declare i int default 0;
repeat
insert into user(name, age) values(concat("demo", i), i);
set i = i + 1;
until i = 100
end repeat;
end