Reputation: 13698
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 ?
Upvotes: 118
Views: 748440
Reputation: 11
I'm connecting to a MySQL linked server. Turned out that the schema name is "public", which in T-SQL is a reserved word. In SSMS, it's seen as:
HostServerName\DatabaseName > Server Objects > Linked Servers > LinkedServerName > Catalogs > LinkedServerName > Tables > public.TableName
Once I surrounded each section in square brackets, that worked.
Select * FROM [LinkedServerName].[LinkedServerName].[public].[TableName]
If I tried to group the entire tablename together ("[public.TableName]"), that failed. The keyword "public" had to be separated by itself. The other components worked either with or without the brackets, since they didn't involve punctuation, spaces, keywords, or anything else to cause problems.
What was frustrating was that Script Table As > Select To failed, either to a window or to the clipboard, with an error of "Enumerate columns failed", without showing the specific failed command or the failed column. When I use a generic Select like the one above, that works - once I get around the reserved keyword.
Some may consider this a dead thread, but it was a top Google result. Maybe this will help someone else with the same issue.
Upvotes: 0
Reputation: 31
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
Upvotes: 0
Reputation: 8714
In sql-server(local) there are two ways to query data from a linked server(remote).
Distributed query (four part notation):
SELECT * FROM [SERVER_NAME].[DATABASE_NAME].[SCHEMA_NAME].[TABLE_NAME]
OPENQUERY
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.
Upvotes: 2
Reputation: 121
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:
You can then query any schema and table using the following syntax:
SELECT TOP 10 *
FROM LinkedServerName...[SchemaName.TableName]
Source: SELECT * FROM MySQL Linked Server using SQL Server without OpenQuery
Upvotes: 0
Reputation: 787
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
Upvotes: 1
Reputation: 13248
PostgreSQL:
You must omit the DBName from the query:
SELECT * FROM OPENQUERY([LinkedServer], 'select * from schema."tablename"')
Upvotes: 0
Reputation: 88064
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.
Upvotes: 226
Reputation: 20401
For those having trouble with these other answers , try OPENQUERY
Example:
SELECT * FROM OPENQUERY([LinkedServer], 'select * from [DBName].[schema].[tablename]')
Upvotes: 17
Reputation: 231
If you still find issue with <server>.<database>.<schema>.<table>
Enclose server name in []
Upvotes: 11
Reputation: 292
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
Upvotes: 0
Reputation: 41
select name from drsql01.test.dbo.employee
I hope it helps to understand, how to execute query for linked server
Upvotes: 4
Reputation: 21
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.
Upvotes: 2
Reputation: 815
SELECT * FROM OPENQUERY([SERVER_NAME], 'SELECT * FROM DATABASE_NAME..TABLENAME')
This may help you.
Upvotes: 51
Reputation: 1249
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
Upvotes: 8
Reputation: 741
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')
Upvotes: 3
Reputation: 135808
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
Upvotes: 10