前事不忘,后事之师,不忘国耻!

 注册  找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2199|回复: 0

[开发应用] 如何获取SQL Server数据库里表的占用容量大小

[复制链接]

[开发应用] 如何获取SQL Server数据库里表的占用容量大小

[复制链接]
ehxz

主题

0

回帖

7251

积分

管理员

积分
7251
2007-10-11 12:08:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
其实只要使用系统内置的存储过程sp_spaceused就可以得到表的相关信息
如:sp_spaceused 'tablename'

以下是为了方便写的一个存储过程,目的是把当前的所有表的相关信息全部都保存在一个指定的表里面
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gifCREATE
PROCEDURE get_tableinfo AS
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
if
not
exists (select
*
from dbo.sysobjects where id =
object_id(N'[dbo].[tablespaceinfo]') and
OBJECTPROPERTY(id, N'IsUserTable') =
1)
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
create
table  tablespaceinfo                         --创建结果存储表
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
              (nameinfo varchar(50) ,  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif               rowsinfo
int , reserved varchar(20) ,  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif               datainfo
varchar(20)  ,  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif               index_size
varchar(20) ,  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif               unused
varchar(20) )
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
delete
from tablespaceinfo --清空数据表
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif

http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
declare
@tablename
varchar(255)  --表名称
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif

http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
declare
@cmdsql
varchar(500)
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
DECLARE Info_cursor CURSOR
FOR  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
select o.name   
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
from dbo.sysobjects o where
OBJECTPROPERTY(o.id, N'IsTable') =
1  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif     
and o.name not
like N'#%%'
order
by o.name
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
OPEN Info_cursor
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
FETCH
NEXT
FROM Info_cursor  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
INTO
@tablename  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
WHILE
@@FETCH_STATUS
=
0
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
BEGIN
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif  
if
exists (select
*
from dbo.sysobjects where id =
object_id(@tablename) and
OBJECTPROPERTY(id, N'IsUserTable') =
1)
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif  
execute sp_executesql  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif         N
'insert into tablespaceinfo  exec sp_spaceused @tbname',
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif          N
'@tbname varchar(255)',
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif         
@tbname
=
@tablename
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif  
FETCH
NEXT
FROM Info_cursor  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif  
INTO
@tablename  
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
END
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
CLOSE Info_cursor
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
DEALLOCATE Info_cursor
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif
GO
http://keenx.cnblogs.com/Images/OutliningIndicators/None.gif


执行存储过程
exec get_tableinfo

查询运行该存储过程后得到的结果
select *
from tablespaceinfo  
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc
免责申明1、欢迎访问本站,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2、本文内容仅代表作者观点,不代表本站立场,作者自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3、本文内容,包括但不限于源码、文字、图片等,仅供参考。本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容。
4、如本帖侵犯到任何版权问题,请立即告知本站 ,本站将及时删除并致以最深的歉意!客服邮箱:admin@dbabbs.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|DBA论坛中国 ( 鲁ICP备20017503号-2 )

GMT+8, 2024-12-4 01:38 , Processed in 0.037512 second(s), 11 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表