0

I want to be able to execute remote queries based on the results of a local query.

For instance:

DECLARE @REMOTESERVER VARCHAR(10)

Select TOP 1 @REMOTESERVER = RemoteServer from TABLE

--Execute the next query on a remote server from the value I retrieved above

Select * from tblCustomers
paul
  • 50
  • 8

1 Answers1

1

What RDBMS are you using? Some will not support a pure sql way of doing this. Others, like SQL Server, might support this scenario. Is the remote server accessible via a linked server that you can access. You could then use dynamic sql to create your sql string. Something like this should work in SQL Server:

SET @Sql = 'SELECT * FROM [' + @RemoteServer + '].dbname.schema.tblCustomers'
EXEC @Sql

Here is a post about linked servers: https://stackoverflow.com/a/4091984/1073631

Community
  • 1
  • 1
sgeddes
  • 62,311
  • 6
  • 61
  • 83
  • Yes, that worked thanks! The only thing I had to do differently was wrap the exec variable as such: EXEC(@Sql) Thanks! – paul Jan 11 '13 at 15:08