title: can one remote execute queries from within t-sql?
body: <p>First the raw question:</p>
  
  <p>Given that there are 2 DBs: DB1 and DB2. Can I, being connected to DB1, perform a query on DB2 as if I'm logged on to DB2? 
  So basically:</p>
  
  <ul>
  <li>I'm connected to DB1</li>
  <li>From within TSQL I somehow connect to DB2</li>
  <li>I perform the query locally (and wait)</li>
  <li>I close the connection and am back on DB1</li>
  <li>and I do the remainder of the query.</li>
  </ul>
  
  <p>SO I know I can do interDB queries like: select * from DB2.somedb.dbo.sometable but this is not what I'm after.</p>
  
  <p>Background:</p>
  
  <p>Is have data sitting on 2 separate DB machines. I have to query the data in such a way as to combine data from tables on both machines.</p>
  
  <p>I have tried lot's of scenarios and everytime it either runs very slow, or a query can't be done due to XML columns being present in one table or the other.</p>
  
  <p>The solution which does work (although manually) is:</p>
  
  <ul>
  <li>log on to DB2 and do half of the query into some temp table</li>
  <li>copy the temptable over to DB1</li>
  <li>logon to db1 and perform the second part of the query.</li>
  </ul>
  
  <p>perferably I want to make this all one script so I can automate it</p>