Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

SQL Server conta il numero di valori distinti in ogni colonna di una tabella

Puoi farlo:

DECLARE @query varchar(max)
    SELECT @query = 
    'SELECT ' + SUBSTRING((SELECT ',' +'COUNT(DISTINCT(' + column_name + ')) 
             As ' + column_name + ' '  
             FROM information_schema.columns
             WHERE 
             table_name = 'table_name'
             for xml path('')),2,200000)  +  'FROM table_name'

PRINT(@query)