查看sqlserver数据库中表大小

人盡茶涼 提交于 2020-02-03 07:07:57
 
 
 
 
--itlearner注:显示表信息
select * 
from tablespaceinfo 
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc
 
 
 
--在查询分析器中运行
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 o.name 
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1 
and o.name not like N'#%%' order by o.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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!