Search code examples
sql-serversql-updatessmsremote-serverdata-synchronization

Update a local db table from remote SQL Server db table


I would like to have a local copy of a remote db table, updated every N minutes. How can I accomplish this with sql management studio (possibly) without build a script from myself?

This is the table

enter image description here


Solution

  • The sql server import and export wizard seems to be not the right way for me: infact it does not support the import from a table to another of only new content.

    I wrote a simple procedure, for syncing a local table with a remote one, with only new data, depending on timestamp

    create procedure p_sync_from_remote
    as
    begin
        declare @lastitem Datetime = 
             (select top 1 Time_Stamp from table_destination order by Time_Stamp desc)
        if (@lastitem is null) or (len(@lastitem) <= 0)
        begin
            insert into table_destination
            select * from table_source
        end
        else
        begin
            insert into table_destination
            select * from table_source where Time_Stamp > @lastitem
        end
    end
    
    exec p_sync_from_remote