Find Temp Table Column Names

I need to find column names temp tables .

If this is a physical table, we can use system views sys.columnsor Information_schema.columnsto search for column names.

Also, is there a way to find the column names present in the temp table?

+5
source share
2 answers
SELECT *
FROM   tempdb.sys.columns
WHERE  object_id = Object_id('tempdb..#sometemptable'); 
+10
source

To get only the column name, you can use this query below:

SELECT * 
FROM tempdb.sys.columns 
WHERE [object_id] = OBJECT_ID(N'tempdb..#temp');

To get the name of a column with a data type, you can use this query, but you need to make sure that sp_help works in the same database where the table is located (tempdb).

EXEC tempdb.dbo.sp_help @objname = N'#temp';

, tempdb.sys.columns, :

SELECT [column] = c.name, 
       [type] = t.name, c.max_length, c.precision, c.scale, c.is_nullable 
    FROM tempdb.sys.columns AS c
    INNER JOIN tempdb.sys.types AS t
    ON c.system_type_id = t.system_type_id
    AND t.system_type_id = t.user_type_id
    WHERE [object_id] = OBJECT_ID(N'tempdb.dbo.#temp');
0

All Articles