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 > Use embedded update in select statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-09, 13:45
subt13 subt13 is offline
Registered User
 
Join Date: Dec 2009
Posts: 4
Use embedded update in select statement

Hi I'm trying to write a query like this:

select
(update Table2 set Table2.Column1 = TableA.Column1)
from TableA

The idea is that for each row the select query returns it will update Table2 with the value from TableA.Column1.

I was able to do it in SQL Server, however I don't know how in DB2.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-16-09, 14:00
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question

Quote:
Originally Posted by subt13 View Post
Hi I'm trying to write a query like this:

select
(update Table2 set Table2.Column1 = TableA.Column1)
from TableA

The idea is that for each row the select query returns it will update Table2 with the value from TableA.Column1.

I was able to do it in SQL Server, however I don't know how in DB2.

Thanks.
Maybe in Version 11 ?
Now most closer to your dream is MERGE statement....

Lenny
Reply With Quote
  #3 (permalink)  
Old 12-16-09, 14:06
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
The idea is that for each row the select query returns it will update Table2 with the value from TableA.Column1.
Which row(s) in Table2?
Reply With Quote
  #4 (permalink)  
Old 12-16-09, 14:06
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by subt13 View Post
The idea is that for each row the select query returns it will update Table2 with the value from TableA.Column1.

I was able to do it in SQL Server, however I don't know how in DB2.
In standard SQL that should look something like
Code:
update Table2 
set Table2.Column1 = (select TableA.Column1 from TableA where ...)
You will need to insert the condition that correlates each row in Table2 with at most one row in TableA, of course.
Reply With Quote
  #5 (permalink)  
Old 12-16-09, 14:19
subt13 subt13 is offline
Registered User
 
Join Date: Dec 2009
Posts: 4
Lenny, I think you hit the nail on the head. I browsed through the DB2 documentation on the merge command, but I didn't see it mention how it would handle a many to 1 or even many to many set.

For example:
With match you use something like this: ON ( table1.Column1 = table2.Column1 )
what happens if Table1 and/or Table2 contain non-distinct/non-unique values that we are comparing?
Reply With Quote
  #6 (permalink)  
Old 12-16-09, 14:20
subt13 subt13 is offline
Registered User
 
Join Date: Dec 2009
Posts: 4
Tonkuma:

I don't understand your question. If there are 10 rows in TableA and 10 rows in TableB, then the answer is 10 rows. Obviously I could filter it down further like with the match and its "on" keyword.
Reply With Quote
  #7 (permalink)  
Old 12-16-09, 14:49
subt13 subt13 is offline
Registered User
 
Join Date: Dec 2009
Posts: 4
n_i: Duh! I should have known that! That works as well, and that's probably what I was thinking of. Thank you.
Reply With Quote
  #8 (permalink)  
Old 12-16-09, 14:51
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by subt13 View Post
I didn't see it mention how it would handle a many to 1 or even many to many set.
The question really is, how YOU would handle such situations. You can always come up with an SQL statement that implements your rules, but you must have those rules in the first place.
Reply With Quote
  #9 (permalink)  
Old 12-16-09, 14:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb

Quote:
Originally Posted by subt13 View Post
Lenny, I think you hit the nail on the head. I browsed through the DB2 documentation on the merge command, but I didn't see it mention how it would handle a many to 1 or even many to many set.

For example:
With match you use something like this: ON ( table1.Column1 = table2.Column1 )
what happens if Table1 and/or Table2 contain non-distinct/non-unique values that we are comparing?
Use On(1 = 1)

Lenny
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