控制语句——变量作用域
- 内部的变量在其作用域内具有更高的优先级;
# 定义存储过程
delimiter //
create procedure proc3()
begin declare v1 varchar(5) default 'outer';
begin declare v1 varchar(5) default 'inner';
select v1;
end;
select v1;
end
//
delimiter ;
# 调用存储过程
call proc3();
-- 结果
v1
inner
v1
outer
控制语句——条件语句
if-then-else 语句
# 创建表并插入记录
drop table if exists test_if;
create table test_if(col int);
insert into test_if values(1);
# 创建存储过程
delimiter //
create procedure if_proc(in par int)
begin
declare var int;
set var=par+1;
if var=0 then
insert into test_if values(17);
end if;
if par=0 then
update test_if set col=col+1;
else
update test_if set col=col+2;
end if;
end
//
delimiter;
# 调用存储过程
call if_proc(3);
# 查看表数据
select * from test_if;
set sql_safe_updates=0;
-- 结果
col
3
case 语句
# 创建表
drop table if exists test_case;
create table test_case(col int);
# 创建存储过程
delimiter //
create procedure case_proc(in par int)
begin
declare var int;
set var=par+1;
case var
when 0 then
insert into test_case values(17);
when 1 then
insert into test_case values(18);
else
insert into test_case values(19);
end case;
end
//
delimiter ;
# 调用存储过程
call case_proc(5);
# 查看表数据
select * from test_case;
-- 结果
col
19
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




