Important alert: (current site time 9/30/2014 3:32:17 PM EDT)
 

VB icon

Get SQL Table Size - Table and Index Space - Row Count

Email
Submitted on: 7/30/2003 11:46:07 PM
By: Zxtreme 
Level: Intermediate
User Rating: By 12 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 80080
 
     This Script will return the row count and the amount of disk space that each table uses within a specifed database. When returning total disk space used, it breaks it up into 3 categories... 1. The amount used by data 2. The Amount used by indexes 3. The amount of unused space Hope this helps you and please vote.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Get SQL Table Size - Table and Index Space - Row Count
-- Description:This Script will return the row count and the amount of disk space that each table uses within a specifed database. 
When returning total disk space used, it breaks it up into 3 categories...
 1. The amount used by data
 2. The Amount used by indexes 
 3. The amount of unused space
Hope this helps you and please vote.
-- By: Zxtreme
--
-- Inputs:See commented code
--
-- Returns:See commented code
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=732&lngWId=5--for details.--**************************************

SET NOCOUNT ON
/*DATABASE TABLE SPY SCRIPT
 Micheal Soelter
 1/24/03
DESCRIPTION
 Returns Table Size Information 
SORTING USAGE
 @Sort bit values
 0 = Alphabetically by table name
 1 = Sorted by total space used by table
*/
DECLARE @cmdstr varchar(100)
DECLARE @Sort bit
SELECT @Sort = 0 /* Edit this value for sorting options */
/* DO NOT EDIT ANY CODE BELOW THIS LINE */ 
--Create Temporary Table
CREATE TABLE #TempTable 
 (	[Table_Name] varchar(50),
	Row_Count int,
	Table_Size varchar(50),
	Data_Space_Used varchar(50),
	Index_Space_Used varchar(50),
	Unused_Space varchar(50)
 )
--Create Stored Procedure String
 SELECT @cmdstr = 'sp_msforeachtable ''sp_spaceused "?"'''
--Populate Tempoary Table
 INSERT INTO #TempTable EXEC(@cmdstr)
--Determine sorting method
IF @Sort = 0 
 BEGIN 
 --Retrieve Table Data and Sort Alphabetically
 SELECT * FROM #TempTable ORDER BY Table_Name
 END
ELSE
 BEGIN
 /*Retrieve Table Data and Sort by the size of the Table*/
 SELECT * FROM #TempTable ORDER BY Table_Size DESC
 END
--Delete Temporay Table
DROP TABLE #TempTable


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/31/2003 12:49:12 PMChristina McEntire

This code is helpful. Thank you. :)
(If this comment was disrespectful, please report it.)

 
8/18/2003 5:44:07 PM

Glad it helped
(If this comment was disrespectful, please report it.)

 
10/8/2003 11:06:07 AMTom Bruinsma

I like it, keep up the good work!
(If this comment was disrespectful, please report it.)

 
5/24/2006 12:53:34 PMCharles Kincaid

Wonderful! Very helpful.
There is one thing. In SQL 2005 the space columns are character type and have KB stuck on the end. Therefore the sort by size does not work.
(If this comment was disrespectful, please report it.)

 
12/12/2006 2:22:21 AMAdel Khader

good and easy to use.
The sort is not working when it is based on Size because it is varchar not int field.
(If this comment was disrespectful, please report it.)

 
12/26/2006 4:03:00 PMJeff Kastner

Isn't this basically the same thing as running sp_spaceused
?
Good script though.. runs well.
(If this comment was disrespectful, please report it.)

 
12/27/2006 6:00:48 PMSean

Changing the order by to this will make it sort as an integer.

ORDER BY Cast(LTRIM(RTRIM(Replace(Table_Size, 'KB', ''))) as integer) DESC
(If this comment was disrespectful, please report it.)

 
1/18/2010 8:23:03 AMRRuth

I realize this script is going back 7 years now but It's pretty fast on my sql server 2000 machine so I'd like to use it. Just wondering if you caould tell me what the column 'Unused Space' represents. for some tables I have a positive, some a zero and a few have a negative number.
Thanks!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.