If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > 'If Exists' Clause in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-31-05, 04:21
madhuri_awal madhuri_awal is offline
Registered User
 
Join Date: Dec 2004
Location: India
Posts: 23
Exclamation 'If Exists' Clause in DB2

Hi all....
Does DB2 UDb uses the clause 'IF EXISTS....ELSE' ????

For eg I want to check whether a record with particular ID exists in the table.If it exists then update the remaining fields else insert a new record.
The below query does not work:

IF EXISTS (SELECT 'X' FROM T1 WHERE ID = 1) UPDATE T1 SET NAME = 'MMM' ELSE INSERT INTO T1 (ID,NAME) VALUES (1,'MMM')

I tried the query UPDATE T1 SET NAME = 'MMM' WHERE EXISTS (SELECT 'X' FROM T1 WHERE ID = 1)....This query works fine but again I want the 'ELSE' part.....

In core...How can I use the update query with conditional exists clause??????



Thanks in advance,
Madhuri.
Reply With Quote
  #2 (permalink)  
Old 01-31-05, 05:32
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
I think you should be able to accompolish this using a MERGE Statement, assuming you are on V8.1.2 atleast
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 01-31-05, 07:54
fohana fohana is offline
Registered User
 
Join Date: Jan 2005
Location: Rio de Janeiro - Brazil
Posts: 28
Just a hunch

I donŽt know where youŽll run this statement but I guess you could try to use the CASE..WHEN..THEN clause.

CASE WHEN (<OPTION>) THEN UPDATE..
ELSE INSERT..
END CASE

You can find more about this statement at the SQL Reference
Reply With Quote
  #4 (permalink)  
Old 06-03-11, 15:52
jkuyken jkuyken is offline
Registered User
 
Join Date: May 2010
Posts: 21
You might want to consider SQL/DB2 merge statement:

DB2 Database for Linux, UNIX, and Windows
Reply With Quote
  #5 (permalink)  
Old 06-06-11, 17:45
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
you are getting a lot of "I guess" and "might" answers, because you did not tell us what the "ELSE" you want done is. If you could explain fully what you are wanting to do, you will indubitably get a more concrete answer.
Dave Nance
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On