SQL Server Linked Server Example Query

Sql ServerLinked Server

Sql Server Problem Overview


While in Management Studio, I am trying to run a query/do a join between two linked servers. Is this a correct syntax using linked db servers:

select foo.id 
from databaseserver1.db1.table1 foo, 
     databaseserver2.db1.table1 bar 
where foo.name=bar.name

Basically, do you just preface the db server name to the db.table ?

Sql Server Solutions


Solution 1 - Sql Server

The format should probably be:

<server>.<database>.<schema>.<table>

For example: DatabaseServer1.db1.dbo.table1


Update: I know this is an old question and the answer I have is correct; however, I think any one else stumbling upon this should know a few things.

Namely, when querying against a linked server in a join situation the ENTIRE table from the linked server will likely be downloaded to the server the query is executing from in order to do the join operation. In the OP's case, both table1 from DB1 and table1 from DB2 will be transferred in their entirety to the server executing the query, presumably named DB3.

If you have large tables, this may result in an operation that takes a long time to execute. After all it is now constrained by network traffic speeds which is orders of magnitude slower than memory or even disk transfer speeds.

If possible, perform a single query against the remote server, without joining to a local table, to pull the data you need into a temp table. Then query off of that.

If that's not possible then you need to look at the various things that would cause SQL server to have to load the entire table locally. For example using GETDATE() or even certain joins. Others performance killers include not giving appropriate rights.

See http://thomaslarock.com/2013/05/top-3-performance-killers-for-linked-server-queries/ for some more info.

Solution 2 - Sql Server

SELECT * FROM OPENQUERY([SERVER_NAME], 'SELECT * FROM DATABASE_NAME..TABLENAME')

This may help you.

Solution 3 - Sql Server

For those having trouble with these other answers , try OPENQUERY

Example:

 SELECT * FROM OPENQUERY([LinkedServer], 'select * from [DBName].[schema].[tablename]') 

Solution 4 - Sql Server

If you still find issue with <server>.<database>.<schema>.<table>

Enclose server name in []

Solution 5 - Sql Server

You need to specify the schema/owner (dbo by default) as part of the reference. Also, it would be preferable to use the newer (ANSI-92) join style.

select foo.id 
    from databaseserver1.db1.dbo.table1 foo
        inner join databaseserver2.db1.dbo.table1 bar 
            on foo.name = bar.name

Solution 6 - Sql Server

select * from [Server].[database].[schema].[tablename] 

This is the correct way to call. Be sure to verify that the servers are linked before executing the query!

To check for linked servers call:

EXEC sys.sp_linkedservers 

Solution 7 - Sql Server

select name from drsql01.test.dbo.employee
  • drslq01 is servernmae --linked serer
  • test is database name
  • dbo is schema -default schema
  • employee is table name

I hope it helps to understand, how to execute query for linked server

Solution 8 - Sql Server

Usually direct queries should not be used in case of linked server because it heavily use temp database of SQL server. At first step data is retrieved into temp DB then filtering occur. There are many threads about this. It is better to use open OPENQUERY because it passes SQL to the source linked server and then it return filtered results e.g.

SELECT *
FROM OPENQUERY(Linked_Server_Name , 'select * from TableName where ID = 500')

Solution 9 - Sql Server

right click on a table and click script table as select

enter image description here

Solution 10 - Sql Server

For what it's worth, I found the following syntax to work the best:

SELECT * FROM [LINKED_SERVER]...[TABLE]

I couldn't get the recommendations of others to work, using the database name. Additionally, this data source has no schema.

Solution 11 - Sql Server

I have done to find out the data type in the table at link_server using openquery and the results were successful.

SELECT * FROM OPENQUERY (LINKSERVERNAME, '
SELECT DATA_TYPE, COLUMN_NAME
FROM [DATABASENAME].INFORMATION_SCHEMA.COLUMNS
WHERE 
     TABLE_NAME  =''TABLENAME''
')

Its work for me

Solution 12 - Sql Server

In sql-server(local) there are two ways to query data from a linked server(remote).

Distributed query (four part notation):

  1. Might not work with all remote servers. If your remote server is MySQL then distributed query will not work.
  2. Filters and joins might not work efficiently. If you have a simple query with WHERE clause, sql-server(local) might first fetch entire table from the remote server and then apply the WHERE clause locally. In case of large tables this is very inefficient since a lot of data will be moved from remote to local. However this is not always the case. If the local server has access to remote server's table statistics then it might be as efficient as using openquery More details
  3. On the positive side T-SQL syntax will work.

SELECT * FROM [SERVER_NAME].[DATABASE_NAME].[SCHEMA_NAME].[TABLE_NAME] 

OPENQUERY

  1. This is basically a pass-through. The query is fully processed on the remote server thus will make use of index or any optimization on the remote server. Effectively reducing the amount of data transferred from the remote to local sql-server.
  2. Minor drawback of this approach is that T-SQL syntax will not work if the remote server is anything other than sql-server.

SELECT * FROM OPENQUERY([SERVER_NAME], 'SELECT * FROM DATABASE_NAME.SCHEMA_NAME.TABLENAME')

Overall OPENQUERY seems like a much better option to use in majority of the cases.

Solution 13 - Sql Server

Following Query is work best.

> Try this Query:

SELECT * FROM OPENQUERY([LINKED_SERVER_NAME], 'SELECT * FROM [DATABASE_NAME].[SCHEMA].[TABLE_NAME]')

> It Very helps to link MySQL to MS SQL

Solution 14 - Sql Server

PostgreSQL:

  1. You must provide a database name in the Data Source DSN.

  2. Run Management Studio as Administrator

  3. You must omit the DBName from the query:

    SELECT * FROM OPENQUERY([LinkedServer], 'select * from schema."tablename"')

Solution 15 - Sql Server

For MariaDB (and so probably MySQL), attempting to specify the schema using the three-dot syntax did not work, resulting in the error "invalid use of schema or catalog". The following solution worked:

  1. In SSMS, go to Server Objects > Linked Servers > Providers > MSDASQL
  2. Ensure that "Dynamic parameter", "Level zero only", and "Allow inprocess" are all checked

You can then query any schema and table using the following syntax:

SELECT TOP 10 *
FROM LinkedServerName...[SchemaName.TableName]

Source: https://stackoverflow.com/questions/31968343/select-from-mysql-linked-server-using-sql-server-without-openquery

Solution 16 - Sql Server

Have you tried adding " around the first name?

like:

select foo.id 
from "databaseserver1".db1.table1 foo, 
     "databaseserver2".db1.table1 bar 
where foo.name=bar.name

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
Questionbmw0128View Question on Stackoverflow
Solution 1 - Sql ServerNotMeView Answer on Stackoverflow
Solution 2 - Sql ServerAkhilesh KamateView Answer on Stackoverflow
Solution 3 - Sql ServerTom StickelView Answer on Stackoverflow
Solution 4 - Sql ServerMianView Answer on Stackoverflow
Solution 5 - Sql ServerJoe StefanelliView Answer on Stackoverflow
Solution 6 - Sql ServerAbhishek JaiswalView Answer on Stackoverflow
Solution 7 - Sql ServerJaspreet SinghView Answer on Stackoverflow
Solution 8 - Sql ServerMuhammad YaseenView Answer on Stackoverflow
Solution 9 - Sql ServerShimon DoodkinView Answer on Stackoverflow
Solution 10 - Sql ServerSean WarrenView Answer on Stackoverflow
Solution 11 - Sql ServerAgungCode.ComView Answer on Stackoverflow
Solution 12 - Sql Serverns94View Answer on Stackoverflow
Solution 13 - Sql ServerVijay SView Answer on Stackoverflow
Solution 14 - Sql ServerShadi NamroutiView Answer on Stackoverflow
Solution 15 - Sql ServernovogView Answer on Stackoverflow
Solution 16 - Sql ServerCodePrime8View Answer on Stackoverflow