Create table qResults (tName nvarchar (370), cname nvarchar (3630), [count] int)
Declare @ tname nvarchar (200)
Declare @ cname nvarchar (200)
Declare @ countOut nvarchar (200)
Declare @ SQL nvarchar (max)
Declare c_search cursor
Select t. name, c. name from sysobjects t inner join syscolumns c on t. id = c. id where t. type = 'U' and c. X type in (56,167,175,231,239) order by t. name -- xtype: The maximum type is int varchar char nvarchar nchar.
Open c_search
Fetch next from c_search into @ tname, @ cname
While @ FETCH_STATUS = 0
Begin
Set @ SQL = n' select @ countx = COUNT (*) from '+ @ tname + 'where [' + @ cname + '] = 'A''
Print @ SQL
EXECUTE sp_executesql @ SQL, n' @ countx nvarchar (200) out', @ countx = @ countOUT out
Insert into qResults values (@ tname, @ cname, @ countOUT)
Fetch next from c_search into @ tname, @ cname
End
Select tName 'table', cname 'field' from qResults where [count]> 0 order by tname
Close c_search
Deallocate c_search
Drop table qResults
Zookeeper