0

在下面的MYSQL存储过程中,每当由于数据截断而出现警告时,都会显示警告。但它不会回滚任何东西。WARNING 的 EXIT 处理程序完全被忽略。请帮忙。

DELIMITER $$

DROP PROCEDURE IF EXISTS `saveDetails` $$
CREATE DEFINER=`dbuser`@`%` PROCEDURE `saveDetails`(IN param_name VARCHAR(200))
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
    DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
    START TRANSACTION;
        UPDATE userDetails SET displaystatus = 2 where displaystatus = 1;
        UPDATE userDetails SET displaystatus = 1 where displaystatus = 0;
        UPDATE userDetails SET name = param_name;
    COMMIT;

END $$

DELIMITER ;

我也尝试了以下版本,但这也不起作用:

分隔符 $$

DROP PROCEDURE IF EXISTS `saveDetails` $$
CREATE DEFINER=`dbuser`@`%` PROCEDURE `saveDetails`(IN param_name VARCHAR(200))
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING  
    BEGIN 
        ROLLBACK;
    END;
    START TRANSACTION;
        UPDATE userDetails SET displaystatus = 2 where displaystatus = 1;
        UPDATE userDetails SET displaystatus = 1 where displaystatus = 0;
        UPDATE userDetails SET name = param_name;
    COMMIT;

END $$

DELIMITER ;
4

1 回答 1

0

有一个非常简单的例子:

/*Table structure for table `mytable` */

DROP TABLE IF EXISTS `mytable`;

CREATE TABLE `mytable` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `value` INT(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB;

/*Data for the table `mytable` */

INSERT  INTO `mytable`(`value`) VALUES (100);

/* Procedure structure for procedure `saveDetails` */

/*!50003 DROP PROCEDURE IF EXISTS  `saveDetails` */;

DELIMITER $$

CREATE PROCEDURE `saveDetails`()
BEGIN
    DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
    START TRANSACTION;
    UPDATE `mytable` SET `value` = 'A' WHERE `id` = 1;
    COMMIT;
END$$

DELIMITER ;

一切都按预期工作:

mysql> SELECT `id`, `value` FROM `mytable`;
+----+-------+
| id | VALUE |
+----+-------+
|  1 |   100 |
+----+-------+
1 ROW IN SET (0.00 sec)

mysql> CALL saveDetails;
QUERY OK, 0 ROWS affected (0.02 sec)

mysql> SELECT `id`, `value` FROM `mytable`;
+----+-------+
| id | VALUE |
+----+-------+
|  1 |   100 |
+----+-------+
1 ROW IN SET (0.02 sec)

你可以显示:SHOW CREATE TABLE userDetails;

于 2013-11-13T11:33:40.513 回答