Link to home
Start Free TrialLog in
Avatar of chambers777
chambers777

asked on

QUERY to update if exists and insert otherwise

How to do I create a query that will update a record if it exists and insert it if the record does not?
LC
ASKER CERTIFIED SOLUTION
Avatar of jadedata
jadedata
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taisk
taisk

What database is this?  If it is Oracle9i, you CAN do update and insert in one query.  Otherwise, chambers777 is right, you need separate statements.
Avatar of chambers777

ASKER

i'm using a db2
How about

if not exists (select * from myTable where id = recordId)
begin insert into myTable values (value1, value2, ....) end
else
begin update myTable set field1=value1 where id = recordId end
did it work??
I get this error:
 [IBM][CLI Driver][DB2/6000] SQL0104N  An unexpected token "exists" was found following "if not ".  Expected tokens may include:  "JOIN".  SQLSTATE=42601