Tuesday, January 4, 2011

Update data in one table with data from another table

Need to switch 2 columns that were wrongfully imported:

Step1
Create a copy of the tabl
e - CREATE TABLE [dbo].[Client2](....

Step2
Inserted the data to the copied table
-
INSERT INTO [dbo].[Client2]
           ([UniqueClientID]
     ,[Latitude]
           ,[Longitude]
           ,[ClientName])
     SELECT [UniqueClientID]
      ,[Latitude]
      ,[Longitude]
      ,[ClientName]
     FROM [dbo].[Client]

Step3
Switch the 2 column data by updating

UPDATE [dbo].[Client]
   set [Latitude] = [dbo].[Client2].[Longitude]
      ,[Longitude] = [dbo].[Client2].[Latitude]
 From [dbo].[Client],[dbo].[Client2]
 where [dbo].[Client].[UniqueClientID] = [dbo].[Client2].[UniqueClientID]
GO