bitscn.com
mysql那些事儿之(十五)流程的控制
相关链接:
mysql那些事儿之(一)mysql的安装
http:///database/201210/162314.html;
mysql那些事儿之(二)有关数据库的操作
http:///database/201210/162315.html;
mysql那些事儿之(三)有关数据表的操作
http:///database/201210/162316.html;
mysql那些事儿之(四)数据表数据查询操作
http:///database/201210/162317.html;
mysql那些事儿之(五)操作时间
http:///database/201210/162318.html;
mysql那些事儿之(六)字符串模式匹配
http:///database/201210/163969.html;
mysql那些事儿之(七)深入select查询
http:///database/201210/163970.html;
mysql那些事儿之(八)索引
http:///database/201210/163971.html;
mysql那些事儿之(九)常用的函数
http:///database/201210/164229.html;
mysql那些事儿之(十)触发器一
http:///database/201210/164516.html;
mysql那些事儿之(十一)触发器二
http:///database/201210/164766.html;
mysql那些事儿之(十二)存储过程
http:///database/201210/164795.html;
mysql那些事儿之(十三)变量、条件的使用
http:///database/201211/165662.html;
mysql那些事儿之(十四)光标的使用
http:///database/201211/165664.html
if语句
sql代码
---语法结构
if search_condition then statement_list
[elseif search_condition then statement_list]....
[else statement_list]
end if
---举例
if i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end if;
case语句
sql代码
---case语句的语法格式
case case_value
when when_value then statement_list
[when when_value then statement_list]....
[else statement_list]
end case
---case语句举例:
case
when i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end case
loop语句
sql代码
[begin_label:] loop
statement_list
end loop [end_label]
---如果不在statement_list中增加退出循环的语句,那么loop语句可以用来实现简单的死循环。
leave语句
sql代码
---将结束符替换为$$
delimiter $$
---创建存储过程
create procedure actor_num()
begin
set @x = 0;
ins:loop
set @x = @x + 1;
if @x = 100 then
leave ins;
end if;
insert into actor(first_name,last_name) values('test',222);
end loop ins;
end;
$$
delimiter ;
iterate语句
sql代码
--必须用在循环中,作用就是跳过当前的循环直接进入下一轮循环。
delimiter $$
create procedure actor_num()
begin
set @x = 0;
ins:loop
set @x = @x + 1;
if @x = 100 then
leave ins;
elseif mod(@x/2,0) = 0 then
iterate ins;
end if;
insert into actor(first_name,last_name) values('test',222);
end loop ins;
end;
$$
delimiter ;
repeat 语句
sql代码
--有条件循环,当满足条件的时候退出循环。
--语法:
[begin_label:] repeat
statement_list
until search_condition
end repeat [end_label]
--举例
repeat
fetch cur_payment into i_staff_id,d_amount;
if i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end if;
until 0 end repeat;
while 语句
sql代码
---语法结构
[begin_label:] while search_condition do
statement_list
end while [end_label]
bitscn.com