且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们可以对两个不同的数据库表使用连接吗?

更新时间:2021-09-09 22:03:58

SQL Server 允许您连接来自不同数据库的表,只要这些数据库位于同一服务器上.连接语法相同;唯一的区别是您必须完全指定表名.

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully specify table names.

假设您在同一台服务器上有两个数据库 - Db1Db2.Db1 有一个名为 Clients 的表,其中有一列 ClientIdDb2 有一个名为 Messages的表code> 带有 ClientId 列(让我们暂且不谈为什么这些表位于不同的数据库中).

Let's suppose you have two databases on the same server - Db1 and Db2. Db1 has a table called Clients with a column ClientId and Db2 has a table called Messages with a column ClientId (let's leave asside why those tables are in different databases).

现在,要对上述表执行连接,您将使用此查询:

Now, to perform a join on the above-mentioned tables you will be using this query:

select *
from Db1.dbo.Clients c
join Db2.dbo.Messages m on c.ClientId = m.ClientId