2016-03-31 133 views
0
DELIMITER // 
CREATE TRIGGER `enclosure-after_update-time` AFTER UPDATE ON `enclosure` 
FOR EACH ROW BEGIN 
    IF NEW.time <> OLD.time THEN 
     DECLARE done INT DEFAULT FALSE; 
     DECLARE ids INT; 
     DECLARE invoice_ids INT; 
     DECLARE cur CURSOR FOR SELECT id AS ids, invoice_id AS invoice_ids FROM accounting WHERE enclosure_id = NEW.id; 
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 

     OPEN cur; 
      ins_loop: LOOP 
       FETCH cur INTO ids, invoice_ids; 
       IF done THEN 
        LEAVE ins_loop; 
       END IF; 
       UPDATE accounting SET time=NEW.time WHERE id=ids; 
       IF invoice_ids THEN 
        UPDATE invoice SET time=NEW.time WHERE id=invoice_ids; 
       END IF; 
      END LOOP; 
     CLOSE cur; 
    END IF; 
END 
// 
DELIMITER ; 

錯誤

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE done INT DEFAULT FALSE; 
     DECLARE ids INT; 
     DECLARE invoi' at line 4 
+0

嘗試在一開始聲明的變量(前'如果') –

回答

0

在聲明變量一開始

DELIMITER // 
CREATE TRIGGER `enclosure-after_update-time` AFTER UPDATE ON `enclosure` 
FOR EACH ROW BEGIN 
    DECLARE done INT DEFAULT FALSE; 
    DECLARE ids INT; 
    DECLARE invoice_ids INT; 
    DECLARE cur CURSOR FOR SELECT id, invoice_id INTO ids, invoice_ids FROM accounting WHERE enclosure_id = NEW.id; 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 

    IF NEW.time <> OLD.time THEN 
     OPEN cur; 
      ins_loop: LOOP 
       FETCH cur INTO ids, invoice_ids; 
       IF done THEN 
        LEAVE ins_loop; 
       END IF; 
       UPDATE accounting SET time=NEW.time WHERE id=ids; 
       IF invoice_ids THEN 
        UPDATE invoice SET time=NEW.time WHERE id=invoice_ids; 
       END IF; 
      END LOOP; 
     CLOSE cur; 
    END IF; 
END 
// 
DELIMITER ; 
+0

然後我得到'#1323 - 遊標SELECT不能有INTO' – clarkk

+0

它替換SELECT'INTO ...'後用'SELECT column AS alias' – clarkk