select *
into #myTempTable -- creates a new temp table
from tMyTable -- some table in your database
exec tempdb..sp_help '#myTempTable'
drop table #myTempTable
SELECT char(9) + '[' + c.column_name + '] ' + c.data_type
+ CASE
WHEN c.data_type IN ('decimal')
THEN isnull('(' + convert(varchar, c.numeric_precision) + ', ' + convert(varchar, c.numeric_scale) + ')', '')
WHEN c.data_type IN ('varchar', 'nvarchar', 'char', 'nchar')
THEN isnull('('
+ CASE WHEN c.character_maximum_length = -1
THEN 'max'
ELSE convert(varchar, c.character_maximum_length)
END + ')', '')
ELSE '' END
+ CASE WHEN c.IS_NULLABLE = 'YES' THEN ' NULL' ELSE '' END
+ ','
FROM tempdb.INFORMATION_SCHEMA.COLUMNS c
WHERE TABLE_NAME LIKE '#myTempTable%'
ORDER BY c.ordinal_position
Select Column_Name + ' [' + DATA_TYPE + ']' +
case when Data_Type in ('numeric', 'varchar', 'char')
then '(' +
case
when DATA_TYPE = 'numeric' then CAST(numeric_precision as varchar(3)) + ',' + CAST(numeric_scale as varchar(3))
when DATA_TYPE = 'varchar' then CAST(CHARACTER_MAXIMUM_LENGTH as varchar(3))
when DATA_TYPE = 'char' then CAST(CHARACTER_MAXIMUM_LENGTH as varchar(3))
end
+ ')'
else ''
end
+ ','
, *
From tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '#MEHTEMPTABLE%'
方法3-使用诸如 INFORMATION _ SCHEMA. Colummns,SYS.Colummns,SYS.Tables 之类的系统表
SELECT * FROM TempDB.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN (
SELECT NAME FROM TempDB.SYS.TABLES WHERE OBJECT_ID=OBJECT_ID('TempDB.dbo.#TempTable')
);
GO
SELECT * FROM TempDB.SYS.COLUMNS WHERE OBJECT_ID=OBJECT_ID('TempDB.dbo.#TempTable');
GO
SELECT * FROM TempDB.SYS.TABLES WHERE OBJECT_ID=OBJECT_ID('TempDB.dbo.#TempTable');
GO
清除当前实例的临时表
IF OBJECT_ID('TempDB..#TempTable') IS NOT NULL
DROP TABLE #TempTable;