如何在 MySQL 儲存過程中正確實現條件?
如需在儲存過程中設定條件,請使用以下語法 −
if yourCondition then yourStatement1; else yourStatement2'; end if ; end //
讓我們按照以上語法來修復儲存過程中的缺失分號 −
mysql> delimiter // mysql> create procedure Test_Demo(In inputValue int) -> BEGIN -> if inputValue=10 then -> select 'You have won 100$'; -> else -> select 'Sorry !!!'; -> end if ; -> end -> // Query OK, 0 rows affected (0.20 sec) mysql> delimiter ;
現在,您可以使用 CALL 命令呼叫儲存過程 −
mysql> call Test_Demo(10);
這將產生以下輸出 −
+-------------------+ | You have won 100$ | +-------------------+ | You have won 100$ | +-------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
廣告