如何写这个Sql语句?

haoguozhong 2003-10-15 10:29:02
表1的内容为:
序号 表名
1 A
2 B
3 C
4 D
表A、表B、表C.....的结构相同,
如何得到数据集:
序号 表名 纪录条数
1 A 表A记录条数
2 B 表B记录条数
3 C 表C记录条数
4 D 表D记录条数

SQL语句如何写?

...全文
27 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoguozhong 2003-10-18
  • 打赏
  • 举报
回复
再up一次
weichenggao 2003-10-17
  • 打赏
  • 举报
回复
这么多呀
orcale 2003-10-17
  • 打赏
  • 举报
回复
方法不是很好,不過實現了你的要求:
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
haoguozhong 2003-10-17
  • 打赏
  • 举报
回复
playyuer(双规干部)的方法我不会用!
CrazyFor(吃素的鼹鼠) 的方法我得写太多的语句,麻烦不实用!

各位有无更好的方法,才能根据表名表(表1),得到各个表对应的记录数的数据集?

haoguozhong 2003-10-16
  • 打赏
  • 举报
回复
我去试试看,先谢了!
CrazyFor 2003-10-15
  • 打赏
  • 举报
回复
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'
.....
playyuer 2003-10-15
  • 打赏
  • 举报
回复
declare @ varchar(8000)
set @ = 'select *,'
select @ = @ + '(select count(*) from ' + 表名 + ')'
from 表1
exec (@ + ' from 表1')
haoguozhong 2003-10-15
  • 打赏
  • 举报
回复
可是如何写SQL语句啊?能否写出来?
playyuer 2003-10-15
  • 打赏
  • 举报
回复
动态SQL

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧