Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / February 2008

Tip: Looking for answers? Try searching our database.

Update across 2 tables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arun Srinivasan - 09 Jan 2008 21:59 GMT
I have 2 tables.
test1 : A integer, B integer - (1,1) , (2,2)
test2 : A integer, B integer (1,2) , (2,3)

I have to update test1, set test1.b = test2. b where test1.a =
test2.a

Can this be done ?
Lennart - 10 Jan 2008 05:01 GMT
> I have 2 tables.
> test1 : A integer, B integer - (1,1) , (2,2)
[quoted text clipped - 4 lines]
>
> Can this be done ?

If A alone is the primary key it is safe to:

update test1 set b = (
   select b from test2
   where test2.a = test1.a
);

/Lennart
Knut Stolze - 10 Jan 2008 09:23 GMT
>> I have 2 tables.
>> test1 : A integer, B integer - (1,1) , (2,2)
[quoted text clipped - 11 lines]
>     where test2.a = test1.a
> );

And if you further want to restrict the rows to be updated to those that
have a matching row in table TEST2:

UPDATE test1
SET    b = ( SELECT b
            FROM   test2
            WHERE  test2.a = test1.a )
WHERE  EXISTS ( SELECT b
               FROM   test2
               WHERE  test2.a = test1.a )

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

Serge Rielau - 10 Jan 2008 11:34 GMT
> I have 2 tables.
> test1 : A integer, B integer - (1,1) , (2,2)
> test2 : A integer, B integer (1,2) , (2,3)
>
> I have to update test1, set test1.b = test2. b where test1.a =
> test2.a
MERGE INTO test1 USING test2 ON test1.a = test2.a
 WHEN MATCHED THEN UPDATE SET test1.b = test2.b

It's ANSI/SQL
Cheers
Serge
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Arun Srinivasan - 04 Feb 2008 17:53 GMT
I just love DB2's set operations (dont know if this is the correct
title)...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.