SQL MERGE to remote (linked) server table

前端 未结 3 2178
死守一世寂寞
死守一世寂寞 2021-02-13 22:23

Is it possible to utilize the SQL MERGE function on a linked server\'s database table? The end goal is to synchronize the remote table with our local SQL server table. I’ve done

相关标签:
3条回答
  • 2021-02-13 22:34

    To reiterate the comment by @Mikael Eriksson, yes, you can. The target of a MERGE cannot be remote, but the source of a MERGE can be remote. So, if you can run the MERGE statement from your server in FL, then it is quite possible. For example, you could run something like this on your remove server in FL:

    MERGE INTO "local FL table" USING "CT server"."database"."schema"."same table" ON ...
    
    0 讨论(0)
  • 2021-02-13 22:39

    Yoy can always use EXEC('SQL CODE HERE')AT YOUR_LINKED_SERVER in your server, maybe as a Stored Procedure.

    This will execute the query you want on your linked server so you could merge a local table (target_table) with a server table (source).

    This is a code I use in a Stored Procedure in my Server that its called from the client. Client exec stored procedure in server->Server Exec Query to update different linked servers (clients) with the same informacion (employees)

        EXEC('
    SET IDENTITY_INSERT PVBC.DBO.empleadas ON
    
    MERGE INTO PVBC.DBO.empleadas A
    USING(
        SELECT id_empleada, nombre, apellidos
        FROM SERVIDOR.PVBC_SERVIDOR.DBO.empleadas) TA
    ON (A.id_empleada =TA.id_empleada)
    WHEN MATCHED THEN
    UPDATE 
        SET A.nombre=TA.nombre,
            A.apellidos=TA.apellidos
    WHEN NOT MATCHED THEN
    
        INSERT 
            (id_empleada, nombre, apellidos)
        VALUES
            (id_empleada, nombre, apellidos);
    
    SET IDENTITY_INSERT PVBC.DBO.empleadas OFF
    ')AT MEGA --This is one of my linked servers
    
    0 讨论(0)
  • 2021-02-13 23:01

    Apparently my research wasn't good enough, it’s stated right on MSDN: “target_table cannot be a remote table” ... so that answers this question...

    0 讨论(0)
提交回复
热议问题