Thursday, February 03, 2005

Missing Link

Query multiple servers from one SQL logon with Linked Servers. Take two servers, and use one login in Query Analyzer to hit both at the same time. Here's how:

1. Set up a Linked server in the MMC. Right click on 'Linked Servers' on Server1 and add Server2 as an SQL server (or other data source, if you really want to.) Set up security either to impersonate the user or group involved, or to log on by default as a specific SQL account.

2. Open Query Analyzer and log into Server1. Write your statement as: SELECT * FROM [Server2].DBNAME.ObjectOwner.TableName. Example, "Select * from [server2].master.dbo.sysobjects" will return everything from Server2 , even though you only logged into, and were running against Server1.

Simple. Makes the idea of distributed SQL kind of fun. Next time, fun with JOIN statements, and multiple table queries.

No comments: