Update database table from one SQL Server database table to another?

后端 未结 4 1214
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 07:32

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

4条回答
  •  礼貌的吻别
    2021-02-05 08:19

    I know this has been answered already but this worked for me.

    • Add a Linked Server under Server Objects | Linked Servers: Microsoft documentation.
    • Name the Linked Server [SERVER-NAME or , ]

      e.g. [10.0.0.200,2345] - I am using port 2345 but the standard MS SQL port is 1433.

    Example:

    • We have a [Customers] table
    • We want to update [CustomerAddress]-field for CustomerId = 123
    • We want to use backup data from a server called [backupServer]
    • The [backupServer] is the machine where we execute the SQL

    This 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 = 
    

提交回复
热议问题