Solved

QUERY to update if exists and insert otherwise

Posted on 2003-11-19
6
612 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 50 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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

739 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