2014-06-29 112 views
0

這是我目前的觸發:需要幫助定製的mysql觸發

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

我試着使用MySQL如果但沒有成功。

另外,它有點自動增量,即使有一個活動id已經(當它忽略)。任何方式我可以阻止這種情況發生?

回答

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 
+0

我確實是,但看起來並不合適。非常感謝 –