Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

QUERY to update if exists and insert otherwise

Posted on 2003-11-19
6
Medium Priority
?
616 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 32

Accepted Solution

by:
jadedata earned 150 total points
ID: 9782574
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
ID: 9783044
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
ID: 9783104
i'm using a db2
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Callandor
ID: 9800373
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
ID: 9800439
did it work??
0
 

Author Comment

by:chambers777
ID: 9800494
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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