5

SQL Server の一時 (#temptable) テーブルの列数を数える方法はありますか?

4

4 に答える 4

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
于 2013-09-12T10:07:22.553 に答える
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 + '%' ) 
于 2015-04-25T05:50:22.557 に答える