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
1
  • 1
    What RDBMS are you using? Commented Jan 10, 2013 at 18:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that worked thanks! The only thing I had to do differently was wrap the exec variable as such: EXEC(@Sql) Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.