简体   繁体   中英

How to copy specific columns from one table to another in SQL?

I am new to SQL Server and SQL language. I am using SQL Server Express.

I have two tables ( Table_Coordinates & Table_Addresses ) both tables have over 40,000 records.

Both table columns are as following:

  • Table_Coordinates : Postcode , Latitude & Longitude .
  • Table_Addresses : Building_Number , Postcode , LatCoord & LongCoord

The LatCoord & LongCoord columns in Table_Addresses are NULL .

As shown in the screenshot , what I want is to loop through each record in Table_Coordinates and copy the Latitude & Longitude values of the 1st Postcode (1st row) and enter those values into LatCoord & LongCoord columns in Table_Addresses for the exact same Poscode, then go to the 2nd record in Table_Coordinates and do the same for all records in Table_Coordinates .

For example as shown in screen shot, copy the L24 2SA postcode coordinates (53.340188, -2.838058) from Table_Coordinates to Table_Addresses to where Postcode = L24 2SA

Does anyone know how to do it?

屏幕截图

If you want to show you can use joins but if you typically want to update record then in your case cursors are preferred. You can do it by the following way.

Declare @LatCoord nvarchar
Declare @LongCoord nvarchar

DECLARE [YourCursorName] CURSOR FAST_FORWARD 
 FOR 
 SELECT LatCoord, LongCoord FROM  Table_Coordinates where postcode = @Postcode
 OPEN [YourCursor] FETCH NEXT FROM [YourCursor] INTO @LatCoord, @LongCoord
 WHILE @@FETCH_STATUS = 0 
 BEGIN  
     Update [Table_Addresses]
     SET LatCoord = @LatCoord, LongCoord = @LongCoord where postcode = @Postcode 
    FETCH NEXT FROM [YourCursor] INTO @LatCoord, @LongCoord
 END  
 CLOSE [YourCursor] 
 DEALLOCATE [YourCursor]  

A simple JOIN with a WHERE clause will do the job

UPDATE T1
SET T1.LatCoord  = T2.Latitude,
    T1.LongCoord = T2.Longitude
FROM Table1 T1 INNER JOIN Table2 T2
     ON T1.PostCode = T2.PostCode
WHERE T1.PostCode = 'L24 2SA'

I don't think you should do this. Remove the coordinates from the addresses table. When you need to access them simply use a join :

select a.*, c.atitude, c.longitude
from table_addresses a left join
     table_coordinates c
     on a.postcode = c.postcode;

If you don't want to run the code explicitly, you can hide it in a view:

create view v_addresses as
    select a.*, c.atitude, c.longitude
    from table_addresses a left join
         table_coordinates c
         on a.postcode = c.postcode;

Why am I recommending this approach? Duplicating data across tables is a bad design decision. This is particularly true if you are new to SQL. You need to learn how to handle data across multiple tables.

Think about what would happen if the data changes in tbl_coordinates ? Then you have to repeat the updates -- if you happen to remember to do so. Otherwise, the data gets out of synch.

What about performance? You won't even notice the difference if you have an index on table_coordinates(postcode) .

UPDATE with JOIN

UPDATE Table_Addresses
SET Table_Addresses.LatCoord = Table_Coordinates.Latitude, Table_Addresses.LongCoord = Table_Coordinates.Longitude 
FROM Table_Addresses JOIN Table_Coordinates
ON Table_Addresses.Postcode = Table_Coordinates.Postcode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM