问题描述
我们可以对来自不同数据库的两个表使用连接操作吗?如果是,我该怎么做?
Can we use the join operation for two tables from different databases? If yes, how do I do it?
两个数据库在同一台服务器上,DBMS 是一样的.
Both databases are on the same server and DBMS is the same.
推荐答案
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.
假设您在同一台服务器上有两个数据库 - Db1
和 Db2
.Db1
有一个名为 Clients
的表,其中有一列 ClientId
,Db2
有一个名为 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
这篇关于我们可以对两个不同的数据库表使用连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!