DECLARE @dbname VARCHAR(100) = 'dbname'
DECLARE @schemaName VARCHAR(100) = 'schemaname'
DECLARE @result TABLE ([NullValues] int,col VARCHAR(4000))
SELECT @dbname dbname
,t.name tbl
,c.name col
INTO #Temp1
FROM sys.columns c
JOIN sys.tables t ON
t.object_id = c.object_id
JOIN sys.schemas s ON
s.schema_id = t.schema_id
WHERE c.is_nullable = 1
AND s.name in (@schemaName)
DECLARE @sql NVARCHAR(MAX) =
STUFF(
(
SELECT 'UNION ALL SELECT Count(*) as [NullValues],''' + @dbname + '.' + @schemaName + '.' + tbl + '.' + col + ''' FROM ' + @dbname + '.' + @schemaName + '.' + tbl + ' WHERE ' + col + ' IS NULL '
FROM #Temp1
FOR XML PATH('')
), 1, 10, ' ')
INSERT @result
EXEC(@sql)
SELECT [NullValues], col
INTO #Nulls
FROM @result
WHERE col IS NOT NULL AND [NullValues] > 0
SELECT
[TABLE_CATALOG] + '.' + [TABLE_SCHEMA] + '.' + [TABLE_NAME] + '.' + [COLUMN_NAME] as SchemaTableColumn
,[TABLE_CATALOG] as 'Database'
,[TABLE_SCHEMA] as 'Schema'
,[TABLE_NAME] as 'TableName'
,[COLUMN_NAME]
,[ORDINAL_POSITION]
,[DATA_TYPE]
INTO #Temp2
FROM [DW_LandingCR].[INFORMATION_SCHEMA].[COLUMNS]
WHERE
[TABLE_SCHEMA] = @schemaName
ORDER BY TABLE_SCHEMA,TABLE_NAME,ORDINAL_POSITION
SELECT sc.name as [Schema],ta.name as [TableName],SUM(pa.rows) RowCnt
INTO #Temp3
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc
ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0
AND pa.index_id IN (1,0)
AND sc.name = @schemaName
GROUP BY sc.name,ta.name
ORDER BY sc.name,ta.name
Select
tp2.*,tp3.RowCnt
Into #Detail
From
#Temp2 tp2
inner join #Temp3 tp3 on tp2.[Schema]=tp3.[Schema] and tp2.[TableName]=tp3.[TableName]
Select
det.*,
n.[NullValues]
From
#Nulls n
Inner Join #Detail det on det.SchemaTableColumn=n.col
Order By
det.[Database],
det.[Schema],
det.TableName,
det.[ORDINAL_POSITION]
Drop Table #Nulls
Drop Table #Detail
Drop Table #Temp1
Drop Table #Temp2
Drop Table #Temp3