代码之家  ›  专栏  ›  技术社区  ›  robert

使用extract函数创建触发器时出现mysql错误

  •  0
  • robert  · 技术社区  · 14 年前

    当我尝试创建如下所示的触发器时,

    CREATE TRIGGER FiscalYearTable1_bi 
    BEFORE INSERT  
    ON FiscalYearTable1 
    FOR EACH ROW  
         IF ( 
                 ( EXTRACT (YEAR FROM FiscalYearTable1.start_date) !=  FiscalYearTable1.fiscal_year - 1) OR
                (EXTRACT (MONTH FROM FiscalYearTable1.start_date) != 04) OR
                (EXTRACT (DAY FROM FiscalYearTable1.start_date) != 01) 
           ) 
              SET FiscalYearTable1.fiscal_year = 1/0;
    

    错误1064(42000):您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获得使用“FROM FiscalYearTable1.start\u date)”附近的正确语法!=FiscalYearTable1.1财年)或(第1行额外)

    1 回复  |  直到 13 年前
        1
  •  0
  •   Jon Black    14 年前

    像这样:

    delimiter #
    
    create trigger FiscalYearTable1_before_ins_trig before insert on FiscalYearTable1
    for each row
    begin
    
    declare y smallint unsigned default 0;
    declare m tinyint unsigned default 0;
    declare d tinyint unsigned default 0;
    
      set y = year(new.start_date);
      set m = month(new.start_date);
      set d = day(new.start_date);
    
      -- whatever logic you require...
      if y != new.fiscal_year-1 or m != 4 or d != 1 then
        set new.fiscal_year = null; 
      end if;
    
    end#
    
    delimiter ;