Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Setting the Server within the code

Author  Topic 

SLM09
Starting Member

31 Posts

Posted - 2009-10-02 : 13:10:31
Is it possible to select the server with something like 'USE' from within the code so multiple queries accessing different servers can run one after another?

ie- something along the lines of:

USE ServerName1.DB1.table1
SELECT ...
USE ServerName2.DB2.table2
SELECT ...

I have found info how to do this from other languages which require a string with login and pw in it (which may not be required with windows authentication)... Just haven't been able to find anything in SQL.

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 13:13:47
nope this is not possible. you can change db however using use
Go to Top of Page

SLM09
Starting Member

31 Posts

Posted - 2009-10-02 : 13:17:57
Thanks visakh

I knew about changing the table and had high hopes for it to be able to handle servers as well. Oh well, maybe in a future release. :P

Go to Top of Page

mivey4
Yak Posting Veteran

66 Posts

Posted - 2009-10-02 : 13:24:28
You may want to consider the possibility of using linked servers to perform multiple queries across Servers though, this is possible.

You still won't be able to use the keyword USE but you will be able to do something like this to any Database Server from one system:

SELECT column1, column2
FROM [ServerName].[DatabaseName].dbo.[TableName]

Oracle OCA
Adaptec ACSP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-02 : 13:29:53
or you could even use ssis to change server connection and execute queries dynamically inside a loop container
Go to Top of Page
   

- Advertisement -