MySQL触发器的语法问题

时间:2014-03-30 14:46:13

标签: mysql sql triggers

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;$$

它返回错误

  

“#1064 - 您的SQL语法出错;请查看与您的MySQL服务器版本对应的手册,以便在第14行'$$'附近使用正确的语法”

请告诉我,代码有什么问题..

1 个答案:

答案 0 :(得分: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 ;