Thursday, 7 March 2013

Stored Procedure to show all field data if any column is ‘null’ in particular table and particular database

Stored Procedure to show all field data if any column is ‘null’ in  particular table


Create proc sp_printnullfieldrow
as
begin
declare @colname varchar(max)
declare @sqlstring varchar(max)
declare @nl varchar(20)

 select * into #temp1 from emp where 1=2

 declare cursorforlevel cursor fast_forward for select Column_Name from information_schema.columns where Table_Name='emp'
 open cursorforlevel

 fetch next from cursorforlevel into @colname
 while @@FETCH_STATUS=0
 begin

set @nl='is null'

set @sqlstring='insert into  #temp1 select * from  emp where ' +  @colname + ' '+ @nl
exec (@sqlstring)

 fetch next from cursorforlevel into @colname
-- Closing cursor
end

close cursorforlevel
-- Dealocate cursor from Memory

deallocate cursorforlevel

select * from #temp1

end

exec sp_printnullfieldrow

--Stored Procedure to show all field data if any column is ‘null’ in  particular table which is given by the user

Create proc sp_printnullfieldrow @tblname varchar(max)
as
begin
declare @colname varchar(max)
declare @sqlstring varchar(max)
declare @nl varchar(20)

 select * into #temp1 from emp where 1=2

 declare cursorforlevel cursor fast_forward for select Column_Name from information_schema.columns where Table_Name=@tblname
 open cursorforlevel

 fetch next from cursorforlevel into @colname
 while @@FETCH_STATUS=0
 begin

set @nl='is null'

set @sqlstring='insert into  #temp1 select * from  emp where ' +  @colname + ' '+ @nl
exec (@sqlstring)

 fetch next from cursorforlevel into @colname
-- Closing cursor
end

close cursorforlevel
-- Dealocate cursor from Memory

deallocate cursorforlevel

select * from #temp1

end

exec sp_printnullfieldrow 'hyrarchy'





0 comments: