如何在 SQL Server 中创建和查询链接的数据库服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/189422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create and query linked database servers in SQL Server?
提问by Kalid
I need to do a join across two different database servers (IPs 10.0.0.50 and 10.0.0.51). What's the best way?
我需要在两个不同的数据库服务器(IP 10.0.0.50 和 10.0.0.51)之间进行连接。最好的方法是什么?
采纳答案by Ben Hoffstein
You need to use sp_linkedserver to create a linked server.
您需要使用 sp_linkedserver 创建链接服务器。
sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ]
[ , [ @provider= ] 'provider_name' ]
[ , [ @datasrc= ] 'data_source' ]
[ , [ @location= ] 'location' ]
[ , [ @provstr= ] 'provider_string' ]
[ , [ @catalog= ] 'catalog' ]
More information available on MSDN.
MSDN 上提供了更多信息。
回答by Kalid
The solution I found:
我找到的解决方案:
1) Run a stored proc
1)运行一个存储过程
exec sp_addlinkedserver @server='10.0.0.51'
2) Verify that the servers were linked (lists linked servers)
2) 验证服务器是否已链接(列出链接的服务器)
exec sp_linkedservers
3) Run the query using the format
3) 使用格式运行查询
[10.0.0.51].DatabaseName.dbo.TableName
回答by MrSimpleMind
I know that the answers above are good, but wanted to share some details that I hope others will find helpful. Worth to mention is the user access part, which I think people will need help with.
我知道上面的答案很好,但想分享一些细节,希望其他人会觉得有帮助。值得一提的是用户访问部分,我认为人们需要帮助。
set up the link:
设置链接:
exec sp_addlinkedserver @server='10.10.0.10\MyDS';
exec sp_addlinkedserver @server='10.10.0.10\MyDS';
set up the access for remote user, example below:
设置远程用户的访问权限,示例如下:
exec sp_addlinkedsrvlogin '10.10.0.10\MyDS', 'false', null, 'adm', 'pwd';
exec sp_addlinkedsrvlogin '10.10.0.10\MyDS', 'false', null, 'adm', 'pwd';
see the linked servers and user logins:
查看链接的服务器和用户登录:
exec sp_linkedservers;
exec sp_linkedservers;
select * from sys.servers;
select * from sys.servers;
select * from sys.linked_logins;
select * from sys.linked_logins;
run the remote query:
运行远程查询:
select * from [10.10.0.10\MyDS].MyDB.dbo.TestTable;
select * from [10.10.0.10\MyDS].MyDB.dbo.TestTable;
drop the linked server and the created login users (adm/pwd)
删除链接服务器和创建的登录用户 (adm/pwd)
exec sp_dropserver '10.10.0.10\MyDS', 'droplogins'; -- drops server and logins
exec sp_dropserver '10.10.0.10\MyDS', 'droplogins'; -- drops server and logins
resources:
资源:
回答by Pittsburgh DBA
You can, as mentioned, use sp_addlinkedserver. However, you may also do this via Enterprise Manager (2000) or SQL Server Management Studio (2005). Under the "Security" node, there is a "Linked Servers" node, which you can use to add and configure Linked Servers. You can specify security settings, impersonation, etc.
如前所述,您可以使用 sp_addlinkedserver。但是,您也可以通过 Enterprise Manager (2000) 或 SQL Server Management Studio (2005) 执行此操作。在“安全”节点下,有一个“链接服务器”节点,您可以使用它来添加和配置链接服务器。您可以指定安全设置、模拟等。
See these for SQL Server 2000:
对于 SQL Server 2000,请参阅以下内容:
Establishing Security For Linked Servers
Configuring OLEDB Providers for Distributed Queries
See these for SQL Server 2005:
请参阅 SQL Server 2005 的这些:
Configuring Linked Servers for Delegation