January 4, 2013 at 10:02 am
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;
January 4, 2013 at 11:55 am
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');
January 4, 2013 at 12:06 pm
I just finished looking into this! 🙂
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].
:connect <ServerName2>
use [Database2]
go
SELECT * FROM [dbo].[TABLE2]
January 4, 2013 at 1:20 pm
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.
January 7, 2013 at 4:23 am
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?
January 7, 2013 at 4:36 am
If this is an ad hoc query you wish to run against multiple servers, you might consider setting up a Central Management Server.
John
January 8, 2013 at 1:26 am
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.aspxeg. 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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
January 8, 2013 at 1:31 am
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
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply