MySQL - SIGNAL Statement

While working with stored procedures in MySQL if an exception or occurs the execution of the procedure terminates abruptly, to avoid this you need to handle the exceptions in MYSQL.
MySQL provides a handler to handle the exceptions in the stored procedures. You can handle these exceptions by declaring a handler using the MySQL DECLARE ... HANDLER Statement.
The SIGNAL in MySQL is used provide error information to a handler, application or a client.
Following is the syntax of the MySQL SIGNAL Statement −
SIGNAL condition_value [SET signal_information_item]

Where, condition_value represents the error value to be returned, which can be sqlstate_value or, condition_name, and signal_information_item which is one of the following: CLASS_ORIGIN, SUBCLASS_ORIGIN, MESSAGE_TEXT, MYSQL_ERRNO, CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, CATALOG_NAME, SCHEMA_NAME, TABLE_NAME, COLUMN_NAME, CURSOR_NAME
Following procedure accepts the short form of the degrees and returns the full forms of them. If we pass a value other than B-Tech, M-Tech, BSC, MSC it generates an error message.
Here we are using the SIGNAL Statement to generate the error message −
DELIMITER //
CREATE PROCEDURE example(IN degree VARCHAR(20), OUT full_form Varchar(50))
   BEGIN
      IF degree='B-Tech' THEN SET full_form = 'Bachelor of Technology'; 
      ELSEIF degree='M-Tech' THEN SET full_form = 'Master of Technology'; 
      ELSEIF degree='BSC' THEN SET full_form = 'Bachelor of Science';
      ELSEIF degree='MSC' THEN SET full_form = 'Master of Science';
      ELSE
         SIGNAL SQLSTATE '01000'
      SET MESSAGE_TEXT = 'Choose from the existing values', MYSQL_ERRNO = 12121;
         SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'Given degree is not valid', MYSQL_ERRNO = 1001;
      END IF;
   END //
DELIMITER ;
You can call the above procedure as shown below −
CALL example('BSC', @fullform);
You can retrieve the value of the variable using SELECT statement −
mysql> SELECT @fullform;
+---------------------+
| @fullform           |
+---------------------+
| Bachelor of Science |
+---------------------+
1 row in set (0.00 sec)

If you pass an invalid value to the procedure, it will generate an error message as follow −
mysql> CALL procedureEx ('BBC', @fullform);
ERROR 1001 (45000): Given degree is not valid
Following is another example demonstrating the usage of the SIGNAL Statement −
DELIMITER //
CREATE PROCEDURE example (num INT)
   BEGIN
      DECLARE testCondition CONDITION FOR SQLSTATE '45000';
      IF num < 0 THEN
         SIGNAL SQLSTATE '01000';
      ELSEIF num > 150 THEN
         SIGNAL SQLSTATE '45000';
      END IF;
   END //
DELIMITER ;
You can call the above procedure by passing two values as shown below −
mysql> DELIMITER ;
mysql> CALL example(15);
Query OK, 0 rows affected (0.00 sec)

mysql> CALL example(160);
ERROR 1644 (45000): Unhandled user-defined exception condition

    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