This post is inspired from one of other post in SQL Server Geeks by Ahmad Osama.

I would like to extend some of my ideas further to get the table count as below. At times, SQL Server developers/DBAs might need to know the table row count for all tables from all databases available on a server. There are various approaches to get the row counts in SQL Server.

Here are few approaches as below:

Approach 1:

For Partition tables, the above query can be changed a bit as below:

Approach 2:

As above, there are still more ways to get the info in SQL Server. I do not really want to list of all the methods as a simple google search would end up with a lots of scripts for the same. Rather , want to talk about one of the major drawback with such approaches is it may or may not be accurate in value. If you look at the MSDN articles for each catalogs used in the above approaches, you can see that it clearly suggests based on the approximate values. So if we are looking for an accurate value for tables that are undergoing frequent DELETE/INSERT, then we should really rely on COUNT() function. As COUNT function reads the data, the row count would be more accurate than any other methods.

Here is my attempt to get the count using sp_MSforeachdb and sp_MSforeachtable. I tried to make the script as simple as possible. You may have a look at below:

Note: You should be very carful as its a very expensive query, you may use at your own risk.Both sp_MSforEachDB and sp_MSforEachtable are undocumented as well. 

Hope you enjoyed this post, please share your thoughts on the same.


Latheesh NK