伯乐共勉

讨论。NET专区
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

取得当前数据库中,所有表的大小

Posted on 2007-02-23 09:38  伯乐共勉  阅读(221)  评论(0编辑  收藏  举报

create procedure sp_allspaceused
as
begin

declare @tablename varchar(200)
set nocount on
if object_id('tempdb..#tablename') is not null
  drop table #tablename
create table #tablename (name varchar(200), rows int, reserved varchar(30), data varchar(30), index_size varchar(30), unused varchar(30))
declare tablecursor cursor for select name from sysobjects where xtype = 'U'
open tablecursor

fetch next from tablecursor into @tablename

while @@fetch_status = 0
begin
  insert into #tablename execute sp_spaceused @tablename
  fetch next from tablecursor into @tablename
end

close tablecursor
deallocate tablecursor

set nocount off
select * from #tablename order by
cast(replace(replace(reserved, ' KB', '000'), ' MB', '000000') as bigint) desc

end