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 for
select t.name,c.name from sysobjects t inner join syscolumns c on t.id=c.id where t.type='u' and c.xtype in(56,167,175,231,239) order by t.name --xtype 這個地方限制類型為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