0
TableA 
ID Address Location Country
1 NULL Region1 Austrailia
2 NULL Region2 Antartica
3 NULL Region3 Egypt
TableB
ID Address Name
1 test avenue james
2 test avenue 2 jessica
3 test avenue 3 joshua

I am working on a query to migrate data Address data from Database 1, TableB to Database 2 TableA. So far I have tried to do the query below however that only inserts one record for the entire database, I want to insert each new sequential address into TableA as it appears in TableB.

UPDATE [dbo].[TableA]
SET [dbo].[TableA].[Address]= Table2.ID
FROM (SELECT [TableB].[ID]
 FROM [TableB]) AS Table2
WHERE [dbo].[TableA].[ID] <= 1449

This results in all records only having the first ID which is 1, so all the rows in TableA are Address = 1, instead of 1,2,3 etc. I think I am close but not quite sure what I am missing

Desired Result

TableA 
 ID Address Location Country
 1 1 Region1 Austrailia
 2 2 Region2 Antartica
 3 3 Region3 Egypt
asked Feb 6, 2020 at 21:25
5
  • Do you mean, for each ID on table A add all addresses of table B? Or TableA.ID = TableB.ID? Commented Feb 6, 2020 at 21:39
  • @McNets For each ID of TableB, add TableB.ID into TableA.Address Commented Feb 6, 2020 at 21:42
  • Please, could you add the desired result of your sample data? Commented Feb 6, 2020 at 21:43
  • @McNets edited question with desired result, so populate TableA with the ID's of TableB Commented Feb 6, 2020 at 21:51
  • Are you sure updating ID is a good idea? Commented Feb 6, 2020 at 22:28

1 Answer 1

0

You can try this

UPDATE a
SET a.Address = b.ID
FROM TableA a
JOIN TableB b ON b.ID = a.ID
WHERE a.ID <= 1449
answered Feb 6, 2020 at 22:30
6
  • I tried this, sadly it has no effect on the data in the column I want to update. Commented Feb 7, 2020 at 2:13
  • Not sure if the JOIN is what is causing the problem being that both tables have nothing really to join on, perhaps Commented Feb 7, 2020 at 2:14
  • If there's nothing to join on, how do you know which row is assigned to which row on the other table? Commented Feb 7, 2020 at 17:03
  • Based on your sample data, my code will work fine. Commented Feb 7, 2020 at 17:05
  • Well the ID's that I want to copy over are sequential, so for example 1,2,3,4,5.... etc. I just want to use the numbers that exists in TableB to populate the Address column in tableA Commented Feb 7, 2020 at 20:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.