Script to find NULL values from all the tables in Database
USE [NULLTest] --database name
GO
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 exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is 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
USE [NULLTest] --database name
GO
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 exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is 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
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.