You could try a stored procedure that either inserts a new row or
updates an existing row:
CREATE PROCEUDRE InsertOrUpdateRecord(IN NewID INT, ... other params ...
IF NOT EXISTS (SELECT ID FROM myTable WHERE ID = NewID) THEN
INSERT INTO myTable (....)
UPDATE myTable SET .....
Secure Enterprise Technology Initiatives
Microsoft Implementation Group
Virginia Tech Information Technology
From: Douglas Pearson [mailto:biz@stripped]
Sent: Friday, April 13, 2007 2:14 AM
Subject: Is it possible to either update or insert in a single query?
Apologies if this is a dumb question, but is it possible to write a
query that either updates certain columns in a row, or adds an entirely
row if there is none already?
I seem to be running into this a lot, and so far I've solved it by:
1) run UPDATE table SET x,y WHERE some row
2) if rowsChanged == 0 then run the INSERT
It just feels like there must be a way to do this more efficiently.
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql