8

I have seen all of the references on how to query two different tables on two different SQL servers, and I understand how it is to be implemented. However, the command doesn't seem work with temporary tables created with the ##.

If I write a join on one server, and it references one temp table on that server, and one temp table on the other server, SQL Server assumes that because the ## is in the command, it automatically looks at the local server's tempdb, not the remote one. I also cannot use OPENROWSET at this time because the feature has been disabled and I have to get approval to turn it back on.

So my question is there a way that I can reconfigure this command to recognize which tempdb to look at?

SELECT * 
FROM (##mytemptable1 Demog 
INNER JOIN MyServer.tempdb.dbo.##mytemptable2 PeakInfo ON (Demog.SAMPLE_NO = PeakInfo.SampleNum)  AND  (Demog.JOB_NO = PeakInfo.JobNum) )
ORDER BY PeakInfo.JobNum, PeakInfo.SampleNum,   PeakInfo.Replicate ,PeakInfo.Reinjection ,PeakInfo.PeakNameCustSort
Pondlife
  • 15,992
  • 6
  • 37
  • 51
mpilatzke76
  • 81
  • 1
  • 1
  • 3
  • Is the remote temp table created by a separate process, or the same one that's joining them together? – Ann L. Dec 03 '12 at 22:54

2 Answers2

20

try this to query global temp table from linked server

SELECT * FROM OPENQUERY(linkedServerName, 'SELECT * FROM ##temp')
S3S
  • 24,809
  • 5
  • 26
  • 45
Ravi
  • 201
  • 2
  • 3
  • 2
    I can verify this works, not sure if OP has access to OpenQuery though since OpenRowSet was disabled. – scw Oct 15 '14 at 21:26
1

MSDN http://msdn.microsoft.com/en-us/library/ms186986(v=sql.105).aspx says that the global temp tables are visible only on the specific instance of SQL Server:

Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

Also, any action attempted on the remote global temp table results in a clear error message:

SELECT * FROM LinkedServerName.TempDB.dbo.##GLOBTABLE

Database name 'TempDB' ignored, referencing object in tempdb.

Looks like the answer is no, there's no (easy) way.

milivojeviCH
  • 1,580
  • 14
  • 29