Published on

October 15, 2001

How to Quickly Count All the Rows in All the Tables in a SQL Server Database

Counting the number of rows in a SQL Server database can be a time-consuming task, especially if you have a large number of tables. However, there is a simple and efficient way to accomplish this using the system table ‘sysindexes’.

The ‘rows’ column in the ‘sysindexes’ table holds the number of committed rows in each table. By querying this column, you can quickly get the row count for all the tables in the database.

Here is an example code snippet that demonstrates how to count all the rows in all the tables:


SELECT OBJECT_NAME(id) AS TableName, rows AS RowCount
FROM sysindexes
WHERE indid < 2
ORDER BY RowCount DESC

This query retrieves the table name and the corresponding row count for each table in the database. The ‘indid < 2’ clause restricts the query to the clustered index or heap info, ignoring subsequent indices.

It’s important to note that the ‘rows’ column only holds the results of committed transactions. Uncommitted transactions are not included in the row count.

By using this method, you can quickly get an overview of the number of rows in each table in your SQL Server database, which can be useful for performance analysis and optimization.

Remember to regularly update your row count statistics to ensure accurate results.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.