Using DECLARE to create variable in MySQL?

MySQLMySQLi Database

You can use DECLARE in a stored procedure. The syntax is as follows −

declare yourVariableName yourDataType;

To understand the above syntax, let us create a stored procedure:

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 ;

Now you can call a stored procedure using call command −

mysql> call square_demo(15);

This will produce the following output −

+-----------------------+
| Output                |
+-----------------------+
| Your Square Value=225 |
+-----------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
raja
Published on 30-Dec-2019 07:03:53
Advertisements