posted on Wednesday, December 21, 2005 11:59 AM by bknight

Quickly Retrieving the Row Count for All Tables

My typical query I run for retrieiving a list of tables and how many records in each table there are is to use the sp_msforeach stored procedure. The sproc loops through each table in a database and performs an action. Given that, you can run a query like this to obtain a list of tables and how many records are in each table:

sp_msforeachtable "Print '?' select count(*) from ?"

The problem for me with this above query is that it's not sortable without some additional customization. So, I needed a different answer. The sysindexes table holds some very valuable information for a developer or DBA. The table shows you a record for each index in your database as well as the number of rows that are estimated to be in the table. It's not guaranteed to be up to date as far as the number of rows but it's a good number to start with. To model a query at that table, you can use the following syntax:

select convert(varchar(30),object_name(id)) [Table Name], rows from sysindexes
where object_name(id) not like 'sys%' and indid = 1
order by object_name(id)

Instantly you retreive in a single recordset the number of estimated records in each table. The key thing to keep in mind with the above query is I'm weeding out any table that began with sys. If you have a user table that begins with sys, it will be excluded. The indid=1 part of the syntax retrieves only the primary index. There are flaws to this method because it's not exact but it does give you a rought idea of your system information.

-- Brian Knight

Comments

# re: Quickly Retrieving the Row Count for All Tables

Thursday, December 22, 2005 7:11 AM by EugeneZ
Just do not forget to say about
DBCC UPDATEUSAGE

# re: Quickly Retrieving the Row Count for All Tables

Thursday, December 22, 2005 7:49 AM by bknight
Good point EugeneZ. Sometimes when Enterprise Manager appears to have out of date sizes or row counts. DBCC UPDATEUSAGE reports and corrects inaccuracies in the sysindexes table with syntax like this:

DBCC UPDATEUSAGE (AdventureWorks)

Thanks for bringing this up.

-- Brian Knight

# re: Quickly Retrieving the Row Count for All Tables

Thursday, December 22, 2005 3:13 PM by Robert Varga
Be careful in 2000 that you select sysindexes.rowcnt rather then rowcnt when oy have a table with over 2,147,483,647 rows.

Cheers,
Rob

# re: Quickly Retrieving the Row Count for All Tables

Monday, July 10, 2006 10:17 AM by Hyung
Hi,
I'm not sure I can post a question here but I have one that related to this row-counting.
I've been used the following command in my stored procedure to get the row count:
use <db_name>
DBCC UPDATEUSAGE ('<db_name>', '<table_name>')
set @Count = (SELECT rows FROM sysindexes
WHERE id = OBJECT_ID ('<table_name>') AND idid<2)
print CAST(@Count as varchar(20))
...

It works for the most tables but one table's row count is incorrect with the result of "select count(*) from <table_name>"
I tried "DBCC UPDATEUSAGE(0)" command & also tried "sp_spaceused" with @updateusage = 'true' option but the result is the same.
Is there any table setting that may prevent the system info is updated?
I can just use "count(*)" because the script is scheduled on the backup server at off-time.
But I want to know the reason.
I browsed Internet but I coudn't get the proper answer for this wierd problem.
Please let me know if you have any idea.
Thank you,
- Hyung -

# re: Quickly Retrieving the Row Count for All Tables

Thursday, April 26, 2007 2:24 AM by krishna
let me know whether this will work for sybase

# How to List All Tables and Their Rowcount in SQL Server

Saturday, September 22, 2007 10:10 AM by TakeNote...
How to List All Tables and Their Rowcount in SQL Server

# re: Quickly Retrieving the Row Count for All Tables

Tuesday, March 18, 2008 7:17 AM by zxevil163
B1YRco Hi from Russia!