MySQL触发器插入另一个表

时间:2010-08-26 14:49:20

标签: mysql

语法方面,以下查询有什么问题:

-- Trigger DDL Statements
DELIMITER $$

USE `mydb`$$

DROP TRIGGER IF EXISTS annual_scenario;

CREATE TRIGGER annual_scenario 
AFTER INSERT ON quote FOR EACH ROW
BEGIN
  SET @annual_date = (SELECT  NEW.date - INTERVAL 1 YEAR);
  SET @annual =  (SELECT COUNT(DISTINCT Q.date)
                  FROM quote Q, instrument I
                  WHERE Q.date = I.date 
                    AND Q.ticker = I.date
                    AND Q.market = I.date
                    AND I.type != 'bank'
                    AND Q.date =  @annual_date
                    AND Q.ticker = NEW.ticker
                    AND Q.market = NEW.market
                  );
   SET @annual_close = (SELECT Q.close
                        FROM quote Q
                        WHERE Q.ticker = NEW.ticker 
                          AND Q.date = @annual_date
                          AND Q.market = NEW.market
                        );

   SET @ratio = (NEW.close / @annual_close);

    IF @annual = 1 THEN
      INSERT INTO 
        scenario(period,ticker,date_1,date_2,close_1,close_2,ratio)
        VALUES('annual',NEW.ticker,NEW.date,@annual_date,NEW.close,@annual_close,@ratio);
    ENDIF 
END $$
DELIMITER ; 

它输出以下错误:

Error Code: 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 'CREATE TRIGGER annual_scenario 
AFTER INSERT ON quote FOR EACH ROW
BEGIN
  SE' at line 2

我要做的是检查年度回报1是否会在表格场景中插入一条带有相应值的新记录。

1 个答案:

答案 0 :(得分:2)

显然MySQL 5.1不使用ENDIF,而是使用END IF。更不用说失踪;在END IF和END之后。

Thanx无论如何你的意见:)