i have some gps modules that send data to my database. On update of the last package sent i want to compare new values with old values and insert into a new table only those values that are diferent.

CREATE TRIGGER upd_check AFTER UPDATE ON coordonates
       IF (OLD.lattitude <> NEW.lattitude ) or (OLD.longitude<> NEW.longitude) 
       OR (OLD.battery_lvl <> NEW.battery_lvl) or (OLD.status <> NEW.status)
         INSERT INTO LogTable(ID, lattitude,longitude, battery_lvl,status) 
         VALUES (NEW.ID, NEW.lattitude,NEW.longitude,NEW.battery_lvl,NEW.status);
       END IF;

but this is not good because i want to insert those new values only if they are different from old value, right now i insert all of them ...

  • Which RDBMS? They are likely to be different. – Rick James Oct 23 at 1:00
  • For SQL server you must use INSTEAD OF trigger and re-insert if not equal. For MySQL you must use BEFORE trigger and generate duplicate error 3822 if equal. – Akina Oct 23 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.