Home > Database > Mysql Tutorial > body text

How to handle an error in mysql

coldplay.xixi
Release: 2020-10-23 09:31:08
Original
2999 people have browsed it

Mysql error processing method: 1. If a 1062 error occurs, execute the SELECT statement; 2. When the SQL select statement does not return a value, set [l_done=1] and continue execution; 3. After an error message is generated Exit the current block or stored procedure; 4. If there is any error, set [l_error] to 1 and then execute.

How to handle an error in mysql

Mysql error handling method:

1. If there is any error (not NOT FOUND), set l_error Continue execution after being 1:

DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
Copy after login

2. If any error occurs (not NOT FOUND), execute ROLLBACK and generate an error message before exiting the current block or stored procedure.

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
Copy after login

3. If MySQL 1062 error (duplicate key value) occurs, execute the SELECT statement (send a message to the calling program) and then continue execution

DECLARE CONTINUE HANDER FOR 1062
SELECT 'Duplicate key in index';
Copy after login

4. If SQLSTATE 2300 error (duplicate key value) health value) occurs, execute the SELECT statement (send a message to the calling program) and continue execution

DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
Copy after login

5. When the cursor or SQL select statement does not return a value, set l_done=1 and continue execution

DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
Copy after login

6. This example is the same as the previous example except that it uses the SQLSTATE variable instead of the named condition

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
Copy after login

7. This example uses the MySQL error code variable instead of the named condition or SQLSTATE variable. The two examples are the same

DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;
Copy after login

More related free learning recommendations:mysql tutorial(Video)

The above is the detailed content of How to handle an error in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template