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

SqlSql ServerDatabaseLinked Server

Sql Problem Overview


Suppose that I have a database which name is testdb in test server. I also have a database named proddb in prod server.

Now I want to select data of a table of testdb database from proddb database.

How can I do that in SQL Server?

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

Sql Solutions


Solution 1 - Sql

You need sp_addlinkedserver()

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]

Solution 2 - Sql

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

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

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

Solution 3 - Sql

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]

Solution 4 - Sql

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

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

Solution 5 - Sql

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 Server you have to specify desired server name for remote server, real server name or IP address (Data Source) and credentials (Security page).

And further you can select data from linked server:

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

Solution 6 - Sql

Try using OPENDATASOURCE The syntax is like this:

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    

Solution 7 - Sql

Select * from [Database].[dbo].[TableName]
select * from [dbTest].[dbo].[Products]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser82431View Question on Stackoverflow
Solution 1 - SqlMatthew FarwellView Answer on Stackoverflow
Solution 2 - SqlArthur RonaldView Answer on Stackoverflow
Solution 3 - SqlBrian WellsView Answer on Stackoverflow
Solution 4 - SqlMitch BakerView Answer on Stackoverflow
Solution 5 - SqlIhor KonovalenkoView Answer on Stackoverflow
Solution 6 - SqlAlirezaView Answer on Stackoverflow
Solution 7 - SqlArshman SaleemView Answer on Stackoverflow