A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Hi @amomen-8749,
You could refer Viorel's solution if your version is SQL server 2017 and later.
If have an old version, you could refer below using STUFF:
declare @tablename varchar(max) = '[Northwind].[dbo].[Orders]'
declare @sql nvarchar(max)
select @sql= STUFF((
select ' union all select '+ quotename([name], '''')+' as Column_Name, count(distinct ', quotename([name]), ')*100.0/Count(*) as [crowdedness(in %)] '+
'from '+ @tablename
FROM sys.all_columns
WHERE object_id = OBJECT_ID(@tablename)
FOR XML PATH('') ), 1, 10, '')
exec(@sql)
Output:
Column_Name crowdedness(in %)
OrderID 100.000000000000
CustomerID 10.722891566265
EmployeeID 1.084337349397
OrderDate 57.831325301204
RequiredDate 54.698795180722
ShippedDate 46.626506024096
ShipVia 0.361445783132
Freight 96.265060240963
ShipName 10.843373493975
ShipAddress 10.722891566265
ShipCity 8.433734939759
ShipRegion 2.289156626506
ShipPostalCode 10.120481927710
ShipCountry 2.530120481927
In case you would like to list all columns of all tables in all databases in master, you could refer below and check whether it is a little helpful:
DROP TABLE IF EXISTS #AllTables
SET NOCOUNT ON
CREATE TABLE #AllTables (DbName sysname,SchemaName sysname, TableName sysname)
DECLARE
@SearchDb nvarchar(200)
,@SearchSchema nvarchar(200)
,@SearchTable nvarchar(200)
,@SQL nvarchar(4000)
SET @SearchDb='%%'
SET @SQL='select ''?'' as DbName, s.name as SchemaName, t.name as TableName from [?].sys.tables t inner join sys.schemas s on t.schema_id=s.schema_id
WHERE ''?'' LIKE '''+@SearchDb+''' AND ''?'' NOT IN (''master'',''model'',''msdb'',''tempdb'',''SSISDB'')'
INSERT INTO #AllTables (DbName, SchemaName, TableName)
EXEC sp_msforeachdb @SQL
SET NOCOUNT OFF
SELECT * FROM #AllTables ORDER BY DbName, SchemaName, TableName
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.