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

7

74

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?

Keener answered 6/4, 2009 at 5:25 Comment(2)
If you're using SQL Server 2012 or above, jump down to https://mcmap.net/q/269815/-how-to-select-data-of-a-table-from-another-database-in-sql-server.Simonides
This same topic has been discussed also in https://mcmap.net/q/168437/-how-can-i-do-something-like-use-databasename/1548275Principle
M
72

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]
Muskogean answered 6/4, 2009 at 8:30 Comment(2)
If you need to pass through different credentials, use EXEC sp_addlinkedsrvlogin 'servername', 'false', NULL, 'SqlUser', 'password'Faefaeces
I was struggling to get this to work, found you can also do this using SSMS (sqlshack.com/…) which then shows you all the various security etc options that are available - i found i had to set set it to use the current logins security context to get it to work.Dustan
C
54

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

Class answered 13/12, 2016 at 17:45 Comment(2)
Doesn't work on Azure. If you need it in Azure, Try this: stackoverflow.com/questions/45715668Happ
This works in Azure now with the same user in both databasesPhenix
L
12

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]
Lexicon answered 4/5, 2011 at 14:13 Comment(0)
G
6

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]
Ghana answered 23/11, 2019 at 8:35 Comment(0)
B
5

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.

Boots answered 6/4, 2009 at 5:28 Comment(2)
The tables are in different server.Keener
How can I link between to servers and how can I select data? please provide steps. thanksKeener
B
2
Select * from [Database].[dbo].[TableName]
select * from [dbTest].[dbo].[Products]
Boxwood answered 10/8, 2021 at 11:16 Comment(0)
H
1

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    
Halt answered 26/11, 2019 at 11:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.