tags:

views:

49

answers:

2

IN SQL Server 2008 is there a way to generate a report as to how much space on disk each table takes?

I can do it one by one by checking the storage properties, but I was wondering if there was a way, maybe even a query to generate such a report.

+2  A: 

Yes, use the sp_spaceused stored procedure to get this information...

USE AdventureWorks;
GO
EXEC sp_spaceused N'Purchasing.Vendor';
GO

Result will look like this:

name    rows    reserved    data    index_size  unused
Vendor  104         48 KB   16 KB   32 KB   0 KB

To get the stats for all the tables in the DB use:

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
kzen
+1, sp_spaceused is the way to go. I was trying to build a single query solution using the source code from it, when you posted your answer. You will not be able to easily sort your results though.
KM
+2  A: 

in addition to @kzen's correct answer, to get a single result set that is sortable use this:

declare @t table (name sysname,rows int,reserved nvarchar(40),data nvarchar(50),index_size nvarchar(50),unused nvarchar(50))
INSERT INTO @T
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

--order by reserved space
select * from @t
order by CONVERT(int,LEFT(reserved,CHARINDEX(' ',reserved))) DESC
KM
+1, I imagine that is the preferred resultset anyone would like to see or use in an app or on a web page...
kzen