views:

433

answers:

5

I have a temp table, that isn't going away. I want to see what is in the table to determine what perhaps bad data might be in there. How can I view the data in the temp table?

I can see it in tempdb. I ran

SELECT * FROM tempdb.dbo.sysobjects WHERE Name LIKE '#Return_Records%'

To get the name of the table.

I can see it's columns and its object id in

select c.*
from tempdb.sys.columns c
inner join tempdb.sys.tables t ON c.object_id = t.object_id
where t.name like '#Return_Records%'

How can i get at the data?

By the way, this doesn't work

SELECT * FROM #Return_Records
A: 

This is something that seems like you obviously tried, but since you didn't mention it I though I would mention just in case:

Did you try "SELECT * FROM #Return_Records"?

mgroves
this doesn't work, i definately tried this first
Danny G
A: 

How about select * from ##Return_Records?

DForck42
this doesn't work, i definately tried this
Danny G
just covering the basics. you didn't mention either select statements in your question.
DForck42
+1  A: 

SQL Server limits access to Local Temp Tables (#TableName) to the connection that created the table. Global temp tables (##TableName) can be accessible by other connections as long as the connection that created it is still connected.

Even though you can see the table in the table catalog, it is not accessible when trying to do a SELECT. It gives you an "Invalid Object Name" error.

There's no documented way of accessing the data in Local Temp Tables created by other connections. I think you may be out of luck in this case.

Jose Basilio
This i knew... but I was hoping there was an un-documented way to get at the data. If the data is there in the DB, there has to be someone way to get at it.
Danny G
A: 

Like José Basilio says, that's a temporary table belonging to another connection. If it's there for a long time, it must belong to a connection that has been open for a long time. Check Maintenance -> Acitivity Monitor; you can sort by Login Time.

Check if the Login Time, or Last Batch Time, matches with the create date of the temporary table. That can be retrieved with:

select crdate from tempdb.dbo.sysobjects WHERE Name LIKE '#Return_Records%'

You can shoot down suspect connections (right click and Kill Process.) If the table is gone after killing a process, you've found the culprit.

To just remove the table, restart the Sql Server service. You can attach Sql Profiler right after with a filter to start looking for the connection that creates the temporary table.

Andomar
+1  A: 

One way of getting at the data in a low-level and not particularly easy to manipulate manner is to use the DBCC PAGE command as described in a blog post by Paul Randal:

http://blogs.msdn.com/sqlserverstorageengine/archive/2006/06/10/625659.aspx

You should be able to find the fileid and page number of the first page in the object by querying on sysindexes .. the last time I did this was on SQL Server 7.

If the data is in the database, then DBCC page will be able to dump it.

pjjH

Paul Harrington
I just got my copy of http://www.amazon.com/Microsoft%C2%AE-SQL-Server%C2%AE-2008-Internals/dp/0735626243 which has a chapter on DBCC written by Paul Randal. I hope to be able to followup with executable examples.
Paul Harrington
CREATE table #foo(i integer, t varchar(max))INSERT #foo(i,t) VALUES(42, 'this is not a banana')dbcc ind('tempdb', '#foo', -1)dbcc page('tempdb', 1, 210, 3) WITH TABLERESULTS[-]Slot 0 Offset 0x60 Length 35 Memory Dump @0x417DC060 00000014: 6973206e 6f742061 2062616e 616e61†††††††††††††is not a bananaSlot 0 Offset 0x60 Length 35 Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4 i 42Slot 0 Offset 0x60 Length 35 t = [BLOB Inline Data] Slot 0 Column 2 Offset 0xf Length 20 Length (physical) 20 417D13D4: 74686973 20697320 6e6f7420 61206261 6e616e61 †this is not a banana
Paul Harrington
Sorry for the borked formatting in the previous comment. The procedure documented at http://www.mssqltips.com/tip.asp?tip=1578 works for me. I was unaware of DBCC IND.
Paul Harrington