--企 蠏谿..
if object_id('tempdb..#size') is not null
drop table #size
create table #size
(
name varchar(500)
, rows bigint
, reserved varchar(500)
, data varchar(500)
, index_size varchar(500)
, unused varchar(500)
);
if object_id('tempdb..#temp') is not null
drop table #temp
create table #temp
(
[schema_name] varchar(500)
, table_name varchar(500)
, rows bigint
, reserved varchar(500)
, data varchar(500)
, index_size varchar(500)
, unused varchar(500)
);
declare
@sch_name varchar(50)
, @tbl_name varchar(50)
, @nm varchar(100);
declare cur cursor for
select table_schema, table_name
from information_schema.tables
open cur;
fetch next from cur into @sch_name, @tbl_name;
while @@fetch_status not in (-1, -2)
begin
set @nm = @sch_name + '.' + @tbl_name;
insert #size exec sp_spaceused @nm;
insert #temp
select @sch_name, * from #size;
truncate table #size;
fetch next from cur into @sch_name, @tbl_name;
end
CLOSE cur;
DEALLOCATE cur;
select
[schema_name]
, sum(reserved) - sum(unused) disk_size
from (
select
[schema_name]
, convert(real, replace(reserved, ' KB', '')) / 1024 reserved
, convert(real, replace(unused, ' KB', '')) / 1024 unused
from #temp
) t
group by [schema_name]
order by disk_size desc
select
[schema_name]
, table_name
, rows
, data
, index_size
, convert(real, replace(reserved, ' KB', '')) / 1024 reserved
, convert(real, replace(unused, ' KB', '')) / 1024 unused
from #temp
where rows is not null
order by reserved desc
drop table #temp
drop table #size