mysql procedure存储过程循环,条件判断实例

本文转载自:     http://www.111cn.net/database/mysql/38878.htm

 

 

mysql教程 procedure存储过程循环,条件判断实例

 

mysql> delimiter $$
mysql> CREATE PROCEDURE myProc()
    -> DETERMINISTIC
    -> BEGIN
    ->   DECLARE counter INT DEFAULT 0;
    ->
    ->   simple_loop: LOOP
    ->     SET counter=counter+1;
    ->     select counter;
    ->     IF counter=10 THEN
    ->        LEAVE simple_loop;
    ->     END IF;
    ->   END LOOP simple_loop;
    ->   SELECT 'I can count to 10';
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+---------+
| counter |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

+---------+
| counter |
+---------+
|       2 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       3 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       4 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       5 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       6 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       7 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       8 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|       9 |
+---------+
1 row in set (0.33 sec)

+---------+
| counter |
+---------+
|      10 |
+---------+
1 row in set (0.33 sec)

+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
1 row in set (0.33 sec)

Query OK, 0 rows affected (0.33 sec)


实例二

mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->     DECLARE i int;
    ->     SET i=1;
    ->     myloop: LOOP
    ->          SET i=i+1;
    ->          IF i=10 THEN
    ->                   LEAVE myloop;
    ->          END IF;
    ->     END LOOP myloop;
    ->     SELECT 'I can count to 10';
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec

带有条件

mysql> create procedure increment (IN in_count INT)
    -> BEGIN
    -> declare count INT default 0;
    ->
    ->     increment: loop
    ->         set count = count + 1;
    ->         if count < 20 then
    ->             iterate increment;
    ->         end if;
    ->         if count > in_count then
    ->             leave increment;
    ->         end if;
    ->     end loop increment;
    ->     select count;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call increment(3);
+-------+
| count |
+-------+
|    20 |
+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL存储过程中,条件判断语句通常使用IF...ELSEIF...ELSE语句来实现。IF语句用于根据给定条件执行一段代码,如果条件为真,则执行IF语句块中的语句。如果条件为假,则跳过IF语句块。可以使用ELSEIF来添加更多的条件判断,以及使用ELSE来处理其他情况。 以下是一个示例的MySQL存储过程,展示了条件判断语句的用法: ``` DELIMITER $$ CREATE PROCEDURE getUserName() BEGIN DECLARE my_userId VARCHAR(255); DECLARE my_userName VARCHAR(255); SET my_userId = 'APP-2016-00494878'; IF my_userId = 'APP-2016-00494878' THEN SET my_userName = 'John Doe'; ELSEIF my_userId = 'APP-2016-00494879' THEN SET my_userName = 'Jane Smith'; ELSE SET my_userName = 'Unknown User'; END IF; SELECT my_userName; END $$ DELIMITER ; ``` 在上面的示例中,存储过程首先声明了两个变量my_userId和my_userName,然后根据my_userId的值进行条件判断。如果my_userId等于'APP-2016-00494878',则将my_userName设置为'John Doe';如果my_userId等于'APP-2016-00494879',则将my_userName设置为'Jane Smith';否则将my_userName设置为'Unknown User'。 最后,存储过程通过SELECT语句返回my_userName的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [mysql存储过程 条件判断/循环语句](https://blog.csdn.net/jxpxlinkui/article/details/79709040)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [MySQL存储过程(二)IF、CASE…WHEN 判断语句](https://blog.csdn.net/Cheng_Q/article/details/129489248)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值