3

I've already run the following command to include another server instance.

EXEC sp_addlinkedserver @server='Server'

Now I'm trying to synchronize these databases using this:

UPDATE
    [Server].[ServerDB].[dbo].[tableName] 
SET 
    [Server].[ServerDB].[dbo].[tableName].[columnName] = [LocalDB].[dbo].[tableName].[columnName]
FROM 
    [Server].[ServerDB].[dbo].[tableName], [LocalDB].[dbo].[tableName]
WHERE 
    [Server].[ServerDB].[dbo].[tableName].id = [LocalDB].[dbo].[tableName].id  

This gave me the following error:

The objects "LocalDB.dbo.tableName" and "Server.ServerDB.dbo.tableName" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

So after fiddling around with it I tried various renditions of this:

UPDATE
    [Server].[ServerDB].[dbo].[tableName] 
SET 
    [Server].[ServerDB].[dbo].[tableName].[columnName] = [LocalDB].[dbo].[tableName].[columnName]
FROM 
    [Server].[ServerDB].[dbo].[tableName] serverDB
INNER JOIN
     [LocalDB].[dbo].[tableName] localDB
ON 
     serverDB.id = localDB.id 

But they all give me some sort of rendition of the error:

The multi-part identifier "Server.ServerDB.dbo.tableName.allrows" could not be bound.

Can someone spot what I'm missing?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120

2 Answers2

3

You need to use this syntax when updating with a join:

UPDATE s
SET s.[columnName] = l.[columnName]
FROM 
    [Server].[ServerDB].[dbo].[tableName] s
INNER JOIN
     [LocalDB].[dbo].[tableName] l
ON 
     l.id = s.id 
Donal
  • 31,121
  • 10
  • 63
  • 72
  • 1
    Okay, thanks! I'm new to SQL and that looks odd to me because it seems like you're using a variable name `s` and `l` in the `SET` before defining them in the `FROM` and `INNER JOIN` – Charles Clayton Jun 17 '15 at 17:08
  • Yes, the syntax for update with join was the issue. It is a bit wierd in SQL Server. s and l are called aliases, you have used serverDB and localDB, I used s and l - but you can use whatever alias you want. – Donal Jun 17 '15 at 17:12
2
UPDATE
    serverDB
SET 
    [columnName] = localdb.[columnName]
FROM 
    [Server].[ServerDB].[dbo].[tableName] serverDB
INNER JOIN
     [LocalDB].[dbo].[tableName] localDB
ON 
     serverDB.id = localDB.id
Stuart Ainsworth
  • 12,792
  • 41
  • 46