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
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
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
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
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.