• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3668
  • Last Modified:

Update multiple rows from another table where ID's equal

Hi,

I'm wondering if there is a way to update a table (tableA) with another table's values (tableB)  where the ID's equal.  For example:

TableA                                TableB
ID, Data, Cust                     ID, Jerry, John
1, xxx, John                         1, 333, null
2, xyx, John                         2, 222, null
3, yyx, John

What I want to do is update TableB column "John" with the data from TableA where the ID is the same and TableA.Customer = 'John'

I'm beating my head against a wall.  Any suggestions?


3, Null
4, xyyy
0
carmodyk
Asked:
carmodyk
  • 4
  • 2
5 Solutions
 
Nick UpsonPrincipal Operations EngineerCommented:
The general form is:

update table1 set

field1 = (select fileld2 from table2 where...),
field2 = (select field3 from table2 where...),
...
where table1...

0
 
carmodykAuthor Commented:
I've tried that, I'm getting an error saying Multiple rows in singleton select
0
 
Nick UpsonPrincipal Operations EngineerCommented:
this part of the query has to be unique with a where clause linking table1 to table2

field1 = (select fileld2 from table2 where...),

something like this, for a lookup change

field1 = (select fileld2 from table2 b where b.something = field1),

0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
Nick UpsonPrincipal Operations EngineerCommented:
Another approach is, If you have a lot of records, writing a stored
procedure (or EXECUTE BLOCK with Firebird 2) should be better:

EXECUTE BLOCK
AS
DECLARE VARIABLE c1...
DECLARE VARIABLE c2...
DECLARE VARIABLE c3... etc. for each column
BEGIN
for select c1, c2, c3, ...
from table1
into :c1, :c2, :c3...
do update table2 set c1 = :c1, c2 = :c2, ... where pk = :pk;
END
0
 
Nick UpsonPrincipal Operations EngineerCommented:
this might make it clearer

UPDATE TableA
SET FieldA =
(SELECT B.FieldB FROM TableA B
WHERE B.FieldID = TableA.FieldID)
0
 
Gary BenadeCommented:
To get around your singleton problem, change your query to something like this:

update table b
set john = (select max(data) from tablea where id = b.id)

HTH
Gary
0
 
carmodykAuthor Commented:
Thanks fellas, I've given up on the problem. But your help I'm sure will be useful in the future. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now