[英]How to Union different tables from two different databases?
I currently find myself stuck in a situation where I have to union different tables from two different databases.我目前发现自己陷入了必须将来自两个不同数据库的不同表联合起来的情况。 The other database served as an archive for records which are no longer accessed but still needed for reports.
另一个数据库用作不再访问但仍需要报告的记录的存档。 I am developing an interface where they could view historical reports.
我正在开发一个界面,他们可以在其中查看历史报告。 I am using MS SQL Server.
我正在使用 MS SQL Server。 Is it possible to do this?
是否有可能做到这一点?
Any help would be highly appreciated...任何帮助将不胜感激...
you need to use three part naming convention您需要使用三部分命名约定
ex:
database name:A
Schema name:dbo
table name test
You can refer like select top 10* from a.dbo.test您可以参考从 a.dbo.test 中选择前 10*
Similarly to union two different tables in two different databases,you can do like below与联合两个不同数据库中的两个不同表类似,您可以像下面这样
select top 10* from a.dbo.test
union
select top 10* from b.dbo.test
if those databases are in different server ( say server1 and server 2),you need to create linked server and you can access like below如果这些数据库在不同的服务器(比如 server1 和 server 2),你需要创建链接服务器,你可以像下面一样访问
select top 10* from server1.a.dbo.test
union
select top 10* from server2.a.dbo.test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.