日期:2014-05-18 浏览次数:20627 次
declare @a varchar(100) declare csr_tbname cursor for select [name] from sys.objects where type='U' open csr_tbname fetch next from csr_tbname into @a; while @@FETCH_STATUS=0 begin exec sp_spaceused @a; fetch next from csr_tbname into @a; end close csr_tbname; deallocate csr_tbname; -- 这样查出来的总和也不会等于数据库的大小,我试过,并且差距很大。 -- sp_helpdb
------解决方案--------------------
2005后,用这个存储过程看看。
USE MASTER GO IF OBJECT_ID('sp_tableinfo') IS NOT NULL DROP PROC sp_tableinfo GO CREATE PROCEDURE sp_tableinfo @tblPat SYSNAME = '%' ,@sort CHAR(1) = 'm' AS --Written by Tibor Karaszi 2010-09-30 --Modified 2010-10-10, fixed rowcount multiplied by number of indexes. --Modified 2010-10-11, fixed rowcount incorrect with BLOB and row overflow data. WITH t AS ( SELECT SCHEMA_NAME(t.schema_id) AS schema_name ,t.name AS table_name ,SUM(CASE WHEN p.index_id IN(0,1) AND a.type_desc = 'IN_ROW_DATA' THEN p.rows ELSE 0 END) AS rows ,SUM(CAST((a.total_pages * 8.00) / 1024 AS DECIMAL(9,2))) AS MB ,SUM(a.total_pages) AS pages ,ds.name AS location FROM sys.tables AS t INNER JOIN sys.partitions AS p ON t.OBJECT_ID = p.OBJECT_ID INNER JOIN sys.allocation_units AS a ON p.hobt_id = a.container_id INNER JOIN sys.data_spaces AS ds ON a.data_space_id = ds.data_space_id WHERE t.name LIKE @tblPat GROUP BY SCHEMA_NAME(t.schema_id), t.name, ds.name ) SELECT schema_name, table_name, rows, MB, pages, location FROM t ORDER BY CASE WHEN @sort = 'n' THEN table_name END ,CASE WHEN @sort = 'r' THEN rows END DESC ,CASE WHEN @sort = 'm' THEN MB END DESC ,CASE WHEN @sort = 's' THEN schema_name END GO