SqlServer中查看数据库所有表的表空间和索引空间信息

时间:2022-12-16 04:34:35
--方法1
exec sp_MSforeachtable @command1="EXEC sp_spaceused '?'";

会返回多个结果集,可能不是所希望的结果。进一步处理,参考方法2 

--方法2
create table #tblResults(
[name] nvarchar(50), --注意有表名会长于50
[rows] int,
[reserved] varchar(18),
[reserved_int] int default(0),
[data] varchar(18),
[data_int] int default(0),
[index_size] varchar(18),
[index_size_int] int default(0),
[unused] varchar(18),
[unused_int] int default(0)
)
exec sp_MSforeachtable @command1 = 
"insert into #tblResults([name],[rows],[reserved],[data],[index_size],[unused])
exec sp_spaceused '?' "
update #tblResults set
 [reserved_int] =CAST(substring([reserved],1,CHARINDEX(' ',[reserved])) as int),
 [data_int] =CAST(substring([data],1,CHARINDEX(' ',[data])) as int),
 [index_size_int] =CAST(substring([index_size],1,CHARINDEX(' ',[index_size])) as int),
 [unused_int] =CAST(substring([unused],1,CHARINDEX(' ',[unused])) as int)
 
 select * from #tblResults

 

 注:原文链接地址http://www.4guysfromrolla.com/webtech/032906-1.shtml