SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Connecting to multiple sql servers


Connecting to multiple sql servers

Author
Message
alex 64682
alex 64682
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 6
Hi,

I'd like to connect to multiple databases on different servers all from the same query. So, is it possible to specify a connection string when using the "USE" keyword?

e.g.

USE Data Source=11.11.111.11,111;Initial Catalog=Blah;User ID=Blah
Select * from Blah;
USE Data Source=22.22.22.22,222;Initial Catalog=Blah;User ID=Blah
Select * from Blah;
e4d4
e4d4
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1109 Visits: 2399
You could use linkedservers, openrowset http://msdn.microsoft.com/en-us/library/ms190312.aspx or openquery http://msdn.microsoft.com/en-us/library/ms188427.aspx

eg. openrowset
SELECT * FROM OPENROWSET('SQLOLEDB', 'Server=HOSTNAME;UID=DB_USER;PWD=StronPassword;','select * table');


fkominsky@hotmail.com
fkominsky@hotmail.com
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 233
I just finished looking into this! Smile

You can use "SQLCMD Mode" in SSMS. To enable it go to the Query menu and select "SQLCMD Mode"

:connect <ServerName>\<InstanceName>
use [Database]
go
SELECT * FROM [dbo].[TABLE]

:connect <ServerName2>
use [Database2]
go
SELECT * FROM [dbo].[TABLE2]
Greg Snidow
Greg Snidow
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4205 Visits: 2494
alex, I think e4d4's suggestion is probably your best (and maybe only) option. Once you set up the linked servers, you can query against them like this...

SELECT a.<column_list>, b.<column_list>
FROM Linkedserver1.catalog.schema.table a INNER JOIN
Linkedserver2.catalog.schema.table b
ON some join criteria



I'm not sure if the catalog is required for linked servers to other SQL Servers, but I have them set up to some DB2 and Oracle databases without the catalog, and I am able to bring them into the same query just fine. One caveat, though, the queries take an extremely long time to run. Now, I don't have exposure to the remote catalogs, so I'm not sure if there might be some index info in there that would make them run faster if I could read them. In general, using OPENQUERY(Linkedserver,'your SQL') is much faster than using the 4 part naming convention if you are only hitting one remote.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
alex 64682
alex 64682
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 6
Many thanks for all the responses. I haven't encountered Openrowset before but it looks like the best option. Not entirely sure where to place the remote IP Address, port number etc for the connection string though, don't suppose anyone has examples of this type of thing?
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33317 Visits: 16649
If this is an ad hoc query you wish to run against multiple servers, you might consider setting up a Central Management Server.

John
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17081 Visits: 6431
alex 64682 (1/7/2013)
Many thanks for all the responses. I haven't encountered Openrowset before but it looks like the best option. Not entirely sure where to place the remote IP Address, port number etc for the connection string though, don't suppose anyone has examples of this type of thing?


I believe that the IP address can be part of the HOSTNAME shown by e4d4 in his example below.

e4d4 (1/4/2013)
You could use linkedservers, openrowset http://msdn.microsoft.com/en-us/library/ms190312.aspx or openquery http://msdn.microsoft.com/en-us/library/ms188427.aspx

eg. openrowset
SELECT * FROM OPENROWSET('SQLOLEDB', 'Server=HOSTNAME;UID=DB_USER;PWD=StronPassword;','select * table');



Beware though, that the arguments passed to OPENROWSET must be string literals; no local variables are allowed.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
anthony.green
anthony.green
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23348 Visits: 6519
John Mitchell-245523 (1/7/2013)
If this is an ad hoc query you wish to run against multiple servers, you might consider setting up a Central Management Server.

John


+1 to that



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search