Solved

SQL 2005 Table Size

Posted on 2011-02-12
4
270 Views
Last Modified: 2012-05-11
Hi,

  I need a query to collect the top 20 tables size for mty SQL 2005 database.

Thanks
0
Comment
Question by:ITMaster1979
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 83 total points
ID: 34881508
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 83 total points
ID: 34881629
hi
this should help you.

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

--

CREATE PROCEDURE #TableSpaceUsed
AS

-- Create the temporary table...
CREATE TABLE #tblResults
(
   [name]   nvarchar(255),
   [rows]   int,
   [reserved]   varchar(255),
   [reserved_int]   int default(0),
   [data]   varchar(255),
   [data_int]   int default(0),
   [index_size]   varchar(255),
   [index_size_int]   int default(0),
   [unused]   varchar(255),
   [unused_int]   int default(0)
)


-- Populate the temp table...
EXEC sp_MSforeachtable @command1=
         "INSERT INTO #tblResults
           ([name],[rows],[reserved],[data],[index_size],[unused])
          EXEC sp_spaceused '?'"
   
-- Strip out the " KB" portion from the fields
UPDATE #tblResults SET
   [reserved_int] = CAST(SUBSTRING([reserved], 1,
                             CHARINDEX(' ', [reserved])) AS int),
   [data_int] = CAST(SUBSTRING([data], 1,
                             CHARINDEX(' ', [data])) AS int),
   [index_size_int] = CAST(SUBSTRING([index_size], 1,
                             CHARINDEX(' ', [index_size])) AS int),
   [unused_int] = CAST(SUBSTRING([unused], 1,
                             CHARINDEX(' ', [unused])) AS int)
   
-- Return the results...
SELECT * FROM #tblResults order by reserved_int desc


-- drop procedure TableSpaceUsed
-- exec TableSpaceUsed
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 84 total points
ID: 34883692
Why re-invent the wheel: There is a standard report in SSMS that already gives you that information:
1. Object Explorer.
2. Right-click the database.
3. Reports
4. Standard Reports
5. Disk Usage by Top Tables.
0
 
LVL 1

Author Closing Comment

by:ITMaster1979
ID: 35020282
good solution
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL: Do I need CLUSTERED here? 13 46
Query Help - MSSQL - Averages 5 27
T-SQL:  Collapsing 9 25
Weighted Randomizing 6 16
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question