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‘,‘15268335587‘); when 2 then insert into test values(2,‘zjj‘,‘tx‘,‘15268335587‘); 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 ;