Earlier this week a colleague had an interesting question. He was working on an application that uses a database containing more than 200 tables and wanted to find all columns in the database where this column is null for all records, so in other words all empty columns. This would give him an idea of possibly unused (i.e. obsolete) columns.
Obviously he did not want to do this manually.
So I wrote him this script:
/* DESCRIPTION: Returns a list of all columns in current database where the column's value is null for all records. WRITTEN BY: Valentino Vranken CREATED: 2009-02-15 VERSION: 1.0 COPIED FROM: http://blog.hoegaerden.be */ declare @tempTable table ( TableSchema nvarchar(256), TableName nvarchar(256), ColumnName sysname, NotNullCnt bigint ); declare @sql nvarchar(4000); declare @tableSchema nvarchar(256); declare @tableName nvarchar(256); declare @columnName sysname; declare @cnt bigint; declare columnCursor cursor for select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE = 'YES'; open columnCursor; fetch next from columnCursor into @tableSchema, @tableName, @columnName; while @@FETCH_STATUS = 0 begin -- use dynamic sql to get count of records where column is not null set @sql = 'select @cnt = COUNT(*) from [' + @tableSchema + '].[' + @tableName + '] where [' + @columnName + '] is not null'; -- print @sql; --uncomment for debugging exec sp_executesql @sql, N'@cnt bigint output', @cnt = @cnt output; insert into @tempTable select @tableSchema, @tableName, @columnName, @cnt; fetch next from columnCursor into @tableSchema, @tableName, @columnName; end; close columnCursor; deallocate columnCursor; select * from @tempTable where NotNullCnt = 0;
Yes I know, it uses a cursor and dynamic SQL but hey, it’s not something that will run every minute in a production environment. And I couldn’t really think of an easy way to do this without these features.
For more info on dynamic SQL I would like to point you to this excellent website by Erland Sommarskog. I’ve read his articles since several years now and always keep coming back to them to refresh my memory. The particular article here is called The Curse and Blessings of Dynamic SQL.
And finally here’s a Microsoft Support article that explains how to capture the output of a dynamic query executed by sp_executesql. According to the article this feature is not documented in the BOL. I think this is no longer a valid statement, there’s even an example in the BOL page on sp_executesql.
Tags: data, Script, SQL Server, T-SQL
-
Like it. Worked great. Added AND [' + @columnName + '] ”” to check for empty strings.
-
Great script. Thank you!
-
What a great solution! I can not wait to try this on a project I am working on. I was dreading going through everything… I think you just saved me! Thank you so much!
-
When I try this I get an error,
ERROR: Must declare the scalar variable “@tableSchema”.
Error
Code: 137Query = fetch next from columnCursor into @tableSchema,
@tableName, @columnNameI could really use this script!
Thanks!






6 comments
Comments feed for this article
Trackback link: http://blog.hoegaerden.be/2009/02/15/script-find-all-empty-columns-in-database/trackback/