声明:博客内所有技术性文章都是由作者本人工作经验的总结, 实现过程中有搜索资料为参考的绝不涉嫌抄袭,均为个人习惯的总结。今天需要操作一下网站后台,添加一些图片,发现100M的数据库空间满了,所以想知道所有表占用空间的大小,以下为脚本内容:

set nocount on

declare @db varchar(20)

set @db = db_name()

dbcc updateusage(@db) with no_infomsgs

go


create table #tblspace

(


    数据表名称 varchar(50) null,

    记录笔数 int null,

    保留空间 varchar(15) null,

    数据使用空间 varchar(15) null,

    索引使用空间 varchar(15) null,

    未使用空间 varchar(15) null,


)

declare @tblname varchar(50)

declare curtbls cursor for

    select table_name from information_schema.tables

    where table_type = 'base table'

open curtbls

Fetch next from curtbls into @tblname

while @@fetch_status = 0

begin

    insert #tblspace exec sp_spaceused @tblname

    fetch next from curtbls into @tblname

end

close curtbls

deallocate curtbls


select * from #tblspace order by

convert(int,left(保留空间,len(保留空间)-2)) desc

drop table #tblspace

这个方法是我从网上众多方法里发掘出来的,经过试验证明完全可以达到理想的效果,以下为效果图

结束

恭喜你成功看到了sql数据库每个表占用硬盘空间的大小。