码迷,mamicode.com
首页 > 数据库 > 详细

查询数据库中所有表的记录数和大小

时间:2015-02-13 16:13:47      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

if exists ( select  *
            from    dbo.sysobjects
            where   id = object_id(N‘[dbo].[TableSpace]‘)
                    and objectproperty(id, N‘IsUserTable‘) = 1 )
    drop table [dbo].[TableSpace]
go
create table TableSpace
    (
      TableName varchar(20) ,
      RowsCount char(11) ,
      Reserved varchar(18) ,
      Data varchar(18) ,
      Index_size varchar(18) ,
      Unused varchar(18)
    )
go
declare @sql varchar(500)
declare @TableName varchar(20)
declare mCursor cursor
for
select name from sysobjects where xtype=‘U‘
open mCursor
fetch NEXT from mCursor into @TableName
while @@fetch_status = 0
    begin
        set @sql = ‘insert into TableSpace ‘
        set @sql = @sql + ‘ exec sp_spaceused ‘‘‘ + @TableName + ‘‘‘ ‘
        exec (@sql)
        fetch NEXT from mCursor into @TableName
    end
close mCursor
deallocate mCursor
go
--显示结果
select TableName,RowsCount from TableSpace

查询数据库中所有表的记录数和大小

标签:

原文地址:http://www.cnblogs.com/stray-dog/p/4290428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!