By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,756 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,756 IT Pros & Developers. It's quick & easy.

sql update from stage tables with two column keys db2 V8

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
Serge

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

This discussion thread is closed

Replies have been disabled for this discussion.