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
1 Answer 1
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
-
I tried this, sadly it has no effect on the data in the column I want to update.kabuto178– kabuto1782020年02月07日 02:13:04 +00:00Commented 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, perhapskabuto178– kabuto1782020年02月07日 02:14:51 +00:00Commented 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?Eric– Eric2020年02月07日 17:03:32 +00:00Commented Feb 7, 2020 at 17:03
-
Based on your sample data, my code will work fine.Eric– Eric2020年02月07日 17:05:16 +00:00Commented 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 tableAkabuto178– kabuto1782020年02月07日 20:28:01 +00:00Commented Feb 7, 2020 at 20:28
ID
is a good idea?