2014-03-30 105 views
0
DELIMITER $$ 

CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand` 
FOR EACH ROW 
BEGIN 
    DECLARE bill_no INT DEFAULT 0; 

    IF NEW.bill_no = 0 THEN 
     SET bill_no = (SELECT bill_no FROM cnitrand 
         WHERE (tx_no = NEW.tx_no AND 
           input_area = NEW.input_area AND 
           tx_line_no = NEW.tx_line_no AND 
           bill_no <> "") OR 
          (tx_no = NEW.tx_no AND 
           bill_no <> "") 
         LIMIT 1); 
    ELSE 
     SET bill_no = NEW.bill_no; 
    END IF; 

    INSERT INTO tblrptcnitrandmap 
    (tx_no, input_area, tx_line_no, bill_cust_code, bill_no) 
    VALUES 
    (NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no); 

END;$$ 

它返回一個錯誤語法問題隨着MySQL的觸發

「#1064 - 你在你的SQL語法錯誤;檢查對應於您的MySQL服務器版本的使用權語法手冊近'$$'在第14" 行

能否請你告訴我,什麼是錯的代碼..

回答

0
DELIMITER $$ 

CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand` 
FOR EACH ROW 
BEGIN 
    DECLARE bill_no INT DEFAULT 0; 

    IF NEW.bill_no = 0 THEN 
     SET bill_no = (SELECT bill_no FROM cnitrand WHERE (tx_no = NEW.tx_no AND input_area = NEW.input_area AND tx_line_no = NEW.tx_line_no AND bill_no <> "") OR (tx_no = NEW.tx_no AND bill_no <> "") LIMIT 1); 
    ELSE 
     SET bill_no = NEW.bill_no; 
    END IF; 

    INSERT INTO tblrptcnitrandmap (tx_no, input_area, tx_line_no, bill_cust_code, bill_no) VALUES (NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no); 

END$$ 
DELIMITER ; 
+0

爲什麼你認爲'END; $$'錯了,'END $$ DELIMITER;'是正確的? –

+0

請參閱[*存儲過程 - MySQL *](http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.pdf) –