﻿<?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 / Discuss content posted by Mohit Nayyar / Article Discussions by Author  / Table row count using 3 different ways / 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>Sat, 25 May 2013 10:38:42 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>ron.mcdowell that is an awesome script. My initial testing shows it is significantly faster than my script and for all my tests it was accurate. Now I need to learn how/why it works :-)-David</description><pubDate>Thu, 16 Jun 2011 08:30:33 GMT</pubDate><dc:creator>dchappell-903283</dc:creator></item><item><title>RE: Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>Nice to see I started a healthy debate!  I'm going to bear this stuff in mind - I'm always looking for smarter-running code!</description><pubDate>Thu, 16 Jun 2011 08:17:04 GMT</pubDate><dc:creator>ianhenderson</dc:creator></item><item><title>RE: Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>Does this seemingly simpler, and far faster, method produce accurate results?[code="sql"]SELECT name [Table], sum(row_count) AS [Rows] FROM sys.dm_db_partition_stats  WITH (NOLOCK), sysobjects  WITH (NOLOCK)WHERE index_id &amp;lt; 2	-- Just grab index 0 (heap) or 1 (cluster)  AND xtype = 'U'  AND object_id = object_id('' + name + '')GROUP BY name[/code]</description><pubDate>Thu, 16 Jun 2011 08:13:19 GMT</pubDate><dc:creator>ron.mcdowell</dc:creator></item><item><title>RE: Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>To expand on ianhenderson's way here is the loop without worrying about the lowest id and just grabbing the next one that doesn't have a count.[code="sql"]DECLARE @TableName VARCHAR(255);SELECT TABLE_NAME AS TableName, null AS RowCntINTO #TempTableCountsFROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'base table';WHILE (EXISTS(SELECT * FROM #TempTableCounts WHERE (RowCnt IS NULL))) BEGIN	SELECT TOP(1) @TableName = TableName FROM #tempTableCounts WHERE (RowCnt IS NULL);	EXEC ('UPDATE #TempTableCounts SET RowCnt = (SELECT count(*) FROM ' + @TableName + ') WHERE (TableName = ''' + @TableName + ''')');ENDSELECT TableName, RowCnt FROM #TempTableCounts;DROP TABLE #TempTableCounts;[/code]-David</description><pubDate>Thu, 16 Jun 2011 06:47:35 GMT</pubDate><dc:creator>dchappell-903283</dc:creator></item><item><title>RE: Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>I've always found cursors a really slow way of doing pretty much anything in SQL, to say nothing of being a horror to code if it's big enough.There could be a fourth way.  Create a temporary table containing the name and object_id values of every user table in the database.  Then create a loop to do the following:a.  Select the name of the table object with the lowest object_id value;b.  Execute a string of dynamic SQL to count the number of rows in the table bearing the name that you retrieved in step 1;c.  Delete the record in the temporary table that you've just been looking at;d.  Repeat until the temporary table is empty;You could refine it further by writing the result of the count into a new table (temporary or otherwise).  Or even by adding an additional column to the original temporary table and then updating that each time.  The only difference is that you wouldn't delete the record once you'd finished with it, merely select the minimum object_id value as long as it was higher than the object_id value you'd just finished with.Just a thought :-)</description><pubDate>Thu, 16 Jun 2011 03:10:59 GMT</pubDate><dc:creator>ianhenderson</dc:creator></item><item><title>Table row count using 3 different ways</title><link>http://www.sqlservercentral.com/Forums/Topic402789-613-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Miscellaneous/31984/"&gt;Table row count using 3 different ways&lt;/A&gt;[/B]</description><pubDate>Tue, 25 Sep 2007 18:44:25 GMT</pubDate><dc:creator>Mohit Nayyar</dc:creator></item></channel></rss>