DECLARE dt_Cursor CURSOR DYNAMIC FOR
select obj.name--, count(*)
from sysobjects obj, syscolumns col
where obj.id=col.id and col.name = 'SEC_CD' and obj.type='U'
OPEN dt_Cursor
declare @firstLine bit
set @firstLine = 1
Declare @sql varchar(8000)
set @sql = ''
Declare @tblName varchar(100)
FETCH NEXT FROM dt_Cursor INTO @tblName
WHILE @@FETCH_STATUS = 0
BEGIN
if @firstLine=1
set @firstLine=0
else
set @sql = @sql + ' union all '
set @sql = @sql + 'select ''' + @tblName + ''' as TABLE_NM, count(*) as ROW_CNT from ' + @tblName + ' where SEC_CD=''02188'''
FETCH NEXT FROM dt_Cursor INTO @tblName
END
CLOSE dt_Cursor
DEALLOCATE dt_Cursor
exec (@sql)