Get table sizes from DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Get table sizes from DB

    My Database is now 75% of its maximum size so pretty soon I will need to do some archiving.
    I can view the number of rows and bytes for each table in Enterprise Manager but this is not ideal.
    I would like to run a query in SQL Analyzer that returns table name, no of rows, size(bytes) ordered by size.
    I assume I need to query information_sch ema or sysobjects but have little experience with this.
    Any help appreciated
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Here, try checking this out...

    Good Luck!!!

    ~~ CK

    Comment

    • nbiswas
      New Member
      • May 2009
      • 149

      #3
      Try this

      Code:
      create table #t(d nvarchar(MAX),t nvarchar(MAX),r int,x nvarchar(100),s nvarchar(100),y nvarchar(100),z nvarchar(100))
      declare @s nvarchar(MAX)
      set @s=replace('exec [~].dbo.sp_msforeachtable "insert into #t(t, r,x,s,y,z) exec [~].dbo.sp_spaceused !?!"','!',char(39))
      EXEC sp_MSForEachDB @command1=@s, @command2="update #t set d='~' where d is null", @replacechar='~'
      select d as DbName, t as [table], s as size, r as rows from #t order by Cast(LEFT(s,len(s)-3) as int) desc
      drop table #t

      Comment

      Working...