See the size of tables using t-sql

  • Hi,

    I have a list of tables (several tables) and I would like to know their size.

    Is there a way to know that using t-sql?

    It will be easyer then having to select each one from the diagram or having to look at the reports, one by one...

    Thank you

  • sp_spaceused 'TableName'

    or query sys.dm_db_index_physical_stats

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank you Gail

  • river1 (1/20/2016)


    It will be easyer then having to select each one from the diagram or having to look at the reports, one by one...

    If you right-click on the database name, you can get a size report for all tables.

    John

  • Exec sys.sp_MSforeachtable ' sp_spaceused "?" '

  • river1 (1/20/2016)


    Hi,

    I have a list of tables (several tables) and I would like to know their size.

    Is there a way to know that using t-sql?

    It will be easyer then having to select each one from the diagram or having to look at the reports, one by one...

    Thank you

    WHICH size? Reserved? Used? Data Size? Index Size? Unused?

    Don't answer that. It's a rhetorical question.

    Rather than messing around with sp_space used or some unsupported stored procedure that runs a cursor in the background, open the code for sp_SpaceUsed and see how they did it. Then, convert it to a set based wonder of your own making to work against one or all of the tables in the current database.

    I'd post my own creation in this area but then the only thing you'd learn is how to use my stuff. If you take the time to look at the code of sp_SpaceUsed, you're going to learn and remember stuff that will help you throughout your career.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/22/2016)


    river1 (1/20/2016)


    Hi,

    I have a list of tables (several tables) and I would like to know their size.

    Is there a way to know that using t-sql?

    It will be easyer then having to select each one from the diagram or having to look at the reports, one by one...

    Thank you

    WHICH size? Reserved? Used? Data Size? Index Size? Unused?

    Don't answer that. It's a rhetorical question.

    Rather than messing around with sp_space used or some unsupported stored procedure that runs a cursor in the background, open the code for sp_SpaceUsed and see how they did it. Then, convert it to a set based wonder of your own making to work against one or all of the tables in the current database.

    I'd post my own creation in this area but then the only thing you'd learn is how to use my stuff. If you take the time to look at the code of sp_SpaceUsed, you're going to learn and remember stuff that will help you throughout your career.

    Hmmm. I wrote a procedure to get the space for all the tables in a database, but it's a wrapper to sp_spaceused to build each row. Now you've got me thinking I really need to dig into sp_spaceused itself.

    Thanks a lot, Jeff. Now I have something else to do. 😛 That's okay though, I know I'll have fun with it.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply