SQL 如何从SQL Server中的另一个数据库中选择表的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/720339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:37:34  来源:igfitidea点击:

How to select data of a table from another database in SQL Server?

sqlsql-serverdatabaselinked-server

提问by user82431

Suppose, I have a database named testdbin test server. Also I have a database named proddbin prodserver.

假设,我有一个名为数据库testdb中的测试服务器。另外我在prod服务器中有一个名为proddb的数据库。

Now I want to select data of a table of testdbdatabase from proddbdatabase.

现在我想从proddb数据库中选择testdb数据库表的数据。

How can I do that in SQL Server?

我怎样才能做到这一点SQL Server

Also, I can do it using database linkin oracle. But how can do that in SQL Server?

另外,我可以使用oracle 中的数据库链接来完成。但是如何在SQL Server 中做到这一点?

回答by Matthew Farwell

You need sp_addlinkedserver()

你需要 sp_addlinkedserver()

http://msdn.microsoft.com/en-us/library/ms190479.aspx

http://msdn.microsoft.com/en-us/library/ms190479.aspx

Example:

例子:

exec sp_addlinkedserver @server = 'test'

then

然后

select * from [server].[database].[schema].[table]

In your example:

在你的例子中:

select * from [test].[testdb].[dbo].[table]

回答by Arthur Ronald

In SQL Server 2012 and above, you don't need to create a link. You can execute directly

在 SQL Server 2012 及更高版本中,您不需要创建链接。可以直接执行

SELECT * FROM [TARGET_DATABASE].dbo.[TABLE] AS _TARGET

I don't know whether previous versions of SQL Server work as well

我不知道以前版本的 SQL Server 是否也能正常工作

回答by Brian Wells

I've used this before to setup a query against another server and db via linked server:

我之前使用过它通过链接服务器设置对另一台服务器和数据库的查询:

EXEC sp_addlinkedserver @server='PWA_ProjectServer', @srvproduct='',
@provider='SQLOLEDB', @datasrc='SERVERNAME\PWA_ProjectServer'

per the comment above:

根据上面的评论:

select * from [server].[database].[schema].[table]

e.g.

例如

select top 6 * from [PWA_ProjectServer].[PWA_ProjectServer_Reporting].[dbo].[MSP_AdminStatus]

回答by Mitch Baker

To do a cross server query, check out the system stored procedure: sp_addlinkedserverin the help files.

要进行跨服务器查询,请查看帮助文件中的系统存储过程:sp_addlinkedserver

Once the server is linked you can run a query against it.

链接服务器后,您可以对其运行查询。

回答by Ihor Konovalenko

Using Microsoft SQL Server Management Studio you can create Linked Server. First make connection to current (local) server, then go to Server Objects> Linked Servers> context menu > New Linked Server. In window New Linked Serveryou have to specify desired server name for remote server, real server name or IP address (Data Source) and credentials (Security page).

使用 Microsoft SQL Server Management Studio,您可以创建Linked Server. 首先连接到当前(本地)服务器,然后转到Server Objects> Linked Servers> 上下文菜单 > New Linked Server。在窗口中,New Linked Server您必须为远程服务器指定所需的服务器名称、真实服务器名称或 IP 地址(数据源)和凭据(安全页面)。

And feather you can select data from linked server:

你可以从链接服务器中选择数据:

select * from [linked_server_name].[database].[schema].[table]

回答by Alireza

Try using OPENDATASOURCE The syntax is like this:

尝试使用 OPENDATASOURCE 语法是这样的:

select * from OPENDATASOURCE ('SQLNCLI', 'Data Source=192.168.6.69;Initial Catalog=AnotherDatabase;Persist Security Info=True;User ID=sa;Password=AnotherDBPassword;MultipleActiveResultSets=true;' ).HumanResources.Department.MyTable