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
Solved

QUERY to update if exists and insert otherwise

Posted on 2003-11-19
6
599 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
free version of MSSQL for over 10GB DB 19 69
Is Dropbox the only way to upload large files? 3 44
Perl Versus AWK? 7 52
Access 2016 Query with Proper Case logic 2 34
This article describes some very basic things about SQL Server filegroups.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

860 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