怎样一次性列出数据库中所有表的记录数目?

昔梦无痕 2008-01-23 11:40:04
有个数据库有80多张表,我怎么能一次列出每个表里有多少条记录能,能一目了然的。谢谢大家。
...全文
476 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2008-01-23
  • 打赏
  • 举报
回复
--获取一个数据库中所有表(每个表)的记录数
declare @sql varchar(8000)
set @sql='select sum(num) as num from ('
select @sql=@sql+' select count(*) as num from '+name+' union all ' from sysobjects where xtype='u'
select @sql=@sql+' select 0) a'
exec(@sql)


declare @sql varchar(8000)
set @sql='select * from ('
select @sql=@sql+' select name = ''' + name + ''' , count(*) as num from '+name+' union all ' from sysobjects where xtype='u'
set @sql = left(@sql,len(@sql) - 10) + ')a'
exec(@sql)
昵称被占用了 2008-01-23
  • 打赏
  • 举报
回复
select o.name,i.rows
from sysobjects o,sysindexes i
where o.xtype='U'
and o.Id=i.Id
and i.Indid<2
order by o.name
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
EXEC   sp_MSforeachtable @command1=   "SELECT '?' as name,  count(*)   AS   num   FROM   ?   "
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
declare @sql varchar(8000),@sql2 varchar(8000),@sql3 varchar(8000)
select @sql=isnull(@sql+' union all ','')+'select count(*) as [con] from ['+name+']'
from (select top 10 name from sysobjects where xtype='U')tp
select @sql2=isnull(@sql2+' union all ','')+'select count(*) as [con] from ['+name+']'
from (select top 10 name from sysobjects where xtype='U')tp
select @sql3=isnull(@sql3+' union all ','')+'select count(*) as [con] from ['+name+']'
from (select top 10 name from sysobjects where xtype='U')tp
exec(@sql+' union all '+@sql2+' union all '+@sql3)


(30 行受影响)

佩服楼上
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'max' 附近有语法错误。
服务器: 消息 137,级别 15,状态 1,行 2
必须声明变量 '@sql'。
服务器: 消息 170,级别 15,状态 1,行 3
第 3 行: 'tp' 附近有语法错误。
服务器: 消息 137,级别 15,状态 1,行 4
必须声明变量 '@sql'。
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
to wzy_love_sly
我的是SQL 2000
你的代码我运行不了。
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
declare @s  varchar(8000),@i int,@j int
create table #1(s int,n varchar(50))
select identity(int,1,1) id,name n into # from sysobjects where xtype='U'
select @j=count(*) from #
set @i = 1
while @i <= @j
begin
select @s ='insert #1 select count(*),'''+n+''' from ['+n+']' from # where id = @i--[n]
exec(@s)
set @i = @i +1
end
select * from #1 order by n
drop table #,#1
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
declare @sql varchar(max)
select @sql=isnull(@sql+' union all ','')+'select count(*) as [con] from ['+name+']'
from (select top 255 name from sysobjects where xtype='U')tp
exec(@sql)


(255 行受影响)
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
为什么?我统计255个表都可以啊
---------------------
你的结果集中有255行吗?
当表的个数超过30个时,拼接的@sql 的长度有可能超过4000,
这时我的SQL SERVER 就强行截断@sql,后面的不要了,导致执行动态
语句错误,不知你们的怎么可以。有时正好截断的不是两个单引号之间的,
语句可以执行,但结果的行数少了很多。
ls采用临时表方式,就没有这些限制了。
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
搞定
declare @s  varchar(8000),@i int,@j int
create table #1(s int,n varchar(50))
select identity(int,1,1) id,name n into # from sysobjects where xtype='U'
select @j=count(*) from #
set @i = 1
while @i <= @j
begin
select @s ='insert #1 select count(*),'''+n+''' from '+n from # where id = @i
exec(@s)
set @i = @i +1
end
select * from #1 order by n
drop table #,#1
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
为什么?我统计255个表都可以啊
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
表的数目超过30个的话就必须用游标来做了。
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
表明别大于256
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
不行就变通变通,varchar(max)
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select * from ('
select @sql=@sql+' select name = ''' + name + ''' , count(*) as num from '+name+' union all ' from sysobjects where xtype='u'
set @sql = left(@sql,len(@sql) - 10) + ')a'
exec(@sql)
----------------------------------------------------------------------------
当len(@sql)>4000 时会自动截去后面的。
所有在我的数据库中执行报错。
tre_sdlpq 2008-01-23
  • 打赏
  • 举报
回复
select sysobjects.name,sysindexes.rows
from sysobjects ,sysindexes
where sysobjects.xtype='U'
and sysobjects.Id=sysindexes.Id
and sysindexes.Indid<2
order by sysobjects.name
yangjiexi 2008-01-23
  • 打赏
  • 举报
回复

--在MS SQL 数据库中每个表都在sysindexes 系统表中拥有至少一条记录,
--该记录中的rows 字段会定时记录表的记录总数。
chuifengde 2008-01-23
  • 打赏
  • 举报
回复
sp_msforeachtable 'select ''?'' 表名,  count(*) 记录数  FROM  ?'
JiangHongTao 2008-01-23
  • 打赏
  • 举报
回复
请高手解释一下
SELECT b.rows ,a.name  FROM   sysobjects   a   WITH(NOLOCK),  
sysindexes b WITH(NOLOCK)
WHERE a.xtype = 'U ' AND b.indid IN (0, 1)
AND a.id = b.id and a.name = 'T_ResearchUser'

select count(*) from T_ResearchUser
/*
rows name
----------- ---------------
249 T_ResearchUser

(所影响的行数为 1 行)

-----------
244

(所影响的行数为 1 行)
*/
加载更多回复(9)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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