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 ?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With