Solved

QUERY to update if exists and insert otherwise

Posted on 2003-11-19
6
569 Views
Last Modified: 2012-06-27
How to do I create a query that will update a record if it exists and insert it if the record does not?
LC
0
Comment
Question by:chambers777
6 Comments
 
LVL 32

Accepted Solution

by:
jadedata earned 50 total points
Comment Utility
Hey chambers777!

 You can't do both with the same query.
 You need to set each up separately.

regards
Jack
0
 
LVL 3

Expert Comment

by:taisk
Comment Utility
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.
0
 

Author Comment

by:chambers777
Comment Utility
i'm using a db2
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Expert Comment

by:Callandor
Comment Utility
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
0
 
LVL 32

Expert Comment

by:jadedata
Comment Utility
did it work??
0
 

Author Comment

by:chambers777
Comment Utility
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
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now