免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: tigerfish
打印 上一主题 下一主题

資料庫的大小及筆數如何統計 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-07-08 16:42 |只看该作者

是啊

是所有用戶表記錄數之和

论坛徽章:
0
12 [报告]
发表于 2005-07-08 18:42 |只看该作者
我想他要的不是這個結果吧

论坛徽章:
0
13 [报告]
发表于 2005-07-09 13:21 |只看该作者

Re: 資料庫的大小及筆數如何統計

最初由 jingliangme 发布
[B]我想知道一個db有多少筆資料及總的大小,有沒有好的存儲過程可以計算呢  是不是要查出每個表然後相加呢  請大家幫忙出個注意
謝謝! [/B]


希望这个贴子能帮到你。[ZT]
近来一个项目要求做几个数据库的比较,其中有几项指标和数据库的表有关:

用户表个数

最大表记录数

记录总条数

如果靠手工一个表一个表的去查看统计,不仅枯燥费时,而且灵活性和扩展都不是很好,可能主要还是想偷懒的原因吧,今天花了点时间写了个简单的存储过程,只是为了快点完成任务,还没得及考虑方便性和处理错误,下面是源代码,原理很简单,参看代码中注释。


create procedure sp_tableCount
@newTable varchar(50),--new create table name
@isSet int            --whether return new table recordset,non 0--return
as
declare @TableName nvarchar(100);
declare @sql nvarchar(800)

SET NOCOUNT ON
----create a target table named @newTable param value--------
IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES
      WHERE table_name = @newTable)
   exec(\'DROP TABLE \'+@newTable)
-----create target table------------
set @sql=\'create table \' + @newTable + \'
(
  Categary nvarchar(100) not null,
  Value    int
)\'
exec(@sql)  

----------add user tables count into target table----------------
set @sql=\'insert into \'+@newTable+\' select \'\'User Tables\'\',count(*)-1  from sysobjects where type=\'\'U\'\'\'
exec(@sql)

--------define a cursor pointing the user tablename recordset--------
declare TableName_Cursor CURSOR FOR
select name  from sysobjects where  type=\'U\'and name<>@newTable


open TableName_Cursor

fetch next from TableName_Cursor into @TableName

-------append every user table recordcount to target table----------------
while @@Fetch_Status=0
begin
  set @sql=\'insert into \'+@newTable+\' select N\'\'\'+@TableName+\'\'\',count(*) from \'  + @TableName
  exec(@sql)


  fetch next from TableName_Cursor into @TableName
end

-------release  resource occupied by TableName_Cursor --------
close TableName_Cursor
deallocate TableName_Cursor

--------deal with the @isSet param-----------------
if @isSet<>0
exec(\'select * from \'+@newTable)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP