如何在来自SQL Server Management Studio的两个不同服务器和数据库的相同查询中选择数据?

时间:2022-05-19 09:51:31

How can I select data in the same query from two different databases that are on two different servers, one DB2 Server and the other a SQL Server?

如何从两个不同的服务器、一个DB2服务器和另一个SQL服务器上的两个不同的数据库中选择相同的查询中的数据呢?

2 个解决方案

#1


1  

On your sql server, set up a linked server to the db2 database.

在您的sql服务器上,将连接的服务器设置为db2数据库。

Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.

然后在sql server上编写查询。我建议您对db2内容使用openquery。如果必须将数据组合起来,就可以使用openquery结果填充sql server temp表,并在那里工作。

The reason I suggest this is performance. I have found that if you use this syntax

我认为这是表现的原因。我发现,如果使用这种语法。

select somefields
from server.database.owner.table
where whatever

sql server will bring back the entire table from the linked server and apply the where clause afterwards.

sql server将从链接服务器返回整个表,然后应用where子句。

#2


1  

You can set up a linked server http://support.microsoft.com/kb/222937

您可以设置一个链接的服务器http://support.microsoft.com/kb/222937。

How to create a linked server

如何创建链接服务器?

#1


1  

On your sql server, set up a linked server to the db2 database.

在您的sql服务器上,将连接的服务器设置为db2数据库。

Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.

然后在sql server上编写查询。我建议您对db2内容使用openquery。如果必须将数据组合起来,就可以使用openquery结果填充sql server temp表,并在那里工作。

The reason I suggest this is performance. I have found that if you use this syntax

我认为这是表现的原因。我发现,如果使用这种语法。

select somefields
from server.database.owner.table
where whatever

sql server will bring back the entire table from the linked server and apply the where clause afterwards.

sql server将从链接服务器返回整个表,然后应用where子句。

#2


1  

You can set up a linked server http://support.microsoft.com/kb/222937

您可以设置一个链接的服务器http://support.microsoft.com/kb/222937。

How to create a linked server

如何创建链接服务器?