Bạn có thể sử dụng DECLARE trong một quy trình được lưu trữ. Cú pháp như sau -
declare yourVariableName yourDataType;
Để hiểu cú pháp trên, chúng ta hãy tạo một thủ tục được lưu trữ:
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 ; Bây giờ bạn có thể gọi một thủ tục được lưu trữ bằng lệnh gọi -
mysql> call square_demo(15);
Điều này sẽ tạo ra kết quả sau -
+-----------------------+ | Output | +-----------------------+ | Your Square Value=225 | +-----------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)