带你轻松接触MySQL数据库的异常处理
2008-09-03 11:08:18 来源:WEB开发网这个列没有默认值,插入的时候会出现警告或者1364错误提示。
mysql> DELIMITER $$
mysql> drop PROCEDURE IF exists `t_girl`.`handlerdemo`$$
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create DEFINER=`root`@`localhost` PROCEDURE `handlerdemo`()
-> BEGIN
-> DECLARE CONTINUE HANDLER FOR 1062 BEGIN END;
-> DECLARE CONTINUE HANDLER FOR SQLWARNING
-> BEGIN
-> update t set s2 = 2;
-> END;
-> DECLARE CONTINUE HANDLER FOR 1364
-> BEGIN
-> insert INTO t(s1,s2) VALUES (1,3);
-> END;
-> SET @x = 1;
-> insert INTO t(s1) VALUES (1);
-> SET @x = 2;
-> insert INTO t(s1) VALUES (1);
-> SET @x = 3;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> call handlerdemo();
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t;
+----+----+
| s1 | s2 |
+----+----+
| 1 | 3 |
+----+----+
1 row in set (0.00 sec)
遇到错误时,插入的新记录。
mysql> select @x;
+------+
| @x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
更多精彩
赞助商链接