SQL 查看数据库表的容量大小

时间:2021-11-22 23:17:55
 1 --==============查看数据库表的容量大小========start================================?============
 2 Create Table #TableSpaceInfo --创建结果存储表 
 3 (
 4   NameInfo NVarchar(50) , 
 5   RowsInfo int , 
 6   Reserved NVarchar(20) , 
 7   DataInfo NVarchar(20) , 
 8   Index_Size NVarchar(20) , 
 9   Unused NVarchar(20) 
10 )
11 
12 
13 Declare @TableName NVarchar(255) --表名称
14 Declare @CmdSql NVarchar(1000)
15 
16 Declare Info_Cursor Cursor For
17 Select o.Name 
18 From dbo.sysobjects o 
19 Where objectProperty(o.ID, N'IsTable') = 1 and o.Name not like N'#%%' Order By o.Name
20 
21 Open Info_Cursor
22 
23 Fetch Next From Info_Cursor 
24 Into @TableName
25 
26 While @@FETCH_STATUS = 0 
27 Begin
28   If exists (Select * From dbo.sysobjects Where ID=object_ID(@tablename) and objectProperty(ID, N'IsUserTable') = 1) 
29     Execute sp_executesql N'Insert Into #TableSpaceInfo Exec sp_Spaceused @TBName', N'@TBName NVarchar(255)', @TBName = @TableName
30 
31   Fetch Next From Info_Cursor 
32   Into @TableName 
33 End
34 
35 Close Info_Cursor 
36 Deallocate Info_cursor 
37 GO
38 
39 
40 --itlearner注:显示数据库信息 
41 sp_spaceused @UpdateUsage = 'TRUE'
42 
43 --itlearner注:显示表信息 
44 Select * 
45 From #TableSpaceInfo 
46 Order By cast(left(lTrim(rTrim(Reserved)) , len(lTrim(rTrim(Reserved)))-2) As Int) Desc
47 
48 
49 Drop Table #TableSpaceInfo
50 --================查看数据库表的容量大小=====end========================?==========================