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
LC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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??
ASKER
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
[IBM][CLI Driver][DB2/6000] SQL0104N An unexpected token "exists" was found following "if not ". Expected tokens may include: "JOIN". SQLSTATE=42601