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 Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-09, 01:20
rajinbits rajinbits is offline
Registered User
 
Join Date: Nov 2007
Posts: 26
Update Query

Hi,
I have the following Update Query. Is there any better way of writing this query ?

Stats: Table2 is a subset of Table1
Both Tables has a unique index on cold and cole

UPDATE TABLE1 A
SET (COLA,COLB) =(SELECT B.COLA,B.COLB
FROM TABLE2 B
WHERE B.COLD = A.COLD
AND B.COLDE = A.COLE
AND (B.COLA != A.COLa
OR B.COLB != A.COLB)
)
WHERE (A.COLD,A.COLE) IN (SELECT COLD,COLE FROM TABLE2);

Thanks,
Rajesh
Reply With Quote
  #2 (permalink)  
Old 02-19-09, 09:49
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You could try a MERGE statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 02-19-09, 09:50
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You could try a MERGE statement. However, your predicates in the 2 subselects are not the same. So you may actually set SQL NULL for some of the rows in TABLE1.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 02-19-09, 11:31
rajinbits rajinbits is offline
Registered User
 
Join Date: Nov 2007
Posts: 26
Thats right....So just wanted to know...if there any other way of doing it...
we can do the positioned update...but that will be expensive given the table size is around 200k..

THanks
Reply With Quote
  #5 (permalink)  
Old 02-19-09, 16:44
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
As I said, the MERGE statement is probably simpler to write than repeating the same subselect in the SET clause and the WHERE clause.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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