Search code examples
sqlsql-serverlinked-servertemp-tables

Is it possible to create a temp table on a linked server?


I'm doing some fairly complex queries against a remote linked server, and it would be useful to be able to store some information in temp tables and then perform joins against it - all with the remote data. Creating the temp tables locally and joining against them over the wire is prohibitively slow.

Is it possible to force the temp table to be created on the remote server? Assume I don't have sufficient privileges to create my own real (permanent) tables.


Solution

  • It's not possible to directly create temporary tables on a linked remote server. In fact you can't use any DDL against a linked server.

    For more info on the guidelines and limitations of using linked servers see:

    Guidelines for Using Distributed Queries (SQL 2008 Books Online)

    One work around (and off the top of my head, and this would only work if you had permissions on the remote server) you could:

    • on the remote server have a stored procedure that would create a persistent table, with a name based on an IN parameter
    • the remote stored procedure would run a query then insert the results into this table
    • You then query locally against that table perform any joins to any local tables required
    • Call another stored procedure on the remote server to drop the remote table when you're done

    Not ideal, but a possible work around.