如何在 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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告