Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the column names of Temp table

I need to find the column names of temp table.

If it is a physical table then we can either use sys.columns or Information_schema.columns system views to find the column names.

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

like image 771
Pரதீப் Avatar asked May 29 '15 17:05

Pரதீப்


People also ask

How do I find column names in a table?

We can verify the data in the table using the SELECT query as below. We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.

How do I get the column names on a #temp table?

If it is a physical table then we can either use sys. columns or Information_schema. columns system views to find the column names.

How do I list the column names in a table in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.


2 Answers

SELECT *
FROM   tempdb.sys.columns
WHERE  object_id = Object_id('tempdb..#sometemptable'); 
like image 86
William Xifaras Avatar answered Oct 15 '22 17:10

William Xifaras


To get only columns name you can use this query below:

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

To get columns name with data type you can use this query but you need to make sure sp_help runs in the same database where the table is located (tempdb).

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

you can achieve same result by joining against tempdb.sys.columns like below:

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');
like image 25
Humayoun_Kabir Avatar answered Oct 15 '22 15:10

Humayoun_Kabir