tags:

views:

17

answers:

2

When I try create a trigger as given below,

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;

I get following error,

ERROR 1064 (42000): 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 'FROM FiscalYearTable1.start_date) != FiscalYearTable1.fiscal_year - 1) OR (EXTRA' at line 1

I can't figure out what the error is. Any ideas? Thanks

A: 

Never mind, just created a similar table and my suggestion didn't work

Nick
i tried it after you mentioned but it still gives same error.
robert
A: 

something like this:

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 ;
f00
ok.. that works gr8.. but i wonder what is problem with extract? Any ideas?
robert
if..then..end if; syntax error possibly - your original code is a little hard to read. if you think my solution works for you could you accept the answer pls - thanks :)
f00