WEB开发网
开发学院数据库MySQL 带你轻松接触MySQL数据库的异常处理 阅读

带你轻松接触MySQL数据库的异常处理

 2008-09-03 11:08:18 来源:WEB开发网   
核心提示: 遇到错误继续的情况 mysql> truncate table t;Query OK, 0 rows affected (0.01 sec)mysql> DELIMITER $$mysql> drop PROCEDURE IF exists `t_girl`.`handl

遇到错误继续的情况

  mysql> truncate table t;
  Query OK, 0 rows affected (0.01 sec)
  mysql> DELIMITER $$
  mysql> drop PROCEDURE IF exists `t_girl`.`handlerdemo`$$
  Query OK, 0 rows affected (0.00 sec)
  mysql> create DEFINER=`root`@`localhost` PROCEDURE `handlerdemo`()
  -> BEGIN
  -> DECLARE CONTINUE HANDLER FOR SQLSTATE ’23000’ BEGIN END;
  -> SET @x = 1;
  -> insert INTO t VALUES (1);
  -> SET @x = 2;
  -> insert INTO t VALUES (1);
  -> SET @x = 3;
  -> END$$
  Query OK, 0 rows affected (0.01 sec)
  mysql> DELIMITER ;
  mysql> call handlerdemo();
  Query OK, 0 rows affected (0.00 sec)
  mysql> select @x;
  +------+
  | @x |
  +------+
  | 3 |
  +------+
  1 row in set (0.00 sec)
  mysql> call handlerdemo();
  Query OK, 0 rows affected (0.00 sec)
  mysql> select @x;
  +------+
  | @x |
  +------+
  | 3 |
  +------+
  1 row in set (0.00 sec)
  mysql>

我们可以看到,始终执行到最后。

当然,上面的SQLSTATE ’23000’可以替换为1062。

警告:

  mysql> alter table t add s2 int not null;
  Query OK, 0 rows affected (0.01 sec)
  Records: 0 Duplicates: 0 Warnings: 0

Tags:轻松 接触 MySQL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接