How to get table,database size in Sql Server

No.of Views1370
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  18 May 2010 10:05:42
Tag : Sql Servers , Database Administrator
How to get table,database size in Sql Server
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article,i would share with you, how to get size of the tables in database in sql server.When you are move your database to production, then you have to check the database size once week or month. becuase host provider give the limited database capacity.

1.Get Database size.

EXEC sp_spaceused

 Result

Image Loading

In above figure,when we are excute the sp_spaceused without any parameters, it will take defult parameter as null and refurn the current database size and details about that database.

2.How to get a table size and other details.

EXEC sp_spaceused 'User_Details'

Result

Image Loading

In above figure, we have excute the same stored procedure, but this time we are specified the table name to get the detail.it has been return how many bytes used and how is allocated as well.even you able to see how many rows has in current state.

3.How to get all table size and other details

In this time we have to use our custom procedure to do this, becuase there is no inbuilt proc to get this details in sql server.how we can do this, let create a procedure and get all tables name from the database and fetch into cursor.

CREATE PROCEDURE spCodeGainSizeUtil
AS
DECLARE @TableName VARCHAR(100)    
DECLARE dataCursor CURSOR
		FOR 
		select [name]
		from dbo.sysobjects 
		where  OBJECTPROPERTY(id, N'IsUserTable') = 1
		FOR READ ONLY


CREATE TABLE #SizeTable
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)

--Open the cursor
OPEN dataCursor

--Get the first table name from the cursor
FETCH NEXT FROM dataCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
	BEGIN
	    
		INSERT  #SizeTable
			EXEC sp_spaceused @TableName	    
		FETCH NEXT FROM dataCursor INTO @TableName
	END
CLOSE dataCursor
DEALLOCATE dataCursor
SELECT * FROM #SizeTable
DROP TABLE #SizeTable

GO

In above procedure,i have compose the above the stored procedure using simple SQL.

Result

Image Loading

In above result, you able to see the all details in when you are execute the in sql server.

Sample Script

Download source files -1 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top