There are several options here:
sp_msforeachtable
sp_msforeachtable 'exec sp_spaceused [?]'
While sp_msforeachtable can be used for this, it has several limitations:
- Firstly, you get the result for each table that is running, which is difficult to work with
- If you run too many tables, you fall within the scope of the result sets that SSMS will support (I think this is usually around 200).
Compiled Results
With a little work, we can collect all the results in one data set, avoiding these limitations. The structure of this solution is similar whenever you need to run sproc several times with different parameters and compare the results of these runs.
--Get list of interesting tables declare @tables table(id int identity(1,1), name varchar(200)) declare @tablename varchar(200) insert @tables select table_name from information_schema.tables where table_type = 'BASE TABLE' --Define table for results declare @info table(name varchar(200), rows bigint, reserved varchar(200), data varchar(200), index_size varchar(200), unused varchar(200)) --Working vars declare @max int, @pos int select @max = count(1), @pos = 1 from @tables --Loop to get all results while @pos <= @max begin select @tablename = name from @tables where id = @pos insert @info exec sp_spaceused @tablename set @pos = @pos + 1 end --return all results select * from @info
I prefer to use variable tables for this, but you can also use a cursor / tempo table.
source share