查询某个库下所有表及其占用空间大小

@name:表名、reserved:存储、data:数据、index_size:索引、unused:空闲、

create table #Data12138(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100)) 
 
declare @name varchar(100) 
declare cur cursor  for 
    select name from sysobjects where xtype='u' order by name 
open cur 
fetch next from cur into @name 
while @@fetch_status=0 
begin 
    insert into #Data12138 
    exec sp_spaceused   @name 
    print @name 
 
    fetch next from cur into @name 
end 
close cur 
deallocate cur 
 
create table #Data12138New(name varchar(100),row int,reserved int,data int,index_size int,unused int) 
 
insert into #Data12138New 
select name,convert(int,row) as row,convert(int,replace(reserved,'KB','')) as reserved,convert(int,replace(data,'KB','')) as data, 
convert(int,replace(index_size,'KB','')) as index_size,convert(int,replace(unused,'KB','')) as unused from #Data12138  
 
select * from #Data12138New order by data desc

drop table  #Data12138
drop table #Data12138New

 

posted @ 2022-09-01 09:38  PrintY  阅读(43)  评论(0编辑  收藏  举报