How to get the number of columns in a temporary table

3

How can I get the number of columns in a given temporary table in the sql server ?

    
asked by anonymous 27.04.2018 / 15:27

2 answers

6

Try using next SQL :

SELECT COUNT(COLUMN_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'sua_tabela'

For temporary tables, use :

SELECT COUNT(*)
FROM tempdb.sys.columns
WHERE object_id = object_id('tempdb..#tmp_grid_email')
    
27.04.2018 / 15:30
1

Hello, see if the code below meets your needs.

-- ==============================
-- exibindo qtd de colunas
-- ==============================

SELECT COUNT(*) AS QTD
FROM 
    sys.sysobjects    AS T (NOLOCK) 
INNER JOIN sys.all_columns AS C (NOLOCK) ON T.id = C.object_id AND T.XTYPE = 'U' 
WHERE 
    T.NAME LIKE '%NOME_TABELA%'

-- ==============================
-- exibindo as colunas
-- ==============================

SELECT 
    T.name AS Tabela, 
    C.name AS Coluna
FROM 
    sys.sysobjects    AS T (NOLOCK) 
INNER JOIN sys.all_columns AS C (NOLOCK) ON T.id = C.object_id AND T.XTYPE = 'U' 
WHERE 
    T.NAME LIKE '%NOME_TABELA%'
ORDER BY 
    C.column_id ASC
    
27.04.2018 / 15:41