插入MySQL触发器后更新值或在另一个表中插入新行

时间:2017-01-29 12:15:59

标签: mysql

我有两张桌子,

QuerySetManager

RawFeed , SellerList. 中插入新行时,如果RawFeed中不存在相应的SellerList,我需要在SellerID中插入一行,

否则,我需要在SellerList表中为该SellerList增加两个列值。

这是我的触发代码 -

SellerID

我收到此错误 -

CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
    BEGIN
        --CHECK SELLER ID EXISTS OR NOT.
        DECLARE SELLEREXISTS INT;
        SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;

        IF SELLEREXISTS=0 THEN
            --INSERT ROW
            INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
        ELSE
            --UPDATE ROW
            UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
        END IF;
    END

1 个答案:

答案 0 :(得分:1)

尝试更改分隔符

DELIMITER $$
CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
    BEGIN
        --CHECK SELLER ID EXISTS OR NOT.
        DECLARE SELLEREXISTS INT;
        SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;

        IF SELLEREXISTS=0 THEN
            --INSERT ROW
            INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
        ELSE
            --UPDATE ROW
            UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
        END IF;
    END
$$
DELIMITER ;