I'm sure you can get something better than this but this should do in a pinch. For the record, sp_spaceused
is your friend in queries such as this!
DECLARE @tbls table (TableID int identity, TableName varchar(max))
INSERT INTO @tbls
SELECT t1.name
FROM sysobjects t1
INNER JOIN sysindexes t2
ON t1.id = t2.id
WHERE t2.indid <= 1
AND t2.rows > 0
AND OBJECTPROPERTY(t1.id,'IsUserTable') = 1
DECLARE @tblcount int
SELECT @tblcount=COUNT(1)
FROM @tbls
DECLARE @results table (name varchar(max), [rows] bigint, reserved varchar(max), data varchar(max), index_size varchar(max), unused varchar(max))
DECLARE @counter int
SET @counter = 0
DECLARE @tblname varchar(max)
while @counter < @tblcount
begin
SELECT @tblname=TableName FROM @tbls WHERE TableID=@counter+1
INSERT INTO @results
exec sp_spaceused @tblname
SET @counter = @counter+1
end
SELECT * FROM @results