使用 DECLARE 在 MySQL 中建立變數?


你可以在儲存過程中使用 DECLARE。語法如下 -

declare yourVariableName yourDataType;

為了理解上述語法,讓我們建立一個儲存過程

mysql> delimiter //
mysql> create procedure square_demo(in Value int)
   begin
   declare magicValue int;
   set magicValue=Value;
   select concat('Your Square Value=',magicValue*magicValue) as Output;
   end ;
   //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

現在,你可以使用呼叫命令來呼叫一個儲存過程 -

mysql> call square_demo(15);

這會產生以下輸出 -

+-----------------------+
| Output                |
+-----------------------+
| Your Square Value=225 |
+-----------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

更新於: 30-12-2019

483 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告