方法不是很好,不過實現了你的要求:
create table #t(id varchar(2),t varchar(10))
create table #c(id varchar(2),c int)
insert into #t values(1,'emp')
insert into #t values(2,'invtx')
declare @str varchar(8000),
@id varchar(2),
@c int,
@t varchar(20)
declare cu_1 cursor for
select id,t from #t
open cu_1
fetch cu_1 into @id,@t
while @@fetch_status=0
begin
set @str='insert into #c select '+ ''''+ @id +''''+',count(*) from ' +''+ @t +''
--select @str
exec (@str)
fetch cu_1 into @id,@t
end
deallocate cu_1
select a.id,a.t,b.c from #t a,#c b where a.id=b.id
drop table #c,#t
select *,cast (0 as int) TableCount into #temp from table1
update #temp set tablecount =(select count(*) from A) where 表名='A'
update #temp set tablecount =(select count(*) from B) where 表名='B'
.....