列出Server上5張最大的表

iSQlServer發表於2009-08-03

        遍歷所有資料庫,但不包括系統資料庫。列出表所在數庫名,表名,表的大小,表的記錄數。非常有趣,T-SQL 看下面:

   1:  create table #t(d nvarchar(MAX),t nvarchar(MAX),r int,x nvarchar(100),s nvarchar(100),y nvarchar(100),z nvarchar(100))
   2:  declare @s nvarchar(MAX)
   3:  set @s=replace('if !~! not in (!master!,!model!,!msdb!,!tempdb!) exec [~].dbo.sp_msforeachtable "insert into #t(t, r,x,s,y,z) exec [~].dbo.sp_spaceused !?!"','!',char(39))
   4:  EXEC sp_MSForEachDB @command1=@s, @command2="update #t set d='~' where d is null", @replacechar='~'
   5:  select top(5) d as base, t as [table], s as size, r as rows from #t order by Cast(LEFT(s,len(s)-3) as int) desc
   6:  drop table #t

 

       結果類似如下:

BASE     TABLE   SIZE        ROWS      
----------------------------------
base1    T1      162720 KB   14233       
base2    T2      38000 KB    8462       
base1    T3      10720 KB    5343        
base1    T4      9040 KB     5006       
base3    T5      2800 KB     1899  
 原文地址:http://www.cnblogs.com/wintersun/archive/2009/08/02/1537006.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-611142/,如需轉載,請註明出處,否則將追究法律責任。

相關文章