declare @sql as nvarchar(max) = \'\'; declare @table_name as nvarchar(50) = \'\';
declare table_cursor cursor for select name from sysobjects where xtype = \'U\' AND id in(select id from syscolumns where id in(select id from sysobjects where xtype = \'U\') and name = \'STATUS_FLAG\' )
open table_cursor
fetch next from table_cursor into @table_name;
set @sql = @sql + \'select \'\'\' + @table_name + \'\'\' table_name,count(1) Record_Count from \' + @table_name + \' where STATUS_FLAG = \'\'N\'\'\';
while @@FETCH_STATUS = 0 begin set @sql = @sql + \' union all \'; set @sql = @sql + \'select \'\'\' + @table_name + \'\'\' table_name,count(1) Record_Count from \' + @table_name + \' where STATUS_FLAG = \'\'N\'\'\'; fetch next from table_cursor into @table_name; end;
set @sql = \'select * from (\' + @sql + \') a order by Record_Count desc\'; exec(@sql)
close table_cursor; DEALLOCATE table_cursor;