Technical Article

Percentage of NULL Column Rows

,

This script can be run against any database.

SET NOCOUNT ON
GO
DROP TABLE #tblHold
GO
DECLARE @tblA TABLE (RowId INT IDENTITY(1,1),SchemaName VARCHAR(65),TableName VARCHAR(65),TotalRows BIGINT)
INSERTINTO @tblA
SELECTS.[name] AS SchemaName,
ST.[name] AS TableName,
SUM(p.[rows]) AS TotalRows
FROM[sys].[tables] AS ST WITH (NOLOCK)
INNER JOIN [sys].[schemas] AS S WITH (NOLOCK)
ONST.[schema_id] = S.[schema_id]
INNER JOIN [sys].[partitions] AS P WITH (NOLOCK)
ON  P.[object_id] = ST.[object_id]
WHEREP.[index_id] IN (0,1)
GROUP BY
S.[name],
ST.[name]

DECLARE @tblB TABLE 
(RowId INT IDENTITY(1,1),SchemaName VARCHAR(65),TableName VARCHAR(65),ColumnName VARCHAR(65),
ExecSQL VARCHAR(2000),NullRows BIGINT)
INSERT INTO @tblB
SELECTS.[name] AS SchemaName,
ST.[name] AS TableName,
SC.[name] AS ColumnName,
'SELECT COUNT(1) FROM ['+DB_NAME()+'].['+S.[name]+'].['+ST.[name]+'] WITH (NOLOCK) WHERE ['+SC.[name]+'] IS NULL' AS ExecSQL,
0 AS NullRows
FROM[sys].[tables] AS ST WITH (NOLOCK)
INNER JOIN [sys].[schemas] AS S WITH (NOLOCK)
ONST.[schema_id] = S.[schema_id]
INNER JOIN [sys].[columns] AS SC WITH (NOLOCK)
ONST.[object_id] = SC.[object_id]
WHERE1=1
ANDSC.[is_nullable] = 1
ORDER BY
S.[name],
ST.[name],
SC.[column_id]

DECLARE @MinIdINT
DECLARE @MaxIdINT
DECLARE @ExecSQLVARCHAR(2000)
DECLARE @tblE TABLE(MyRows BIGINT)

SELECT@MinId=MIN(RowId),@MaxId=MAX(RowId) FROM @tblB
WHILE(@MinId<=@MaxId)
BEGIN

DELETE@tblE

SELECT @ExecSQL=ExecSQL FROM @tblB WHERE RowId = @MinId
INSERT INTO @tblE EXEC(@ExecSQL)

UPDATE@tblB
SETNullRows=(SELECT MyRows FROM @tblE)
WHERERowId = @MinId

SELECT@MinId=@MinId+1
END

SELECTB.RowId,
A.SchemaName,
A.TableName,
B.ColumnName,
B.NullRows,
A.TotalRows,
CAST(CAST(CAST(NullRows AS VARCHAR(10))+'.00' AS DEC(15,2))/CAST(CAST(TotalRows AS VARCHAR(10))+'.00' AS DEC(15,2))*100 AS DEC(5,2)) AS PercentageNull
INTO#tblHold
FROM@tblA AS A
INNER JOIN @tblB AS B
ONA.SchemaName = B.SchemaName
ANDA.TableName  = B.TableName
WHERENullRows > 0 
ANDNullRows <> TotalRows
GO

SELECT* 
FROM#tblHold
ORDER BY
PercentageNull DESC

Rate

2 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

2 (4)

You rated this post out of 5. Change rating