Unable to use SIGNAL and INSERT at the same time

隐身守侯 提交于 2020-03-06 03:32:05

问题


I have a problem with this trigger. When I use the INSERT and the SIGNAL statements inside the IF condition, the insertion is not performed. However, without SIGNAL, the insertion is done. Anyone have an explanation for that? My main concern is that I need both the insertion and the SIGNAL statement to cancel the main insertion (the insertion that throws the trigger)

    DELIMITER //
CREATE TRIGGER log_venta BEFORE INSERT ON `venta_producto`
FOR EACH ROW BEGIN
    DECLARE value int;
    DECLARE valor_venta int;
    DECLARE saldo_cliente int;
    DECLARE cliente_id int;

    SELECT cliente INTO cliente_id FROM venta WHERE id=NEW.ventaID;
    SELECT credito INTO saldo_cliente FROM cliente WHERE no_cliente=cliente_id;
    SELECT precio INTO valor_producto FROM producto WHERE id=NEW.producto;

    SET valor_venta = NEW.cantidad*valor_producto;

    IF valor_venta > saldo_cliente THEN
      INSERT INTO log(cliente) VALUES (cliente_id);
      SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
    END IF; 
END
//
DELIMITER ;

Thanks


回答1:


14.6.7.5 SIGNAL Syntax

. . .

SIGNAL is the way to “return” an error.

. . .


21.3.1 Trigger Syntax and Examples

. . .

  • For transactional tables, failure of a statement should cause rollback of all changes performed by the statement. Failure of a trigger causes the statement to fail, so trigger failure also causes rollback. For nontransactional tables, such rollback cannot be done, so although the statement fails, any changes performed prior to the point of the error remain in effect.

. . .

Mentioned above can be demonstrated in the following example:

mysql> DROP TABLE IF EXISTS `venta_producto`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `log`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `venta_producto` (
    ->   `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ->   `cliente_id` INT UNSIGNED
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE IF NOT EXISTS `log` (
    ->   `cliente_id` INT UNSIGNED
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE TRIGGER `log_venta` BEFORE INSERT ON `venta_producto`
    -> FOR EACH ROW
    -> BEGIN
    ->   INSERT INTO `log` (`cliente_id`) VALUES (NEW.`cliente_id`);
    ->   SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> INSERT INTO `venta_producto`
    ->   (`cliente_id`)
    -> VALUES
    ->   (1);
ERROR 1643 (02000): ERROR

mysql> SELECT
    ->   `id`,
    ->   `cliente_id`
    -> FROM
    ->   `venta_producto`;
Empty set (0.00 sec)

mysql> SELECT
    ->   `cliente_id`
    -> FROM
    ->   `log`;
Empty set (0.00 sec)

mysql> ALTER TABLE `log` ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> INSERT INTO `venta_producto`
    ->   (`cliente_id`)
    -> VALUES
    ->   (2);
ERROR 1643 (02000): ERROR

mysql> SELECT
    ->   `id`,
    ->   `cliente_id`
    -> FROM
    ->   `venta_producto`;
Empty set (0.00 sec)

mysql> SELECT
    ->   `cliente_id`
    -> FROM
    ->   `log`;
+------------+
| cliente_id |
+------------+
|          2 |
+------------+
1 row in set (0.00 sec)


来源:https://stackoverflow.com/questions/37062106/unable-to-use-signal-and-insert-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!