-==========================================================================-- 说明: 本脚本用于查询当前数据库中所有表格的记录条数-- 并将结果存入TableInfo表中,不会删除以备用户再做处理与分析-- 不过,最后请用户删除此表。--==========================================================================
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 Cursor1 Cursorfor select name from sysobjects where xtype='U'
open Cursor1fetch next from Cursor1 into @TableName
while @@fetch_status = 0begin set @sql = 'insert into TableSpace ' set @sql = @sql + ' exec sp_spaceused '''+ @TableName + ''' ' exec (@sql) fetch next from Cursor1 into @TableNameendclose Cursor1deallocate Cursor1go
--显示结果select * from TableSpace--order by TableName--order by TableName ASC --按表名称,用于统计表--order by RowsCount DESC --按行数量,用于查看表行数--order by Reserved DESC, Data DESC --按占用空间--order by Index_size DESC, Reserved DESC --按索引空间查看go
--查看库的使用状况,可以随时执行的。--exec sp_spaceused--go