Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating one table with values from another 2

Status
Not open for further replies.

dragony

MIS
Sep 26, 2001
17
TH
Hello SQL experts,

It's me the SQL newbie again. This time I'm trying to update a table with data from another table. Seems like a simple thing, but I can't seem to get it right. Here's some background info:
<TARGET Table>
ID,Title,Firstname,Lastname,Nationality,Religion,Sex

<SOURCE table>
ID,Nationality,Religion,Sex

The TARGET table already have ID and names populated, I'm just trying to add Nationality, Religion, Sex to it. Here's what I'm trying to execute:

UPDATE target
SET Nationality=(SELECT Nationality FROM SOURCE where TARGET.[ID]=SOURCE.[ID])

But I keep getting this error:
Server: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=,<,<=,>,>= or when the subquery is used as an expression.
This statement has been terminated.

The SOURCE table has less data than the TARGET, and also has some duplicate data within SOURCE dable. Could this be the issue?

Any thoughts or suggestion?

Thank you very much in advance,
dragony
 
UPDATE target
SET Nationality = B.Nationality
FOM target as A inner join Source as B
ON A.ID = B.ID
 
UPDATE target
SET Nationality = B.Nationality
FROM target as A inner join Source as B
ON A.ID = B.ID
 
If the ID column in the source table isn't unique then this is almost certain to be causing the problem.
How do you know which rows in the source table you want to use to update?
 
Thanks DBomrrsm, that did the trick. Simple, yet effective. I guess I'll have to learn more about the join command and concept to apply somewhere else.

Katy44, You're absolutely right. There are some duplicate ID in the SOURCE table. I think I'm lucky this time that the records with the same ID will have the same Nationality as well.

Thanks a bunch guys for quick help and suggestions,
dragony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top