Assuming you don't name your #temp tables with three consecutive underscores, this should only match your #temp tables. However, it will not take your table variables and cannot in any way change this code to select tables in a foreign connection - this only works because OBJECT_ID('tempdb..#foo') can return true only for the table in your session .
SELECT name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1), t.[object_id] FROM tempdb.sys.tables AS t WHERE t.name LIKE '#%[_][_][_]%' AND t.[object_id] = OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
You may also be interested in the space used by each of these tables (at least for heap or clustered index), for example:
SELECT name = SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1), t.[object_id], p.used_page_count, p.row_count FROM tempdb.sys.tables AS t INNER JOIN tempdb.sys.dm_db_partition_stats AS p ON t.[object_id] = p.[object_id] WHERE t.name LIKE '#%[_][_][_]%' AND p.index_id IN (0,1) AND t.[object_id] = OBJECT_ID('tempdb..' + SUBSTRING(t.name, 1, CHARINDEX('___', t.name)-1));
You can expand this to show a common space for all indices. I was not worried about joining each section, as these are #temp tables.
Aaron Bertrand Jul 24 2018-12-18T00: 00Z
source share