Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query global temp table on a linked server

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
like image 688
mpilatzke76 Avatar asked Dec 03 '12 22:12

mpilatzke76


2 Answers

try this to query global temp table from linked server

SELECT * FROM OPENQUERY(linkedServerName, 'SELECT * FROM ##temp')
like image 175
Ravi Avatar answered Sep 19 '22 15:09

Ravi


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.

like image 44
milivojeviCH Avatar answered Sep 18 '22 15:09

milivojeviCH