views:

446

answers:

3

I'm using SQL Server 2005 and would like to know how I can get a list of all tables with the number of records in each.

I know I can get a list of tables using the sys.tables view, but I'm unable to find the count.

Thank you

+2  A: 

Perhaps something like this:

SELECT 
    [TableName] = so.name, 
    [RowCount] = MAX(si.rows) 
FROM 
    sysobjects so, 
    sysindexes si 
WHERE 
    so.xtype = 'U' 
    AND 
    si.id = OBJECT_ID(so.name) 
GROUP BY 
    so.name 
ORDER BY 
    2 DESC

http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html

Paul Lefebvre
+5  A: 

From here: http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html

SELECT 
    [TableName] = so.name, 
    [RowCount] = MAX(si.rows) 
FROM 
    sysobjects so, 
    sysindexes si 
WHERE 
    so.xtype = 'U' 
    AND 
    si.id = OBJECT_ID(so.name) 
GROUP BY 
    so.name 
ORDER BY 
    2 DESC
brian
+3  A: 

I might add that sysindexes.rows is an approximation of the number of rows. I'd run a DBCC UPDATEUSAGE if you need a more accurate value. We had this issue on a DB with tables containing over 47-50 million rows and we thought we'd lost around half a million from each of them.

Kev