if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
create table tablespaceinfo --创建临时表
(nameinfo varchar(50) , --表名
rowsinfo int , --表中现有的行数
reserved varchar(20) , --表空间总量
datainfo varchar(20) , --表中数据的空间量
index_size varchar(20) , --表中索引使用的空间量
unused varchar(20) ) --表中未用的空间量
delete from tablespaceinfo
declare @tablename varchar(255)
declare @cmdsql varchar(500)
DECLARE Info_cursor CURSOR FOR
select name
from dbo.sysobjects where OBJECTPROPERTY(id, N'IsTable') = 1
and name not like N'#%%' order by name
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
execute sp_executesql
N'insert into tablespaceinfo exec sp_spaceused @tbname',
N'@tbname varchar(255)',
@tbname = @tablename
FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO
--表空间信息
select *
from tablespaceinfo
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc
--转自