一、查看表名和对应的数据行数
select a.name as '表名',b.rows as '表数据行数'from sysobjects a inner join sysindexes bon a.id = b.idwhere a.type = 'u'and b.indid in (0,1)--and a.name not like 't%'order by b.rows desc 二、查看表名和表占用空间信息--判断临时表是否存在,存在则删除重建if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')drop table #tabNamegocreate table #tabName(tabname varchar(100),rowsNum varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused_size varchar(100)) declare @name varchar(100)declare cur cursor forselect name from sysobjects where xtype='u' order by nameopen curfetch next from cur into @namewhile @@fetch_status=0begin insert into #tabName exec sp_spaceused @name --print @name fetch next from cur into @nameendclose curdeallocate curselect tabname as '表名',rowsNum as '表数据行数',reserved as '保留大小',data as '数据大小',index_size as '索引大小',unused_size as '未使用大小'
from #tabName--where tabName not like 't%'order by cast(rowsNum as int) desc--系统存储过程说明:
--sp_spaceused 该存储过程在系统数据库master下。
exec sp_spaceused '表名' --该表占用空间信息exec sp_spaceused --当前数据库占用空间信息
以下的文章主要是教会你如何正确查看 SQL Server 2000 数据表的大小,下面这几条相关的语句,可以返回每个SQL Server 2000 数据表大小,以下就是具体方案的描述,希望在你今后的学习中会有所帮助。
create table tmp (name varchar(50),rows int,reserved varchar(50), data varchar(50),index_size varchar(50),unused varchar(50)) insert into tmp (name,rows,reserved, data,index_size,unused) exec sp_msforeachTable @Command1="sp_spaceused '?'"sp_spaceused 't_vehicle' select * from tmp order by data desc drop table tmp 看单个表, 占用数据的情况sp_spaceused '表名'
查看整个数据库所有表占用空间的情况 但发现从sysindexes中取表, 会少一部分SQL Server 2000 数据表(无主键,无索引的表)
select object_name(id) tablename,8*reserved/1024 reserved,rtrim(8*dpages/1024)+'Mb' used,8*(reserved-dpages)/1024 unused,8*dpages/1024-rows/1024*minlen/1024 free, rows,* from sysindexes where indid=1 order by reserved desc 查看整个数据库所有表占用空间的情况 但看起来比较麻烦
exec sp_MSforeachtable "exec sp_spaceused '?'"
drop table #Data
drop table #DataNewcreate table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100)) declare @name varchar(100) declare cur cursor for select name from sysobjects where xtype='u' order by name open cur fetch next from cur into @name while @@fetch_status=0 begin insert into #data exec sp_spaceused @name print @name fetch next from cur into @name end close cur deallocate cur create table #DataNew(name varchar(100),row int,reserved int,data int,index_size int,unused int) insert into #dataNew select name,convert(int,row) as row,convert(int,replace(reserved,'KB','')) as reserved,convert(int,replace(data,'KB','')) as data, convert(int,replace(index_size,'KB','')) as index_size,convert(int,replace(unused,'KB','')) as unused from #data select * from #dataNew order by data desc