Unix Technical Forum

Need Help With SQL Statement

This is a discussion on Need Help With SQL Statement within the DB2 forums, part of the Database Server Software category; --> Here is what I have: Table1 with two key fields: PartNum and WH# (warehouse number) Table2 with only one ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 03:30 AM
jbow0527
 
Posts: n/a
Default Need Help With SQL Statement

Here is what I have:

Table1 with two key fields: PartNum and WH# (warehouse number)
Table2 with only one key field: PartNum

Both tables contain a field called Price.

I want to update Table1 with the Price field from Table2 where the
PartNum field matches. Of course the WH# field does not have to match
because Table2 does not contain that field.

What SQL statement would I use to accomplish this? Any help would be
appreciated.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 03:30 AM
Serge Rielau
 
Posts: n/a
Default Re: Need Help With SQL Statement

jbow0527 wrote:
> Here is what I have:
>
> Table1 with two key fields: PartNum and WH# (warehouse number)
> Table2 with only one key field: PartNum
>
> Both tables contain a field called Price.
>
> I want to update Table1 with the Price field from Table2 where the
> PartNum field matches. Of course the WH# field does not have to match
> because Table2 does not contain that field.
>
> What SQL statement would I use to accomplish this? Any help would be
> appreciated.
>

UPDATE Table1 SET price = (SELECT price FROM Table2 WHERE Table1.PartNum
= Table2.PartNum) WHERE EXISTS(SELECT 1 FROM Table2 WHERE Table1.PartNum
= Table2.PartNum);

Cheers
Serge
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 03:30 AM
Bob Stearns
 
Posts: n/a
Default Re: Need Help With SQL Statement

Serge Rielau wrote:
> jbow0527 wrote:
>
>> Here is what I have:
>>
>> Table1 with two key fields: PartNum and WH# (warehouse number)
>> Table2 with only one key field: PartNum
>>
>> Both tables contain a field called Price.
>>
>> I want to update Table1 with the Price field from Table2 where the
>> PartNum field matches. Of course the WH# field does not have to match
>> because Table2 does not contain that field.
>>
>> What SQL statement would I use to accomplish this? Any help would be
>> appreciated.
>>

> UPDATE Table1 SET price = (SELECT price FROM Table2 WHERE Table1.PartNum
> = Table2.PartNum) WHERE EXISTS(SELECT 1 FROM Table2 WHERE Table1.PartNum
> = Table2.PartNum);
>
> Cheers
> Serge

Wouldn't it be simpler (in DB2 UDB v8.1.2 and later) to:

merge into table1
using table2
on table1.partnum=table2.partnum
when matched then
update set price = table2.price

I realize your solution is more portable (and that OP didn't specify his
version), but for me at least, takes much more "unraveling" to
understand. As far as I know, your solution is what the merge statement
becomes at some point during sql processing or optimization.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 03:30 AM
Knut Stolze
 
Posts: n/a
Default Re: Need Help With SQL Statement

Bob Stearns wrote:

> Serge Rielau wrote:
>> jbow0527 wrote:
>>
>>> Here is what I have:
>>>
>>> Table1 with two key fields: PartNum and WH# (warehouse number)
>>> Table2 with only one key field: PartNum
>>>
>>> Both tables contain a field called Price.
>>>
>>> I want to update Table1 with the Price field from Table2 where the
>>> PartNum field matches. Of course the WH# field does not have to match
>>> because Table2 does not contain that field.
>>>
>>> What SQL statement would I use to accomplish this? Any help would be
>>> appreciated.
>>>

>> UPDATE Table1 SET price = (SELECT price FROM Table2 WHERE Table1.PartNum
>> = Table2.PartNum) WHERE EXISTS(SELECT 1 FROM Table2 WHERE Table1.PartNum
>> = Table2.PartNum);

>
> Wouldn't it be simpler (in DB2 UDB v8.1.2 and later) to:
>
> merge into table1
> using table2
> on table1.partnum=table2.partnum
> when matched then
> update set price = table2.price
>
> I realize your solution is more portable (and that OP didn't specify his
> version), but for me at least, takes much more "unraveling" to
> understand. As far as I know, your solution is what the merge statement
> becomes at some point during sql processing or optimization.


Serge's version would be much clearer to me because it says what the
statement is doing: only an UPDATE - not a MERGE. But I guess that's very
much influenced by personal taste.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 03:30 AM
Serge Rielau
 
Posts: n/a
Default Re: Need Help With SQL Statement

Bob Stearns wrote:
> Serge Rielau wrote:
>
>> jbow0527 wrote:
>>
>>> Here is what I have:
>>>
>>> Table1 with two key fields: PartNum and WH# (warehouse number)
>>> Table2 with only one key field: PartNum
>>>
>>> Both tables contain a field called Price.
>>>
>>> I want to update Table1 with the Price field from Table2 where the
>>> PartNum field matches. Of course the WH# field does not have to match
>>> because Table2 does not contain that field.
>>>
>>> What SQL statement would I use to accomplish this? Any help would be
>>> appreciated.
>>>

>> UPDATE Table1 SET price = (SELECT price FROM Table2 WHERE
>> Table1.PartNum = Table2.PartNum) WHERE EXISTS(SELECT 1 FROM Table2
>> WHERE Table1.PartNum = Table2.PartNum);
>>
>> Cheers
>> Serge

>
> Wouldn't it be simpler (in DB2 UDB v8.1.2 and later) to:
>
> merge into table1
> using table2
> on table1.partnum=table2.partnum
> when matched then
> update set price = table2.price
>
> I realize your solution is more portable (and that OP didn't specify his
> version), but for me at least, takes much more "unraveling" to
> understand. As far as I know, your solution is what the merge statement
> becomes at some point during sql processing or optimization.

Yes, MERGE statement is the SQL2003 way and Db2 V8.1.2 way of writing this.
The UPDATE turns into something close to the MERGE rather than the other
way around.
As long as table2.partnum is unique DB2 will merge the EXIST and the
scalar subselect to generate (internally) what IDS and SQL Server users
know as:
UPDATE table1 SET price = table2.price
FROM table1, table2 WHERE table1.partnum = table2.partnum

Cheers
Serge
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com