473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Nov 12 '05 #1
4 1952
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
Nov 12 '05 #2
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.
Nov 12 '05 #3
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
Nov 12 '05 #4
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
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary Stollman | last post by:
My Father is a Clone//ETs plotting to overthrow the human race! On August 19, 1987 a gun-carrying Gary Stollman entered the studio of Los Angeles's KNBC television, crashing consumer reporter...
5
by: Alex | last post by:
I am having difficulty writing a SQL statement and I was wondering if one of you gurus could help me. I have a table that has three fields: InvoiceID, Item, ItemPrice. Each InvoiceID is associated...
1
by: Jill | last post by:
I am trying to store the current date to an ms access database on my server. I set it up with a dsnless connection. Here is the statement: Insert Into employees(timestamp) Values ('" & date() &...
1
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
1
by: JP | last post by:
I just installed db2 V8.1 and I am trying to create a simple compound statement in the command center script area and every time I would get the following error. what am i doing wrong? (note: my...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
14
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
2
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.