MySQL - DECLARE Statement

When you are working with BEGIN ... END compound statements such as variable declarations, conditions, cursors, including loops, conditional tests, functions and procedures, if you need to define items locally in it you can do so using the DECLARE Statement.
Using this statement, you can create Local variables, Conditions, handlers and, Cursors. While declaring these locally you need to follow the below order −
Variable and condition declarations –> Cursor declarations-> handler declarations.
Following is the syntax of the MySQL DECLARE statement −
DECLARE var_name [, var_name] ... type [DEFAULT value]

You can use the MySQL DECLARE statement to define local variables, Conditions and handlers and, cursors.
Following is an example of declaring local variables −
DELIMITER //
CREATE PROCEDURE RepeatExample()
BEGIN
   DECLARE val INT;
   DECLARE squares INT;
   DECLARE res VARCHAR(100);
   SET val=1;
   SET squares=1;
   SET res = '';
   REPEAT
      SET squares = val*val;
      SET res = CONCAT(res, squares,',');
      SET val = val + 1;
   UNTIL val >= 10
   END REPEAT;
   SELECT res;
END//
DELIMITER ;
You can call the above procedure as follows −
mysql> CALL RepeatExample;
+--------------------------+
| res                      |
+--------------------------+
| 1,4,9,16,25,36,49,64,81, |
+--------------------------+
1 row in set (0.07 sec)
Query OK, 0 rows affected (0.08 sec)
Following is an example of declaring conditions variables −
mysql> DELIMITER //
   CREATE PROCEDURE sample(IN dividend INT, IN divisor INT, OUT res double)
   BEGIN
      DECLARE dividedByZero CONDITION FOR SQLSTATE '22012';
      DECLARE CONTINUE HANDLER FOR dividedByZero
      RESIGNAL SET MESSAGE_TEXT = 'Error Denominator given is zero';
   IF divisor = 0 THEN
      SIGNAL dividedByZero;
   ELSE
      SET res:= dividend / divisor;
   END IF;
   END//
Query OK, 0 rows affected (1.21 sec)
mysql> DELIMITER ;
If you call the above procedure by passing the second parameter as '0' an error will be displayed as −
mysql> CALL sample(25, 0, @res);
ERROR 1644 (22012): Error Denominator given is zero
Assume we have created a table with name mobile_sales in MySQLdatabase using CREATE statement as shown below −
CREATE TABLE mobile_sales (
   SNO INT,
   mobile_brand VARCHAR(255),
   unit_sale INT
);
Now, we will insert 11 records in mobile_sales table using INSERT statements −
insert into mobile_sales values(1, 'Iphone', 3000);
insert into mobile_sales values(2, 'Samsung', 4000);
insert into mobile_sales values(3, 'Nokia', 5000);
insert into mobile_sales values(4, 'Vivo', 1500);
insert into mobile_sales values(5, 'Oppo', 900);
insert into mobile_sales values(6, 'MI', 6400);
insert into mobile_sales values(7, 'MotoG', 4360);
insert into mobile_sales values(8, 'Lenovo', 4100);
insert into mobile_sales values(9, 'RedMI', 4000);
insert into mobile_sales values(10, 'MotoG', 4360);
Following query is an example of the cursors statement −
mysql> DELIMITER //
mysql> CREATE PROCEDURE cursorExample(num INT)
   BEGIN
      DECLARE number_of_units varchar(255);
      DECLARE cur1 CURSOR for SELECT unit_sale FROM mobile_sales WHERE SNO=num;
      OPEN cur1;
         FETCH cur1 into number_of_units;
         SELECT number_of_units;
      CLOSE cur1;
   END //
Query OK, 0 rows affected (0.02 sec)
mysql> delimiter ;
You can call the above procedure as shown below −
mysql> CALL cursorExample(5);
+-----------------+
| number_of_units |
+-----------------+
| 900             |
+-----------------+
1 row in set (0.04 sec)
Query OK, 0 rows affected (0.04 sec) 

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page