两个数据库之间的MySQL触发器

时间:2018-12-04 14:17:31

标签: mysql sql database-trigger

我在两个数据库中有两个表。一个站点用于客户,一个站点用于会员,因此我记录了两个站点。我使用MySQL触发器,但收到错误

  

“无法更新存储的函数/触发器中的表product_option_value   因为它已被语句使用...”

我在testkosmeb_adtest上更新数量。product_option_value。 (数据库2)

出什么问题了,我该如何解决?

我的触发器:

在数据库1上

DELIMITER $$    
CREATE TRIGGER `before_update_option_quantity`
    BEFORE UPDATE ON `testkosmeb_adcom`.`product_option_value`
    FOR EACH ROW
BEGIN
    DECLARE newquantity INT default 0;
    DECLARE khacctv INT default 0;
    SET newquantity = NEW.quantity - NEW.ctv_quantity;
    SET khacctv = NEW.ctv_quantity - OLD.ctv_quantity;
    IF newquantity < 0 AND khacctv = 0 THEN
        SET NEW.ctv_quantity = NEW.quantity;
    END IF;

    IF NEW.ctv_quantity <> OLD.ctv_quantity AND NEW.update_trig <> 1 THEN
        UPDATE `testkosmeb_adtest`.`product_option_value`
        SET quantity = NEW.ctv_quantity,
        update_trig = 1
        WHERE product_option_value_id = NEW.product_option_value_id;
    END IF;
    SET NEW.update_trig = 0;

END$$
DELIMITER ;




-- Xóa quantity
DROP TRIGGER IF EXISTS before_delete_option_quantity;
DELIMITER $$    
CREATE TRIGGER `before_delete_option_quantity`
    AFTER UPDATE ON `testkosmeb_adcom`.`product_option_value`
    FOR EACH ROW
BEGIN
    DELETE FROM `testkosmeb_adtest`.`product_option_value`
    WHERE product_option_value_id = OLD.product_option_value_id;

END$$
DELIMITER ;


-- THÊM quantity
DROP TRIGGER IF EXISTS before_insert_option_quantity;

DELIMITER $$    
CREATE TRIGGER `before_insert_option_quantity`
    AFTER INSERT ON `testkosmeb_adcom`.`product_option_value`
    FOR EACH ROW
BEGIN
    INSERT INTO `testkosmeb_adtest`.`product_option_value` SET
    product_option_value_id = NEW.product_option_value_id,
    product_id = NEW.product_id,
    option_id = NEW.option_id,
    update_trig = 1,
    option_value_id = NEW.option_value_id,
    sku = NEW.sku,
    quantity = NEW.ctv_quantity,
    subtract = NEW.subtract,
    price = NEW.price,
    price_prefix = NEW.price_prefix,
    points = NEW.points,
    weight = NEW.weight,
    weight_prefix = NEW.weight_prefix,
    costing_method = NEW.costing_method,
    cost_amount = NEW.cost_amount,
    cost = NEW.cost,
    cost_prefix = NEW.cost_prefix;

END$$
DELIMITER ;

在数据库2上

DROP TRIGGER IF EXISTS before_update_option_quantity_ctv;
DELIMITER $$    
CREATE TRIGGER `before_update_option_quantity_ctv`
    BEFORE UPDATE ON `testkosmeb_adtest`.`product_option_value`
    FOR EACH ROW
BEGIN
    DECLARE thaydoi INT default 0;
    SET thaydoi = NEW.quantity - OLD.quantity;

    IF thaydoi <> 0  AND NEW.update_trig <> 1 THEN

    UPDATE `testkosmeb_adcom`.`product_option_value`
    SET quantity = quantity + thaydoi,
    ctv_quantity = ctv_quantity + thaydoi,
    update_trig = 1
    WHERE product_option_value_id = NEW.product_option_value_id;
    END IF;
    SET NEW.update_trig = 0;

END$$
DELIMITER ;

0 个答案:

没有答案