create table #SuspectColumns (
TABLE_SCHEMA sysname,
TABLE_NAME sysname,
COLUMN_NAME sysname
)
declare csrColumns cursor fast_forward for
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where IS_NULLABLE = 'YES'
declare @TABLE_SCHEMA sysname,
@TABLE_NAME sysname,
@COLUMN_NAME sysname,
@sql nvarchar(max)
open csrColumns
while (1=1) begin
fetch next
from csrColumns
into @TABLE_SCHEMA, @TABLE_NAME, @COLUMN_NAME
if @@FETCH_STATUS<>0 break
set @sql = N'if not exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is not null)
insert into #SuspectColumns values (''' + @TABLE_SCHEMA + N''',''' + @TABLE_NAME + N''',''' + @COLUMN_NAME + N''')'
exec sp_executesql @sql
end /* while */
close csrColumns
deallocate csrColumns
select * from #SuspectColumns
drop table #SuspectColumns