﻿<?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 ~~Hari.Bagra / Article Discussions by Author  / Get Record Count for each table 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>Sat, 25 May 2013 10:03:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]rbarryyoung (8/10/2008)[/b][hr]Hmm, yeah I see where it says that Jeff.  I may be wrong here, when I wrote my view (about 2-3 years ago) I was still working on both 2000 &amp; 2005 most days and I remember having accuracy problems that ultimately caused my to add DBCC UpdateUsage periodically.  But it [i]may[/i] have been only happening on SQL 2000.[/quote]Either way, it doesn't hurt to update usage as long as you have the time.  The important thing about any of these techniques (yours, mine and Jeff's) are that they are not cursor based and return the desired results.</description><pubDate>Sun, 10 Aug 2008 15:11:24 GMT</pubDate><dc:creator>Jeffrey Williams 3188</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]Jeffrey Williams (8/10/2008)[/b][hr]Well, according to BOL - no, you don't have to update usage.[/quote]Hmm, yeah I see where it says that Jeff.  I may be wrong here, when I wrote my view (about 2-3 years ago) I was still working on both 2000 &amp; 2005 most days and I remember having accuracy problems that ultimately caused my to add DBCC UpdateUsage periodically.  But it [i]may[/i] have been only happening on SQL 2000.</description><pubDate>Sun, 10 Aug 2008 14:13:09 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>... and, with SQL Server 2000, we end up with this...[code]   DBCC UPDATEUSAGE (0) SELECT so.Name AS TableName,        si.Rows AS [Rows]   FROM dbo.SysObjects so  INNER JOIN dbo.SysIndexes si     ON si.ID    = so.ID  WHERE si.IndID &amp;lt; 2    AND so.XType = 'U'    AND OBJECTPROPERTY(so.ID,'IsMSShipped') = 0[/code]Yeah... I know... if I didn't use the two part naming convention, they'd both work and they'd both be identical.  Actually, the code above will work in both environments... I just have an affinity for the two part naming convention for a lot of reasons. :P</description><pubDate>Sun, 10 Aug 2008 13:48:25 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>So... hedging a bet against BOL and changing the code to something a bit more conventional, we end up with this...[code]   DBCC UPDATEUSAGE (0) SELECT so.Name AS TableName,        si.Rows AS [Rows]   FROM sys.SysObjects so  INNER JOIN sys.SysIndexes si     ON si.ID    = so.ID  WHERE si.IndID &amp;lt; 2    AND so.XType = 'U'    AND OBJECTPROPERTY(so.ID,'IsMSShipped') = 0[/code]</description><pubDate>Sun, 10 Aug 2008 13:43:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>Since I went ahead and made the query which actually does provide the table and associated rowsize without cursors thought I would post it.     (though I am sure Barry's does a LOT more ).[code]SELECT so.name 'Table', si.rows 'Rows'     FROM          (SELECT Name FROM sysobjects WHERE type='U') so     JOIN          (SELECT rows,id,indid FROM sysindexes) si         ON si.id = OBJECT_ID(so.name) AND si.indid &amp;lt; 2[/code]Toni</description><pubDate>Sun, 10 Aug 2008 11:43:12 GMT</pubDate><dc:creator>toniupstny</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]Jeff Moden (8/10/2008)[/b][hr]Again, I ask... do you have to update usage for the rowcounts to be accurate?[/quote]Well, according to BOL - no, you don't have to update usage.[quote]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. In SQL Server 2005 and later, these values are always maintained correctly. Databases upgraded from SQL Server 2000 may contain invalid counts. We recommend running DBCC UPDATEUSAGE after upgrading to correct any invalid counts.[/quote]But, as we all know - BOL is not always correct.  There are situations where you will need to update usage, but I am not sure what those are.  We just went through a large purge operation and these numbers were updated accurately.</description><pubDate>Sun, 10 Aug 2008 11:40:24 GMT</pubDate><dc:creator>Jeffrey Williams 3188</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]Jeff Moden (8/10/2008)[/b][hr][quote][b]rbarryyoung (8/10/2008)[/b][hr]Gimme time to eat lunch, Jeff!  :P :)[/quote]Heh... hey!  I need to know... does brother Darth have to take off the Chevy look-alike to eat?[/quote]Straws.  And shakes.  Lots of shakes.  Milk shakes, oatmeal shakes, yam shakes, broccoli shakes, steak shakes, spinach shakes, etc. :sick:</description><pubDate>Sun, 10 Aug 2008 11:17:54 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]rbarryyoung (8/10/2008)[/b][hr]Gimme time to eat lunch, Jeff!  :P :)[/quote]Heh... hey!  I need to know... does brother Darth have to take off the Chevy look-alike to eat?</description><pubDate>Sun, 10 Aug 2008 11:07:39 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]Jeff Moden (8/10/2008)[/b][hr]Again, I ask... do you have to update usage for the rowcounts to be accurate?[/quote]Gimme time to eat lunch, Jeff!  :PYes, you do.   But unless it is a table with rapidly shifting contents, you shouldn't have to do it every time.  Besides, I couldn't put it in my view anyway.  :)</description><pubDate>Sun, 10 Aug 2008 11:03:30 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]Jeffrey Williams (8/10/2008)[/b][hr]For SQL Server 2005 and greater - there is no need to cursor at all.  Just use the DMV sys.dm_db_partition_stats as in the following:[code] Select object_schema_name(object_id) As SchemaName        ,object_name(object_id) As ObjectName        ,row_count   From sys.dm_db_partition_stats  Where index_id &amp;lt; 2    And object_schema_name(object_id) &amp;lt;&amp;gt; 'sys'  Order By object_schema_name(object_id);[/code][/quote]Again, I ask... do you have to update usage for the rowcounts to be accurate?</description><pubDate>Sun, 10 Aug 2008 10:56:18 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>For SQL Server 2005 and greater - there is no need to cursor at all.  Just use the DMV sys.dm_db_partition_stats as in the following:[code] Select object_schema_name(object_id) As SchemaName        ,object_name(object_id) As ObjectName        ,row_count   From sys.dm_db_partition_stats  Where index_id &amp;lt; 2    And object_schema_name(object_id) &amp;lt;&amp;gt; 'sys'  Order By object_schema_name(object_id);[/code]</description><pubDate>Sun, 10 Aug 2008 10:43:58 GMT</pubDate><dc:creator>Jeffrey Williams 3188</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>Aye... that's more like it.The only thing I'd be worried about is... do you need to update usage (DBCC UPDATEUSAGE) as you do in SQL Server 2000 to get accurate row counts?</description><pubDate>Sun, 10 Aug 2008 10:32:51 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>Now [i]this view[/i] can get the estimated row counts (and a lot more besides)without cursors: [url]http://www.sqlservercentral.com/scripts/tables/62545/[/url]</description><pubDate>Sun, 10 Aug 2008 10:27:25 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>Thanks Jeff. I was just looking through the code for the sp_MSforeach procedures (generated a Create for sp_MSforeachtable, sp_MSforeachworker and the sp_ MSforeach_worker) to see why the table one couldn't be called from the database one.  I saw the cursor like you said right there so was coming back to post a "NEVERMIND"!   Hey...  I learned something anyway...   thanks again.Toni</description><pubDate>Sun, 10 Aug 2008 10:18:07 GMT</pubDate><dc:creator>toniupstny</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>[quote][b]toniupstny (8/10/2008)[/b][hr]An alternative without cursor is:[center]sp_msforeachtable 'select ''?'', count(*) Rows from ?'[/center]Toni[/quote]Ummm.... not quite correct, Toni.  If you go and look at the code for sp_MSForEachTable, you'll find a monster cursor that is typically slower than any cursor you would ever intentionally build because it was written to handle just about any size code.  The cursor that Hari wrote will likely run much faster than using sp_MSForEachTable.</description><pubDate>Sun, 10 Aug 2008 09:37:32 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>An alternative without cursor is:[center]sp_msforeachtable 'select ''?'', count(*) Rows from ?'[/center]Toni</description><pubDate>Sun, 10 Aug 2008 08:37:46 GMT</pubDate><dc:creator>toniupstny</dc:creator></item><item><title>Get Record Count for each table in a database.</title><link>http://www.sqlservercentral.com/Forums/Topic403906-555-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Miscellaneous/31987/"&gt;Get Record Count for each table in a database.&lt;/A&gt;[/B]</description><pubDate>Fri, 28 Sep 2007 01:07:16 GMT</pubDate><dc:creator>Hari.Sharma</dc:creator></item></channel></rss>