select - MySQL trigger with if statement trying to query a different table for value to be inserted -
i setting triggers if there update on table value trigger insert fields changelog table. on original update have foreign keys setup keys changed value not used @ all.
the following sql , keep getting syntax error:
drop trigger if exists `history_hosts_modify`; delimiter // create trigger `history_hosts_modify` before update on `hosts` each row begin if old.hostname != new.hostname insert changelogs (cid, remoteid, cat, action, oldval, newval, modified) values(null, old.id, 'host', 'mod', old.hostname, new.hostname, now()); end if; if old.clients_id != new.clients_id declare temp1, temp2 int; select client clients id=old.clients_id temp1; select client clients id=new.clients_id temp2; insert changelogs (cid, remoteid, cat, action, oldval, newval, modified) values(null, old.id, 'host', 'mod', temp1, temp2, now()); end if; end // delimiter ;
can please me?
...
declare permitted inside begin ... end compound statement , must @ start, before other statements.
...
try:
drop trigger if exists `history_hosts_modify`; delimiter // create trigger `history_hosts_modify` before update on `hosts` each row begin declare temp1, temp2 int; if old.hostname != new.hostname insert changelogs (cid, remoteid, cat, action, oldval, newval, modified) values (null, old.id, 'host', 'mod', old.hostname, new.hostname, now()); end if; if old.clients_id != new.clients_id -- declare temp1, temp2 int; select client clients id=old.clients_id temp1; select client clients id=new.clients_id temp2; insert changelogs (cid, remoteid, cat, action, oldval, newval, modified) values (null, old.id, 'host', 'mod', temp1, temp2, now()); end if; end// delimiter ;
Comments
Post a Comment