Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. select
  2. 'null columns: ' ||
  3. case when max(col1) is null then 'col1 ' else '' end ||
  4. case when max(col2) is null then 'col2 ' else '' end ||
  5. case when max(col3) is null then 'col3 ' else '' end ||
  6. case when max(col4) is null then 'col4 ' else '' end ||
  7. ...
  8. from mytable;
  9.  
  10. DECLARE @strTablename varchar(100) = 'dbo.MyTable'
  11. DECLARE @strQuery varchar(max) = ''
  12. DECLARE @strUnPivot as varchar(max) = ' UNPIVOT ([Count] for [Column] IN ('
  13.  
  14. CREATE TABLE ##tblTemp([Column] varchar(50), [Count] Int)
  15.  
  16. SELECT @strQuery = ISNULL(@strQuery,'') + 'Count([' + name + ']) as [' + name + '] ,' from sys.columns where object_id = object_id(@strTablename) and is_nullable = 1
  17. SELECT @strUnPivot = ISNULL(@strUnPivot,'') + '[' + name + '] ,' from sys.columns where object_id = object_id(@strTablename) and is_nullable = 1
  18.  
  19.  
  20. 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 '
  21.  
  22. INSERT INTO ##tblTemp EXEC (@strQuery)
  23.  
  24. SELECT [Column] from ##tblTemp Where [Count] =0
  25.  
  26. DROP TABLE ##tblTemp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement