473,396 Members | 1,599 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.

sql update from stage tables with two column keys db2 V8

Given table a: col1 col2 name1 name2 where col1 and col2 make up the
unique key.
Given a stage table b: col1 col2 name1 name2. (again unique key same
as a)

table a has 34M rows
table b has 2 million rows

i want to update table a's name1 and name2 with the values from table
b
where a.col1 = b.col1 and a.col2 = b.col2.

I don't think this can be done within a sql update statement so I am
writing a program to do a cursor update; However I would think the
db2 engine would be more efficient.
Can it be done?

Thanks,
Mike
Nov 12 '05 #1
2 8308
Mike wrote:
I don't think this can be done within a sql update statement so I am
writing a program to do a cursor update; However I would think the
db2 engine would be more efficient.
Can it be done?

Of course :-)
The classic (SQL 92):
UPDATE a
SET (name1, name2) = (SELECT name1, name2 FROM b
WHERE a.col1 = b.col1 and a.col2 = b.col2)
WHERE EXISTS(SELECT 1 FROM b
WHERE a.col1 = b.col1 and a.col2 = b.col2)
Presuming (col1,colb) have a unique index on both A and B DB2 merge the
two queries on B.

The modern (SQL 4):
MERGE INTO a
USING b
ON a.col1 = b.col1 and a.col2 = b.col2
WHEN MATCHED THEN UPDATE SET (name1, name2) = (b.name1, b.name2)

Cheers
Serge
Nov 12 '05 #2
Serge

Thanks for your excellent examples...!
Mike
Nov 12 '05 #3

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

Similar topics

8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
7
by: robert | last post by:
i need to update a column which is a member of the PK on this table. there are some thousands of rows to be updated, many more thousand already in the table. so, i get a constraint violation...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
3
by: dbuchanan | last post by:
Hello, (Windows forms - SQL Server) I fill my datagrid with a stored procedure that includes relationships to lookup tables so that users can see the values of the combobox selections rather...
4
by: mimime | last post by:
Scenario: Assign a new_customer_id to each existing customer of our three business. A master lookup table (M) was given to me with pre-populated (SEQ_NOs), (NEW_CUSTIDs), and NULL in (OLD_CUSTIDS)....
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.