SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script to find table sizes in a database


Script to find table sizes in a database

Author
Message
harsha.majety
harsha.majety
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 448
Comments posted to this topic are about the item Script to find table sizes in a database
Andrew Watson-478275
Andrew Watson-478275
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1915 Visits: 2719
If you don't mind using undocumented procedures, you can simplify it slightly by replacing all the loop stuff with:


exec sp_msforeachtable @command1='insert #TempTable exec sp_spaceused ''?'''



I also convert the "KB" values from sp_spaceused into numbers to allow for easier manipulation.
Hardy21
Hardy21
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1862 Visits: 1399
Andrew Watson-478275 (12/14/2010)
If you don't mind using undocumented procedures, you can simplify it slightly by replacing all the loop stuff with:


exec sp_msforeachtable @command1='insert #TempTable exec sp_spaceused ''?'''



I also convert the "KB" values from sp_spaceused into numbers to allow for easier manipulation.


Though sp_msforeachtable procedure is undocumented, but it is really useful if we want to apply the changes to all the tables in the database - by using this we can avoid Loop or cursors. We are using this to enable/disable all the constraints at one go.

Thanks
Andrew Watson-478275
Andrew Watson-478275
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1915 Visits: 2719
You don't really avoid using cursors or loops - if you have a look inside sp_msforeachtable and sp_msforeach_worker, you'll see it uses them itself.

For me, the main reason for using it is that it's easier.
jordonpilling
jordonpilling
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 451
Great Script, Just a small tweak if i may;

The 'Order By' clause you use on line 45 does not really work as the data column is a nvarchar and contains " KB".

replace

ORDER  BY data DESC;



with

ORDER BY CONVERT(INT, replace(data,' KB','')) desc;



handy for databases that contain 1000+ tables

Knock Knock, Who's There?, sp underscore, sp underscore who?
spid1, spid2, spid3....
william.long
william.long
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 14
Here are some tweaks I did on my SQL Server 2005 to get it to work for me...

--DECLARE @i INT = 1;
DECLARE @i INT ;
SET @i = 1;
--DECLARE @tableCount INT = (SELECT COUNT(1) FROM #AllTables );
DECLARE @tableCount INT
SET @tableCount = (SELECT COUNT(1) FROM #AllTables );
peter-757102
peter-757102
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1069 Visits: 2559
Don't forget to first use: DBCC UPDATEUSAGE(0);

BOL says about this:

Reports and corrects pages and row count inaccuracies in the catalog views. These inaccuracies may cause incorrect space usage reports returned by the sp_spaceused system stored procedure.
gitmo
gitmo
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 187
In SQL 2008 I right click the database name in Object Explorer, select Reports / Standard Report / Disk Usage by Table.
m.alves.amorim
m.alves.amorim
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 2
To see sizes of all tables in SQL 2005 use this:

DECLARE @tableName VARCHAR(1000); 
CREATE TABLE #AllTables
(
row_num INT IDENTITY(1, 1),
table_name VARCHAR(1000)
);

--Using temp table, i dont like to use cursors
INSERT INTO #AllTables
(table_name)
SELECT s.[NAME] + '.' + t.[name]
FROM sys.Tables t, sys.schemas s
WHERE t.[SCHEMA_ID] = s.[SCHEMA_ID]

CREATE TABLE #TempTable
(
tableName VARCHAR(100),
[rows] VARCHAR(100),
reserved VARCHAR(50),
data VARCHAR(50),
index_size VARCHAR(50),
unused VARCHAR(50)
)

DECLARE @i INT;
DECLARE @tableCount INT;

SELECT @i = 1;
SELECT @tableCount = (SELECT COUNT(1) FROM #AllTables );

--Loop to get all tables
WHILE ( @i <= @tableCount )
BEGIN
SELECT @tableName = table_name
FROM #AllTables
WHERE row_num = @i;

--Dump the results of the sp_spaceused query to the temp table
INSERT #TempTable
EXEC sp_spaceused @tableName;

SET @i = @i + 1;
END;

--Select all records so we can use the reults
SELECT *
FROM #TempTable
ORDER BY data DESC;

--Final cleanup!
DROP TABLE #TempTable

DROP TABLE #Alltables;



Hardy21
Hardy21
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1862 Visits: 1399
gitmo (12/14/2010)
In SQL 2008 I right click the database name in Object Explorer, select Reports / Standard Report / Disk Usage by Table.

Oh great... Thanks. I don't know about this - it is really useful. You can also check no of records for all the tables in a single go.

Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search