I am trying to update database fields from one SQL Server table to another.
Our production SQL Server is [spdbprod.test.com\\spprod]
, our QA server is
I know this has been answered already but this worked for me.
Name the Linked Server [SERVER-NAME or <some ipaddress>, <some-port>]
e.g. [10.0.0.200,2345]
- I am using port 2345 but the standard MS SQL port is 1433.
Example:
[Customers]
table [CustomerAddress]
-field for CustomerId = 123
[backupServer]
[backupServer]
is the machine where we execute the SQLThis is the SQL-code:
UPDATE production
SET
CustomerAddress = backupServer.CustomerAddress
FROM
[10.0.0.200,2345].[ProductionDatabase].[dbo].[Customers] production
INNER JOIN
[BackupDatabase].[dbo].[Customers] backupServer
ON
production.CustomerId = backupServer.CustomerId
WHERE
backupServer.CustomerId = 123
Generalized format:
UPDATE production
SET
columnName = backupServer.columnName
FROM
[SERVER-NAME or IP,PORT].[ProductionDatabase].[dbo].[tableName] production
INNER JOIN
[BackupDatabase].[dbo].[tableName] backupServer
ON
production.SomeId = backupServer.SomeId
WHERE
backupServer.SomeId = <id>
how to add a linked server using tsql , please check this link : how to find linked server
for an example purpose suppose i have named the linked server "DESTINATION_SERVER" , database name is "DESTINATION_DB" and table name is "DESTINATION_TBL". then from your source server your query could be like this below:
UPDATE t1
SET t1.updatecolumn = t2.updatecolumn
FROM [DESTINATION_SERVER].[DESTINATION_DB].[dbo].[DESTINATION_TBL] t1
INNER JOIN [SOURCE-SERVER].[SOURCE_DB].[dbo].
[SOURCE_TBL] t2 ON (t1.matcingcolumn = t2.matchingcolumn)
I believe you have to have a database link (linked servers) for this to work.
I do not have access to two SQL servers here at work so I cannot test it, but I sure that you need the link. Do you have a linked server setup?
Here is a url that may help http://msdn.microsoft.com/en-us/library/ms188279.aspx
Harvey Sather
You are using table alias in a wrong way. You cannot do UPDATE table1 t SET field1=val
, you have to write UPDATE table1 SET field=val
(Or UPDATE table1 SET field=val FROM table1 t
). So change your query to
UPDATE [spdbprod.test.com\spprod].[aspnetdb].[dbo].[Communities_Groups]
SET Show = t2.show
FROM [spdbprod.test.com\spprod].[aspnetdb].[dbo].[Communities_Groups] t1
INNER JOIN [spdbQA.test.com\spQA].[aspnetdb].[dbo].
[Communities_Groups] t2 ON (t1.GroupID = t2.GroupID)