0% found this document useful (0 votes)
21 views

Is There A Way To Get A List of All Current Temporary Tables in SQL Server - Stack Overflow

Uploaded by

sestidavide.mail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Is There A Way To Get A List of All Current Temporary Tables in SQL Server - Stack Overflow

Uploaded by

sestidavide.mail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/3/2019 Is there a way to get a list of all current temporary tables in SQL Server?

- Stack Overflow

Is there a way to get a list of all current temporary tables in SQL Server? Ask Question

I realize that temporary tables are session/connection bound and not visible or accessible out of the
session/connection.
53 I have a long running stored procedure that creates temporary tables at various stages.

Is there a way I can see the list of current temporary tables? What privileges do I need to be able to
do so?

7 Alternatively,

Is there a way I can see the particular SQL statement being executed inside a running stored
procedure? The procedure is running as a scheduled job in SQL Server.

I am using SQL Server 2000.

Thanks for your guidance.

sql sql-server sql-server-2000 temp-tables

edited Aug 16 '11 at 8:50 asked Aug 16 '11 at 8:29


marc_s AAsk
580k 130 1119 626 4 13 22
1266

5 Answers

Is this what you are after?

select * from tempdb..sysobjects


88 --for sql-server 2000 and later versions

select * from tempdb.sys.objects


--for sql-server 2005 and later versions

edited Mar 20 '17 at 13:29 answered Aug 16 '11 at 8:37


Pradeep Kumar Sandro
Prabaharan 1,762 1 15 35
2,049 1 18 28

This fails in SQL Server 2000. – AAsk Aug 16 '11 at 8:47


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
12 In 2008: tempdb.sys.objects, In 2000: tempdb..sysobjects – Mitch Wheat Aug 16 '11 at 8:48
https://stackoverflow.com/questions/7075483/is-there-a-way-to-get-a-list-of-all-current-temporary-tables-in-sql-server 1/3
7/3/2019 Is there a way to get a list of all current temporary tables in SQL Server? - Stack Overflow

5 this lists many different objects in tempdb including indexes and also global temp tables. should not be chosen
as answer – FLICKER Sep 23 '15 at 18:15

You can get list of temp tables by following query :

select left(name, charindex('_',name)-1)


8 from tempdb..sysobjects
where charindex('_',name) > 0 and
xtype = 'u' and not object_id('tempdb..'+name) is null

edited Jan 10 '13 at 5:54 answered Aug 16 '11 at 8:39


Rais Alam Upendra Chaudhari
5,768 11 47 84 5,486 4 18 41

Just a comment, I would find your last clause much more readable as and object_id(...) is not null –
Aaron Bertrand Aug 16 '11 at 11:39
Home
This returns permanent tables as well (which obviously may not belong to my session). – Aaron Bertrand Jul 24
'12 at 18:09
PUBLIC

Stack Overflow

Tags
For SQL Server 2000, this should tell you only the #temp tables in your session. (Adapted from my
Users example for more modern versions of SQL Server here.) This assumes you don't name your tables
2 with three consecutive underscores, like CREATE TABLE #foo___bar :
Jobs
SELECT
name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1),
t.id
Teams
FROM tempdb..sysobjects AS t
Q&A for work WHERE t.name LIKE '#%[_][_][_]%'
AND t.id =
Learn More OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));

edited May 23 '17 at 12:02 answered Jul 24 '12 at 18:12


Community ♦ Aaron Bertrand
1 1 211k 27 369 407

If you need to 'see' the list of temporary tables, you could simply log the names used. (and as others
have noted, it is possible to directly query this information)
2 If you need to 'see' the content of temporary tables, you will need to create real tables with a (unique)
temporary name.

You can trace the SQL being executed using SQL Profiler:

Using SQL Server Profiler


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
How to Identify Slow Running Queries with SQL Profiler

https://stackoverflow.com/questions/7075483/is-there-a-way-to-get-a-list-of-all-current-temporary-tables-in-sql-server 2/3
7/3/2019 Is there a way to get a list of all current temporary tables in SQL Server? - Stack Overflow
[These articles target SQL Server versions later than 2000, but much of the advice is the same.]

If you have a lengthy process that is important to your business, it's a good idea to log various steps
(step name/number, start and end time) in the process. That way you have a baseline to compare
against when things don't perform well, and you can pinpoint which step(s) are causing the problem
more quickly.

edited Feb 9 '14 at 7:40 answered Aug 16 '11 at 8:32


Mitch Wheat
256k 36 408 501

Thanks. By 'log the names used', Am I correct in thinking that you mean write the temp table name (after
creation) to another table? – AAsk Aug 16 '11 at 8:36

@AAsk: yes, that's one possibility. Can I ask what you are actually trying to solve? Do you have many temp
tables? – Mitch Wheat Aug 16 '11 at 8:37

I have a stored procedure running once a week on Sunday at 3:00am, usually taking just under 2 hours. Last
Sunday it carried on for 6 hours and hadn't finished. I am running it again without alteration - hence my question.
If it fails again, I'll write messages at key stages to a 'debug' table so I can see its progress. – AAsk Aug 16 '11
at 8:45

In addition to "real tables with a (unique) temporary name" you could also consider "a single real table with a key
on SPID/session_id"... – Aaron Bertrand Aug 16 '11 at 11:38

SELECT left(NAME, charindex('_', NAME) - 1)


FROM tempdb..sysobjects
WHERE NAME LIKE '#%'
2 AND NAME NOT LIKE '##%'
AND upper(xtype) = 'U'
AND NOT object_id('tempdb..' + NAME) IS NULL

you can remove the ## line if you want to include global temp tables.

edited Sep 23 '15 at 18:43 answered Sep 23 '15 at 18:21


FLICKER
3,966 2 21 48

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/7075483/is-there-a-way-to-get-a-list-of-all-current-temporary-tables-in-sql-server 3/3

You might also like