0

How do insert into table values from another table where id from one table = id from the other table?

Example:

Table 1:

ID Name CardHolderID Date
1  N     1231212     02/20/2011
2  S     1212312     02/20/2011

Table 2:

ID Name CardholderID Date
1  N       null      02/20/2011
2  S       null      02/20/2011

This is just an example. The database contains about 60000 records. When the data was exported, apparently the CardholderID was left out. They sent the data with CardHolderID in a different file. How can I insert the data into the Table 2 from Table 1?

I tried something like this

INSERT INTO TableA(cardholderid) 
select cardholderid from tableB where tableA.id = tableB.id

I'm getting "TableA could not be bound" error.

Thanks

user1828605
  • 1,723
  • 1
  • 24
  • 63
  • 1
    Sounds like you need to update Table 2 from CardHolderID values from Table 1. Try this http://stackoverflow.com/questions/2334712/update-from-select-using-sql-server/2334741#2334741 – rageit Jun 18 '14 at 21:27

1 Answers1

1

this is not an insert, but an update

update a
set a.cardholderid = b.cardholderid
from tableb b
join tablea a on a.id = b.id

see SqlFiddle

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122