﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Harsha Majety  / Script to find table sizes in a database / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 10:04:24 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>can somebody tell me, how can we list all the columns from a temp(#) table without using SP_Help or system objects.</description><pubDate>Mon, 17 Jan 2011 03:16:58 GMT</pubDate><dc:creator>kiran.konankal</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>[quote][b]gitmo (12/14/2010)[/b][hr]In SQL 2008 I right click the database name in Object Explorer, select Reports / Standard Report / Disk Usage by Table.[/quote]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.</description><pubDate>Tue, 14 Dec 2010 21:31:14 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>To see sizes of all tables in SQL 2005 use this:[code="sql"]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 cursorsINSERT INTO #AllTables            (table_name)SELECT s.[NAME] + '.' + t.[name]FROM   sys.Tables t, sys.schemas sWHERE  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 tablesWHILE ( @i &amp;lt;= @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 reultsSELECT *FROM   #TempTableORDER  BY data DESC;--Final cleanup!DROP TABLE #TempTableDROP TABLE #Alltables; [/code]</description><pubDate>Tue, 14 Dec 2010 18:20:26 GMT</pubDate><dc:creator>m.alves.amorim</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>In SQL 2008 I right click the database name in Object Explorer, select Reports / Standard Report / Disk Usage by Table.</description><pubDate>Tue, 14 Dec 2010 08:28:37 GMT</pubDate><dc:creator>gitmo</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>Don't forget to first use: [b]DBCC UPDATEUSAGE(0);[/b]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 [b]sp_spaceused[/b] system stored procedure.</description><pubDate>Tue, 14 Dec 2010 08:28:14 GMT</pubDate><dc:creator>peter-757102</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>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 );</description><pubDate>Tue, 14 Dec 2010 07:31:51 GMT</pubDate><dc:creator>william.long</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>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[code="sql"]ORDER  BY data DESC;[/code]with[code="sql"]ORDER BY CONVERT(INT, replace(data,' KB','')) desc;[/code]handy for databases that contain 1000+ tables</description><pubDate>Tue, 14 Dec 2010 05:21:43 GMT</pubDate><dc:creator>jordonpilling</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>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.</description><pubDate>Tue, 14 Dec 2010 02:27:20 GMT</pubDate><dc:creator>Andrew Watson-478275</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>[quote][b]Andrew Watson-478275 (12/14/2010)[/b][hr]If you don't mind using undocumented procedures, you can simplify it slightly by replacing all the loop stuff with:[code="sql"]exec sp_msforeachtable @command1='insert #TempTable exec sp_spaceused ''?'''[/code]I also convert the "KB" values from sp_spaceused into numbers to allow for easier manipulation.[/quote]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.</description><pubDate>Tue, 14 Dec 2010 02:21:05 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>If you don't mind using undocumented procedures, you can simplify it slightly by replacing all the loop stuff with:[code="sql"]exec sp_msforeachtable @command1='insert #TempTable exec sp_spaceused ''?'''[/code]I also convert the "KB" values from sp_spaceused into numbers to allow for easier manipulation.</description><pubDate>Tue, 14 Dec 2010 01:54:17 GMT</pubDate><dc:creator>Andrew Watson-478275</dc:creator></item><item><title>Script to find table sizes in a database</title><link>http://www.sqlservercentral.com/Forums/Topic1034178-2846-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Administration/71777/"&gt;Script to find table sizes in a database&lt;/A&gt;[/B]</description><pubDate>Mon, 13 Dec 2010 21:57:25 GMT</pubDate><dc:creator>harsha.majety</dc:creator></item></channel></rss>