How to determine (via transact SQL) space usage per table

I know I can use Enterprise Manager to display rowcounts and space usage on a per table basis
(Select database, Views, TaskPad and then look at the table info tab). I would like to be able to get the same information via transact SQL so that I can manipulate the results programmatically.

LVL 6
gwalkeriqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulKeatingCommented:
Here is half of your solution:

exec sp_spaceused mytable

This will return the data you want, but only to the console. To get it into variables, I think you may have to copy sp_spaceused and hack it to have output parameters.
0
ABaruhCommented:
CREATE TABLE #UsageData
(
     [name] varchar(255)
     , [rows] varchar(255)
     , [reserved] varchar(255)
     , [data] varchar(255)
     , index_size varchar(255)
     , [unused] varchar(255)
)

INSERT #UsageData
exec sp_spaceused mytable

This gives you a temp table with data you could use elsewhere
0
SjoerdVerweijCommented:
These are the guts of a stored proc I use to see which tables are hoggin' the most. Let me know if this helps.

  Set NoCount On


  Create Table #Res
   (Name SysName,
    Rows Int,
    Reserved VarChar(20),
    Data VarChar(20),
    Index_Size VarChar(20),
    Unused VarChar(20))


  Declare
    @More Bit,
    @CTableName As SysName

  Declare CTable Cursor
  Local Fast_Forward
  For
  Select
    Table_Name
  From
    Information_Schema.Tables
  Where
    Table_Type = 'Base table'


  Set @More = 1
  Open CTable


  While (@More = 1)
    Begin

      Fetch Next From CTable
      Into @CTableName

      If (@@Fetch_Status != 0)
        Set @More = 0      
      Else
        Insert Into #Res
        Execute sp_SpaceUsed @CTableName

    End


  Select * From #Res
  Order By Cast(Left(Reserved, Len(Reserved) - 3) As Int) Desc

  Drop Table #Res


  Set NoCount Off
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.