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 - Stack Overflow
- 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.
5 Answers
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
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));
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:
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.
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
you can remove the ## line if you want to include global temp tables.
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