if 實現條件判斷,知足不一樣的條件執行不一樣的語句列表。mysql
# if 語句 # 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 實現比 if 更復雜一些的條件構造。sql
# case 語句 # CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list] ... [ELSE statement_list] END CASE # CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list] END CASE > case when i_staff_id = 2 then set @x1 = @x1 + d_amount; else set @x2 = @x2 + d_amount; end case; > case i_staff_id when 2 then set @x1 = @x1 + d_amount; else set @x2 = @x2 + d_amount; end case;
loop 實現簡單的循環,退出循環的條件須要使用其餘的語句定義,一般能夠使用 leave 語句實現。oop
# loop 語句 # [begin_label:] LOOP statement_list END LOOP [end_label]
若是不在 statement_list
中增長退出循環的語句,那麼 loop 語句能夠用來實現簡單的死循環。code
leave 用來從標註的流程構造中退出,一般和 BEGIN ... END 或者循環一塊兒使用。it
下面是一個使用 loop 和 leave 的簡單例子,循環 100 次向 actor 表中插入記錄,當插入 100 條記錄後,退出循環。io
# leave 語句 > create procedure actor_insert () 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', '201'); END LOOP ins; END; $$ Query OK, 0 rows affected (0.00 sec) > call actor_insert(); Query OK, 0 rows affected (0.01 sec) > select count(*) from actor where first_name = 'Test'; +----------+ | count(*) | +----------+ | 100 | +----------+ 1 row in set (0.00 sec)
iterate 必須用在循環中,做用是跳過當前循環的剩下的語句,直接進入下一輪循環。ast
下面的例子使用了 iterate 語句,當 @x 變量是偶數的時候,再也不執行循環中剩下的語句,而直接進行下一輪的循環。變量
# iterate 語句 > CREATE PROCEDURE actor_insert () BEGIN set @x = 0; ins: LOOP set @x = @x + 1; IF @x = 10 THEN LEAVE ins; ELSEIF mod(@x,2) = 0 THEN ITERATE ins; END IF; INSERT INTO actor(actor_id,first_name,last_name) VALUES (@x+200, 'Test',@x); END LOOP ins; END; $$ Query OK, 0 rows affected (0.00 sec) > call actor_insert(); Query OK, 0 rows affected (0.00 sec) > select actor_id,first_name,last_name from actor where first_name='Test'; +----------+------------+-----------+ | actor_id | first_name | last_name | +----------+------------+-----------+ | 201 | Test | 1 | | 203 | Test | 3 | | 205 | Test | 5 | | 207 | Test | 7 | | 209 | Test | 9 | +----------+------------+-----------+ 5 rows in set (0.00 sec)
repeat 有條件的循環控制語句,當知足條件的時候退出循環。select
# repeat 語句 # [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 實現的也是有條件的循環控制語句,即當知足條件時執行循環的內容。循環
while 循環和 repeat 循環的區別
# while 語句 # [begin_label:] WHILE search_condition DO statement_list END WHILE [end_label] > delimiter $$ > CREATE PROCEDURE loop_demo () BEGIN set @x = 1 , @x1 = 1; REPEAT set @x = @x + 1; UNTIL @x > 0 END REPEAT; WHILE @x1 < 0 DO set @x1 = @x1 + 1; END WHILE; END; $$ Query OK, 0 rows affected (0.00 sec) > delimiter ; > call loop_demo(); Query OK, 0 rows affected (0.00 sec) > select @x,@x1; +------+------+ | @x | @x1 | +------+------+ | 2 | 1 | +------+------+ 1 row in set (0.00 sec)