统计数据库中表大小

use testdb
go
if object_id('tempdb.dbo.#tablespaceinfo','U') is not null
  drop table #tablespaceinfo
create table #tablespaceinfo (  
    nameinfo varchar(555),  
    rowsinfo bigint,  
    reserved varchar(255),  
    datainfo varchar(255),  
    index_size varchar(255),  
    unused varchar(255)  
)  
 
DECLARE @tablename varchar(255);  
 
DECLARE Info_cursor CURSOR FOR
    SELECT [name] FROM sys.tables WHERE type='U';  
 
OPEN Info_cursor  
FETCH NEXT FROM Info_cursor INTO @tablename  
 
WHILE @@FETCH_STATUS = 0  
BEGIN
    insert into #tablespaceinfo exec sp_spaceused @tablename  
    FETCH NEXT FROM Info_cursor  
    INTO @tablename  
END
 
CLOSE Info_cursor  
DEALLOCATE Info_cursor  
 
if object_id('tempdb.dbo.#tab','U') is not null
  drop table #tab
SELECT
 nameinfo
 ,rowsinfo
 ,cast(replace(reserved,' KB','') as bigint)/1024 "reserved(MB)"
 ,cast(replace(datainfo,' KB','') as bigint)/1024 "datainfo(MB)"
 ,cast(replace(index_size,' KB','') as bigint)/1024 "index_size(MB)"
 ,cast(replace(unused,' KB','') as bigint)/1024 "unused(MB)"
into #tab
FROM #tablespaceinfo  
ORDER BY Cast(Replace(reserved,'KB','') as INT) DESC

创新互联公司成立10余年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都网站设计、成都网站建设、网站策划、网页设计、域名与空间、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联公司通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。


文章名称:统计数据库中表大小
网页地址:http://scyanting.com/article/ijggeh.html