Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Linked Server Example Query

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 ?

like image 755
bmw0128 Avatar asked Nov 03 '10 21:11

bmw0128


People also ask

How do I query a linked SQL Server?

Right-click on the Linked Server node and choose New Linked Server. In the General tab of the New Linked Server window, choose a name for your linked server, then choose the type of the server you need to connect to using that linked server.

How do I get a list of linked servers in SQL Server?

Open SQL Server Management Studio; go to Server Objects -> Linked Server. Under Linked Server node we have Providers node that already having installed provides and that mapped to SQL Server.

How do I access a linked server database?

Open SQL Server Management Studio and connect to an instance of SQL Server. In the Object Explorer, expand the node for the SQL Server database. In the Server Objects node, right-click Linked Servers and click New Linked Server. The New Linked Server dialog is displayed.

What is a SQL Server linked server?

Linked servers enable the SQL Server Database Engine and Azure SQL Managed Instance to read data from the remote data sources and execute commands against the remote database servers (for example, OLE DB data sources) outside of the instance of SQL Server.


1 Answers

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.

like image 180
NotMe Avatar answered Sep 21 '22 06:09

NotMe