DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   mySQL TRIGGER problem (http://www.daniweb.com/forums/thread123166.html)

M3nTaL May 8th, 2008 6:32 am
mySQL TRIGGER problem
 
Hello,

I'm having some sintax problems using PL/SQL in mySQL TRIGGERS.. I need some help please :).
Here is the code:

CREATE TRIGGER modificou
  AFTER UPDATE ON monitor
  FOR EACH ROW
 BEGIN
  IF NOT EXISTS(SELECT estado_tmp FROM alteracoes WHERE id_tmp = OLD.id)
  THEN
      INSERT INTO alteracoes VALUES(OLD.id,NEW.estado);
  ELSE
      UPDATE alteracoes SET estado_tmp = NEW.estado WHERE id_tmp = OLD.id;
  END IF;
 END modificou;

What could be wrong?? For testing I'm using phpmyadmin 2.11.1

Thanks

tesuji May 9th, 2008 12:00 pm
Re: mySQL TRIGGER problem
 
hi,
what is the error message?

Your trigger will be fired AFTER update, and, obviously, it were to catch update errors IF the record to be updated does not exist in your table. Well, if that record does not exist, update would not be execute, therefore (i believe so:)) AFTER-update trigger will never be fired.

If you want to prevent failure, if a record to be inserted already exists (that would lead into duplicate primary key), you might use
INSERT INTO table (...) VALUES (...) ON DUPLICATE KEY UPDATE ....
krs,
tesu


All times are GMT -4. The time now is 4:57 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC