简体   繁体   中英

How to synchronize two tables stored in different databases

I have two tables in different databases. The tables are exactly alike (same name,same columns,etc). My question is, how can I retrieve new rows from parent table and store into the child table? I need to do so in a click_event of a button. Thanks in advance.

There are several technologies specifically for this type of scenario:

  • SQL Replication
    • Supports unidirectional or bidirectional synchronization
  • SSIS
    • Lets you define the mappings of the data, as well as transformations, and attach other code to the process easily
  • Linked-servers
    • Allows you to query databases and tables on remote servers as though they are part of the local database. Very easy to setup (just call exec sp_addlinkedserver ) and once defined uses nothing but plain old SQL

Since you mention this needs to occur on a button-click then I'd suggest you use linked servers within a stored procedure--they're the simplest option. SSIS would also be suitable, you'd need to execute the package on the button-click.

Resolved it myself using Linked Server. Here is a simple tutorial about how to create a linked server. After creating linked server, we can query it as follows:

select * from LinkedServerName.DatabaseName.dbo.TableName

Works just perfect!! Accepting STW's answer as he explains different approaches.

(long and non-optimal solution) get all id's from first table. get all id's from second table. loop through the first array and remove all items that are found in the second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM