SQL获取数据表的空间占用
分享我写的一个存储过程,调用系统存储过程来返回表的空间占用情况:
代码
1 --获取所有用户表的空间占用情况
2 create procedure sp_get_table_space_info
3 as
4 begin
5 if not exists(select * from sysobjects where xtype='U' and name='TableSpaceInfo')
6 create table TableSpaceInfo(
7 name varchar(255),
8 rows bigint,
9 reserved varchar(20),
10 data varchar(20),
11 index_size varchar(20),
12 unused varchar(20)
13 )
14 delete from TableSpaceInfo
15 exec sp_MSforeachtable "insert into TableSpaceInfo exec sp_spaceused '?'"
16 end
17 go
2 create procedure sp_get_table_space_info
3 as
4 begin
5 if not exists(select * from sysobjects where xtype='U' and name='TableSpaceInfo')
6 create table TableSpaceInfo(
7 name varchar(255),
8 rows bigint,
9 reserved varchar(20),
10 data varchar(20),
11 index_size varchar(20),
12 unused varchar(20)
13 )
14 delete from TableSpaceInfo
15 exec sp_MSforeachtable "insert into TableSpaceInfo exec sp_spaceused '?'"
16 end
17 go
调用的时候执行一次,然后直接查询 TableSpaceInfo 表就可以了!
获取数据库的大小执行 exec sp_spaceused 就可以了