如何在 MySQL 存储过程中实现 If else?


要实现 if-else,语法如下 −

if yourCondition then
     yourStatement1;
    else
    yourStatement2;
    end if ;

为了理解存储过程中 if-else 的上述概念,我们创建一个存储过程 −

mysql> delimiter //
mysql> create procedure If_else_stored_demo(value int)
     begin
     if value > 1000 then
     select "your value is greater than 1000";
     else
     select "your value is less than or equal to 1000";
     end if ;
     end
     //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

现在,你可以使用 call 命令调用存储过程 −

mysql> call If_else_stored_demo(500);

这会产生以下输出 −

+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

更新日期: 24-12-2019

310 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.