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 > Update column based on whether a row exists in another column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-17-11, 12:20
aijain28 aijain28 is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
Update column based on whether a row exists in another column

Hello,

I have table T1 with columns (A,B)
and table T2 with columns (C,D)

I wanted to do the following in plain english-

update T1 set B = 'Y'

if
exists (select 1 from T2 where T2.C = T1.A)

else

update T1 set B = 'N'

How can I do this in DB2?
Reply With Quote
  #2 (permalink)  
Old 03-17-11, 12:52
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try this:

Code:
update t1 set b = case when exists (select 1 from T2 where T2.C = T1.A) then 'Y' else 'N' end
Andy
Reply With Quote
  #3 (permalink)  
Old 03-17-11, 15:38
aijain28 aijain28 is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
Thank you!
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