SQL Server中临时表中的列数

时间:2013-09-12 09:59:52

标签: sql sql-server

有没有办法计算sql server中temp(#temptable)表中的列数?

4 个答案:

答案 0 :(得分:9)

SELECT COUNT(*)
FROM tempdb.sys.columns
WHERE object_id = object_id('tempdb..#mytemptable')

答案 1 :(得分:1)

查询以获取指定表格的列数

SELECT Count(*) as cnt into #TempTable FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'TableName'

查询以获取指定表的列名

SELECT COLUMN_NAME into #TempTable FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'TableName'

查询以获取#TempTable的列数

SELECT COUNT(*) as Cnt FROM tempdb.sys.columns WHERE object_id = object_id('tempdb..#TempTable')

DROP table #TempTable

答案 2 :(得分:0)

您可以使用此查询:

Declare @TableName NVarChar(150)
Declare @ColumnName NVarChar(150)
Set @TableName = ''
Set @ColumnName = ''

SELECT 
    t.name AS table_name,
    SCHEMA_NAME(schema_id) AS schema_name,
    c.name AS column_name
FROM sys.tables AS t
    INNER JOIN sys.columns As C ON t.OBJECT_ID = c.OBJECT_ID 
Where 
    ( T.name Like '%' + @TableName + '%' )
    And
    ( C.name Like '%' + @ColumnName + '%' ) 


SELECT 
    COUNT(C.Name) Count_Of_Columns,
    COUNT(Distinct T.Name) Count_Of_Tables
FROM sys.tables AS t
    INNER JOIN sys.columns As C ON t.OBJECT_ID = c.OBJECT_ID 
Where 
    ( T.name Like '%' + @TableName + '%' )
    And
    ( C.name Like '%' + @ColumnName + '%' ) 

答案 3 :(得分:-1)

SELECT COUNT(*) FROM temptable