How to declare a variable correctly in a MySQLProcedure?n

The variable declaration must be between BEGIN and END. Under BEGIN and END, the first statement must be declaration of variable. After that you can include insert, select, etc.

Let us now see an example. Here, the variable name is “output”:

mysql> DELIMITER //
mysql> CREATE PROCEDURE showVariablesValue()
   -> BEGIN
   -> DECLARE output varchar(100);
   -> SET output="Hello MySQL";
   -> SELECT output;
   -> END
   -> //
Query OK, 0 rows affected (0.25 sec)

mysql> DELIMITER ;

Now you can call the stored procedure using call command:

mysql> CALL showVariablesValue();

This will produce the following output

| output      |
| Hello MySQL |
1 row in set (0.03 sec)

Query OK, 0 rows affected (0.04 sec)