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?
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.
If it is a physical table then we can either use sys. columns or Information_schema. columns system views to find the column names.
USE db_name; DESCRIBE table_name; it'll give you column names with the type.
SELECT *
FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#sometemptable');
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With