Home >Database >Mysql Tutorial >How to handle an error in mysql
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.
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;
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;
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';
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';
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;
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;
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;
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!