简体   繁体   English

SQL:从不同的数据库获取实时数据

[英]SQL: Get real time data from different database

I want to insert data from database1 into database2 when something changes. 我想在发生某些变化时将数据从database1插入到database2中。 Like a trigger but the thing is I am not allowed to create a trigger in database1. 像触发器一样,但是事情是不允许我在database1中创建触发器。

I basically need to insert certain data into a table from a database into another database as they happen. 我基本上需要在发生某些情况时将某些数据从一个数据库插入到另一个数据库中的表中。

Any suggestions would be great! 任何建议都很好! Thanx 谢谢

What about the merge operator ? 合并运算符呢?

Merge operator will commit changes into the target table from the source table when something change. 发生更改时,合并运算符会将更改从源表提交到目标表中。

As you cannot use trigger this cannot be a real time process but with a process scheduler like SQL Agent, you can run it each 10 seconds depending on your table size. 由于您不能使用触发器,因此这不是实时进程,而是使用SQL Agent之类的进程调度程序,因此您可以每10秒运行一次,具体取决于表的大小。

https://msdn.microsoft.com/en-us/library/bb510625.aspx https://msdn.microsoft.com/zh-CN/library/bb510625.aspx

You can do it when your source come from multiple table by using CTE like that : 当您的源来自多个表时,可以使用CTE来做到这一点,例如:

With TableSources As
(
    Select id,Column1, Column2 from table1
    UNION 
    Select id,Colum1,Column2 from table2
)
MERGE INTO TargetTable
USING TableSources ON TargetTable.id=TableSources.id
WHEN NOT MATCHED BY Target
Insert(Column1,Column2) Values(Column1,Column2)
WHEN NOT MATCHED BY Source
Delete
WHEN MATCHED
Update Set Column1=TableSources.Column1, Column2=TableSources.Column2
;

You can create a program to do that, there are many ways instead but I think this is the straightforward and easy one. 您可以创建一个程序来做到这一点,相反,有很多方法,但是我认为这是简单明了的方法。 For example if you work with SQL server you can create C# code that connect to the two databases and check for data in the first one then send it to the second. 例如,如果您使用SQL Server,则可以创建连接两个数据库的C#代码,并在第一个数据库中检查数据,然后将其发送到第二个数据库。

For example: you can create a trigger for the first DB tables you want to check them, then you can create a web service that check if the trigger fire, it will get the data from first database and send it to the second, this will be better to enhance and you can change the code to do whatever you want without making any changes from the database side 例如:您可以为要检查的第一个数据库表创建触发器,然后可以创建一个Web服务,以检查触发器是否触发,它将从第一个数据库获取数据并将其发送到第二个数据库,这将更好地进行增强,您可以更改代码以执行所需的任何操作,而无需从数据库端进行任何更改

MS SQL Server Replication MS SQL Server复制

I think suitable for you is Transactional Replication Transactional replication is asynchronous but close to real time 我认为适合您的是事务复制事务复制是异步的,但接近实时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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