Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Subject:- Database Management Systems (310242)

Error Handling
Error Handling
 When error is raised in executing procedure it need to be handled.
 MySql facilitates handler which can hanlde warning as well errors in
procedure.
Declaring a handler
DECLARE action HANDLER FOR condition_value statement;

 If a condition whose value matches the condition_value , MySQL will execute


the statement and continue or exit the current code block based on the action .
The action accepts one of the following values:

 CONTINUE : The execution of the enclosing code block ( BEGIN … END )


continues.
 EXIT : the execution of the enclosing code block, where the handler is declared,
terminates.
The condition_value specifies a particular condition or a class of conditions that
activate the handler. The condition_value accepts one of the following values:

 A MySQL error code.


 A standard SQLSTATE value. Or it can be an SQLWARNING , NOTFOUND or
SQLEXCEPTION condition, which is shorthand for the class of SQLSTATE
values. The NOTFOUND condition is used for a cursor or SELECT INTO
variable_list statement.
 A named condition associated with either a MySQL error code or SQLSTATE
value.
The statement could be a simple statement or a compound statement enclosing by the
BEGIN and END keywords.
SQLEXCEPTION
create procedure na(in nm int, in str varchar(150))
begin
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
insert into people1 values(nm,str);
--ROLLBACK;
SELECT 'An error has occurred, operation rollbacked and the stored procedure was
terminated';
END;
end;
NOT FOUND(cursor,SELECT
INTO variable_list)
CREATE PROCEDURE test()
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_email varchar(100) DEFAULT "";
DECLARE email_list varchar(100) DEFAULT "";
DEClARE email_cursor CURSOR FOR SELECT name FROM people;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN email_cursor;
get_email: LOOP
FETCH email_cursor INTO v_email;
IF v_finished = 1 THEN
LEAVE get_email;
END IF; -- build email list
SET email_list = CONCAT(v_email,";",email_list);
END LOOP get_email;
select email_list;
CLOSE email_cursor; END $$
mysql> select * from people
$$
+-------+-----+
| name | age |
+-------+-----+
| suhas | 10 |
| sf | 12 |
| ram | 45 |
+-------+-----+
3 rows in set (0.00 sec)
SQLEXCEPTION
create procedure na(in nm int, in str varchar(150))
begin
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
insert into people1 values(nm,str);
--ROLLBACK;
SELECT 'An error has occurred, operation rollbacked and the stored procedure was
terminated';
END;
end;
mysql> select * from people
$$
+-------+-----+
| name | age |
+-------+-----+
| suhas | 10 |
| sf | 12 |
| ram | 45 |
+-------+-----+
3 rows in set (0.00 sec)
SQLEXCEPTION
CREATE PROCEDURE in2(IN age INT, IN name varchar(100))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'SQLException invoked';
DECLARE EXIT HANDLER FOR 1062
SELECT 'MySQL error code 1062 invoked';
-- 1062 duplicate primary key
DECLARE EXIT HANDLER FOR SQLSTATE '23000'
SELECT 'SQLSTATE 23000 invoked';
-- insert a new record into article_tags
INSERT INTO people(name,age)
VALUES(name,age);
-- return tag count for the people
SELECT COUNT(*) FROM people;
END
mysql> call in2(10,'ggg') $$
+-------------------------------+
| MySQL error code 1062 invoked |
+-------------------------------+
| MySQL error code 1062 invoked |
+-------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00


sec)
mysql> call in2(11,'ggg') $$
+----------+
| COUNT(*) |
+----------+
| 4|
+----------+
1 row in set (0.06 sec)
Query OK, 0 rows affected (0.06 sec)
mysql> select * from people $$
+-------+-----+
| name | age |
+-------+-----+
| suhas | 10 |
| ggg | 11 |
| sf | 12 |
| ram | 45 |
+-------+-----+ Referance:-http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/
4 rows in set (0.00 sec)

You might also like