需要帮助自定义mysql触发器

时间:2014-06-29 14:31:24

标签: mysql triggers

这是我目前的触发器:

DELIMITER $$
CREATE DEFINER=`root`@`127.0.0.1` TRIGGER `unique_visit_new_campaign` AFTER INSERT ON `unique_visit` FOR EACH ROW
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE pixel_id int;
    DECLARE campaign_id varchar(45);
    DECLARE cur CURSOR FOR SELECT id FROM pixels;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    OPEN cur;
            ins_loop: LOOP
                FETCH cur INTO pixel_id;
                IF done THEN
                    LEAVE ins_loop;
                END IF;
                INSERT IGNORE INTO pixels_campaign (campaign_id , pixel_id , date) VALUES (NEW.campaign_id ,pixel_id, current_timestamp);
            END LOOP;
        CLOSE cur;
END

当new.campaign_id为空或等于字符串{campaign_id}

时,我需要 NOT TRIGGER

我尝试使用MySQL IF但没有成功。

此外,即使已经有一个广告系列ID(忽略它),它也会自动增加。我能以何种方式阻止这种情况发生?

1 个答案:

答案 0 :(得分:1)

你试过这个吗?

DELIMITER $$
CREATE DEFINER=`root`@`127.0.0.1` TRIGGER `unique_visit_new_campaign` AFTER INSERT ON `unique_visit` FOR EACH ROW
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE pixel_id int;
    DECLARE campaign_id varchar(45);
    DECLARE cur CURSOR FOR SELECT id FROM pixels;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    IF new.campaign = '' or new.campaign = '{campaign_id}' or new.campaign is null THEN

        OPEN cur;
                ins_loop: LOOP
                    FETCH cur INTO pixel_id;
                    IF done THEN
                        LEAVE ins_loop;
                    END IF;
                    INSERT IGNORE INTO pixels_campaign (campaign_id , pixel_id , date) VALUES (NEW.campaign_id ,pixel_id, current_timestamp);
                END LOOP;
            CLOSE cur;
    END IF

END