You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by John Storta Jr <js...@storta.net> on 2011/03/03 20:50:24 UTC

Copy data from one table to another

TABLEA

Contains columns called A, X, Y, and Z

Column A is a key field while columns X, Y, and Z are empty.

There are also other columns with valid data so I cannot remove the table
and start fresh.

 

TABLEB

Contains columns called A, X, Y, and Z also

Column A is a foreign key to TABLEA

Columns X, Y, and Z contain valid data.

 

I want to copy the data in columns X, Y, and Z from TABLEB into the columns
X, Y, and Z in TABLEA where column A matches.

 

UPDATE TABLEA SET 

TABLEA.X = TABLEB.X, 

TABLEA.Y = TABLEB.Y, 

TABLEA.Z = TABLEB.Z 

WHERE TABLEA.A = TABLEB.A;

 

Naturally I get an error because TABLEB is not in a FROM clause.  I have
tried a few different things to incorporate TABLEB into the mix, but nothing
is working.  Most just give me a syntax error.

 

 

Any thoughts?

 

Thanks,

John S.

 


Re: Copy data from one table to another

Posted by Knut Anders Hatlen <kn...@oracle.com>.
"John Storta Jr." <js...@storta.net> writes:

> I think that is going to update every row in TABLEA to have the same
> set of values.  I am trying some variations now without much success.
>  
>  
>  
> On Thu, Mar 3, 2011 at 3:30 PM, Bergquist, Brett
> <BB...@canoga.com> wrote:
>
>     
>         Try something like:
>
>      
>
>     UPDATE TABLEA SET 
>
>     TABLEA.X = (SELECT TABLEB.X FROM TABLEB WHERE TABLEA.A =
>     TABLEB.B), 
>
>     TABLEA.Y = (SELECT TABLEB.Y FROM TABLEB WHERE TABLEA.A =
>     TABLEB.B), 
>
>     TABLEA.Z = (SELECT TABLEB.Z FROM TABLEB WHERE TABLEA.A =
>     TABLEB.B);

I think the above would work if you add a WHERE clause to limit the set
of rows on which the update statement operates:

UPDATE TABLEA SET
   TABLEA.X = (SELECT TABLEB.X FROM TABLEB WHERE TABLEA.A = TABLEB.B),
   TABLEA.Y = (SELECT TABLEB.Y FROM TABLEB WHERE TABLEA.A = TABLEB.B),
   TABLEA.Z = (SELECT TABLEB.Z FROM TABLEB WHERE TABLEA.A = TABLEB.B)
WHERE EXISTS (SELECT * FROM TABLEB WHERE TABLEA.A = TABLEB.B)

But it'll only work if TABLEB.B doesn't contain duplicates.

If you don't know that the values in TABLEB.B are a subset of the values
in TABLEA.A, you'll also need an insert statement to add the new rows:

INSERT INTO TABLEA SELECT * FROM TABLEB
 WHERE NOT EXISTS (SELECT * FROM TABLEA WHERE TABLEA.A = TABLEB.B)

-- 
Knut Anders

Re: Copy data from one table to another

Posted by "John Storta Jr." <js...@storta.net>.
I think that is going to update every row in TABLEA to have the same set of
values.  I am trying some variations now without much success.



On Thu, Mar 3, 2011 at 3:30 PM, Bergquist, Brett <BB...@canoga.com>wrote:

>  Try something like:
>
>
>
> UPDATE TABLEA SET
>
> TABLEA.X = (SELECT TABLEB.X FROM TABLEB WHERE TABLEA.A = TABLEB.B),
>
> TABLEA.Y = (SELECT TABLEB.Y FROM TABLEB WHERE TABLEA.A = TABLEB.B),
>
> TABLEA.Z = (SELECT TABLEB.Z FROM TABLEB WHERE TABLEA.A = TABLEB.B);
>
>
>
> The value can be an expression.
>
>
>
> *From:* John Storta Jr [mailto:js@storta.net]
> *Sent:* Thursday, March 03, 2011 2:50 PM
> *To:* derby-user@db.apache.org
> *Subject:* Copy data from one table to another
>
>
>
> TABLEA
>
> Contains columns called A, X, Y, and Z
>
> Column A is a key field while columns X, Y, and Z are empty.
>
> There are also other columns with valid data so I cannot remove the table
> and start fresh.
>
>
>
> TABLEB
>
> Contains columns called A, X, Y, and Z also
>
> Column A is a foreign key to TABLEA
>
> Columns X, Y, and Z contain valid data.
>
>
>
> I want to copy the data in columns X, Y, and Z from TABLEB into the columns
> X, Y, and Z in TABLEA where column A matches.
>
>
>
> UPDATE TABLEA SET
>
> TABLEA.X = TABLEB.X,
>
> TABLEA.Y = TABLEB.Y,
>
> TABLEA.Z = TABLEB.Z
>
> WHERE TABLEA.A = TABLEB.A;
>
>
>
> Naturally I get an error because TABLEB is not in a FROM clause.  I have
> tried a few different things to incorporate TABLEB into the mix, but nothing
> is working.  Most just give me a syntax error.
>
>
>
>
>
> Any thoughts?
>
>
>
> Thanks,
>
> John S.
>
>
>

RE: Copy data from one table to another

Posted by "Bergquist, Brett" <BB...@canoga.com>.
Try something like:

UPDATE TABLEA SET
TABLEA.X = (SELECT TABLEB.X FROM TABLEB WHERE TABLEA.A = TABLEB.B),
TABLEA.Y = (SELECT TABLEB.Y FROM TABLEB WHERE TABLEA.A = TABLEB.B),
TABLEA.Z = (SELECT TABLEB.Z FROM TABLEB WHERE TABLEA.A = TABLEB.B);

The value can be an expression.

From: John Storta Jr [mailto:js@storta.net]
Sent: Thursday, March 03, 2011 2:50 PM
To: derby-user@db.apache.org
Subject: Copy data from one table to another

TABLEA
Contains columns called A, X, Y, and Z
Column A is a key field while columns X, Y, and Z are empty.
There are also other columns with valid data so I cannot remove the table and start fresh.

TABLEB
Contains columns called A, X, Y, and Z also
Column A is a foreign key to TABLEA
Columns X, Y, and Z contain valid data.

I want to copy the data in columns X, Y, and Z from TABLEB into the columns X, Y, and Z in TABLEA where column A matches.

UPDATE TABLEA SET
TABLEA.X = TABLEB.X,
TABLEA.Y = TABLEB.Y,
TABLEA.Z = TABLEB.Z
WHERE TABLEA.A = TABLEB.A;

Naturally I get an error because TABLEB is not in a FROM clause.  I have tried a few different things to incorporate TABLEB into the mix, but nothing is working.  Most just give me a syntax error.


Any thoughts?

Thanks,
John S.