如何同步两个数据库表,其中一个数据库是活动的而另一个数据库不是 [英] How to synchronize two database tables where one database is live and other one is not

查看:79
本文介绍了如何同步两个数据库表,其中一个数据库是活动的而另一个数据库不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo ALL,



如何同步共享同一服务器的两个数据库。我在每个数据库中都有表。如果我的实时数据库在其表中有一些新条目。我如何与其他数据库表进行比较并将其添加到另一个表中。 ?



例如,如何在表2中插入表1C,D,E中新添加的数据





谢谢!



我的尝试:



我尝试过的事情:

Hallo ALL,

How can i synchronize two databases sharing the same server. I have tables in each database. If my live database has some new entries in its tables. How can i compare with my other database table and add it to the other table. ?

For example, how can i have the data which is added new in table1 "C,D,E" inserted in Table 2


Thank you!

What I have tried:

Things i have tried:

INSERT INTO  [Database2].[dbo].[Table2]
SELECT * FROM [database1].[dbo].[Table1]





此查询插入所有数据table1再次,怎么能一直同步,以防万一Table1中有任何新数据,它更新到table2



表1:实时数据



this query inserts all data in the table1 again, how can it be synchronized all time so that in case if there is any new data in Table1 it updates into table2

Table 1 : LIVE DATA

| Field1 | Field2 | Field3 |
 | ------ | ------ | -----: |
 |  A     |   1    |  10   |
 |  B     |   2    |  20   |
 |  C     |   3    |  30   |
 |  D     |   4    |  40   |
 |  E     |   5    |  50   |





表2:



Table 2 :

| Field1 | Field2 | Field3 |
 | ------ | ------ | -----: |
 |  A     |   1    |   10   |
 |  B     |   2    |   20   |

推荐答案

似乎,你在谈论数据库镜像(SQL Server)| Microsoft Docs [ ^ ],这是您的最佳选择。

如果您的非实时数据库位于同一台服务器上,您可能需要创建存储过程 [ ^ ]其中插入数据进入实时数据库,然后进入镜像数据库。
Seems, you're talking about Database Mirroring (SQL Server) | Microsoft Docs[^], which is the best option to you.
If your non-live database is on the same server, you may want to create a stored procedure[^] which inserts data into live database, then into the mirroring database.


如果你在Field1字段中有不同的值,你可以试试这个



INSERT INTO [Database2]。[dbo]。[表2]



SELECT * FROM [database1]。[dbo]。[Table1] with (nolock)

其中Field1不在(选择Field1 FROM [Database2]。[dbo]。[Table2] with(nolock))
If you have distinct value in Field1 field you can try this

INSERT INTO [Database2].[dbo].[Table2]

SELECT * FROM [database1].[dbo].[Table1] with(nolock)
where Field1 not in (select Field1 FROM [Database2].[dbo].[Table2] with(nolock) )


这篇关于如何同步两个数据库表,其中一个数据库是活动的而另一个数据库不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆