if-case-循环语句

IF语句

drop procedure if exists p_hello_world;
create procedure p_hello_world(in v_id int)
begin
if (v_id > 0) then
select '> 0';
elseif (v_id = 0) then
select '= 0';
else
select '< 0';
end if;
end;
call p_hello_world(-9);

Case语句

drop procedure if exists pro2;
delimiter //
create procedure pro2(in tid int(10))
begin
case tid
when 1 then
insert into test values(1,'xjh','tx','');
when 2 then
insert into test values(2,'zjj','tx','');
end case;
end//
delimiter ;

如果case中未处理的参数则会报 Case not found for CASE statement 错误

循环

  WHILE-DO…END-WHILE循环

delimiter //
create procedure pro2()
begin
set @i = 0;
while @i<10 do
insert into test (tid,tname)values(@i,'批量');
set @i=@i+1;
end while;
end//
delimiter ;

  REPEAT...END REPEAT

delimiter //
create procedure pro2()
begin
set @i = 0;
repeat
insert into test (tid,tname)values(@i,'批量');
set @i = @i + 1;
UNTIL @i< 10 end repeat;
end//
delimiter ;

  LOOP...END LOOP

delimiter //
create procedure pro2()
begin
set @i = 0;
loop_pos:loop
insert into test (tid,tname)values(@i,'批量');
set @i = @i + 1;
if @i =2 then
leave loop_pos;
end if;
end loop;
end//
delimiter ;
上一篇:5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task


下一篇:Eclipse freemark报错:In a macro declaration,parameters without a default value must all occur before t