SQL Server查询数据库中表使用空间信息实现脚本

时间:2021-12-26 10:35:24

使用了微软提供的不公开的存储过程sp_MSforeachtable
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CREATE TABLE #temp
(
 TableName NVARCHAR (128),
  RowsCnt VARCHAR (11),
 ReservedSpace VARCHAR(18),
 DataSpace VARCHAR(18),
 CombinedIndexSpace VARCHAR(18),
 UnusedSpace VARCHAR(18)
 )
 
 EXEC sp_MSforeachtable'INSERT INTO #temp (TableName, RowsCnt, ReservedSpace,DataSpace, CombinedIndexSpace, UnusedSpace) EXEC sp_spaceused ''?'', FALSE'
 
SELECT TableName, RowsCnt,ReservedSpace, DataSpace, CombinedIndexSpace, UnusedSpace
 FROM #temp
 ORDER BY TableName
 
DROP TABLE #temp