Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select
- 'null columns: ' ||
- case when max(col1) is null then 'col1 ' else '' end ||
- case when max(col2) is null then 'col2 ' else '' end ||
- case when max(col3) is null then 'col3 ' else '' end ||
- case when max(col4) is null then 'col4 ' else '' end ||
- ...
- from mytable;
- DECLARE @strTablename varchar(100) = 'dbo.MyTable'
- DECLARE @strQuery varchar(max) = ''
- DECLARE @strUnPivot as varchar(max) = ' UNPIVOT ([Count] for [Column] IN ('
- CREATE TABLE ##tblTemp([Column] varchar(50), [Count] Int)
- SELECT @strQuery = ISNULL(@strQuery,'') + 'Count([' + name + ']) as [' + name + '] ,' from sys.columns where object_id = object_id(@strTablename) and is_nullable = 1
- SELECT @strUnPivot = ISNULL(@strUnPivot,'') + '[' + name + '] ,' from sys.columns where object_id = object_id(@strTablename) and is_nullable = 1
- SET @strQuery = 'SELECT [Column],[Count] FROM ( SELECT ' + SUBSTRING(@strQuery,1,LEN(@strQuery) - 1) + ' FROM ' + @strTablename + ') AS p ' + SUBSTRING(@strUnPivot,1,LEN(@strUnPivot) - 1) + ')) AS unpvt '
- INSERT INTO ##tblTemp EXEC (@strQuery)
- SELECT [Column] from ##tblTemp Where [Count] =0
- DROP TABLE ##tblTemp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement