欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » Sql随手笔记 » 正文

热门阅读

利用SQL查询数据库中所有表的数据行数

创建时间:2011年10月12日 12:00  阅读次数:(7393)
分享到:
今天在客户实施时,因经常性的需要查看用户录入到数据库中数据的数量,所以写了以下SQL语句用来查询数据库中所有表的数据量,发布到此,希望能给有同样需求的朋友带来帮助。

declare @TableName varchar(128)
declare @T_TableRows Table
(
TableName varchar(128) not null,--表名
RowsCount int not null default(0)--数据的数量
)

declare cur_table cursor local for
select name from sys.tables order by name
open cur_table
fetch cur_table into @TableName
while @@fetch_status = 0
begin
insert into @T_TableRows(TableName,RowsCount)
exec('select ''' + @TableName + ''',count(*) from ' + @TableName)
fetch cur_table into @TableName
end
close cur_table
deallocate cur_table

在上面的SQL中用到了sys.tables表,关于该有的详细用法,请参见本站:
利用SQL语句查询数据库中所有表

说明:因为sys.tables表只有sql2005以后才有,所以使用Sql2000的朋友可以使用sysobjects表来替代sys.tables表实现该功能:
SQL如下:
declare @TableName varchar(128)
declare @T_TableRows Table(TableName varchar(128) not null,RowsCount int not null default(0))

declare cur_table cursor local for
select name from sysobjects where xtype='u' order by name
open cur_table
fetch cur_table into @TableName
while @@fetch_status = 0
begin
insert into @T_TableRows(TableName,RowsCount)
exec('select ''' + @TableName + ''',count(*) from ' + @TableName)
fetch cur_table into @TableName
end
close cur_table
deallocate cur_table

select * from @T_TableRows order by RowsCount desc,TableName
来源:.net学习网
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 60+28=? 的结果(结果是:88)
结  果: