TSQL - How to Get Row Count Of All The Tables In SQL Server Database

Sometime we want to get the Row Count quickly for all the tables in our Database for analysis. There are multiple ways to do that. We can use TSQL Cursor to loop through all the tables and use count(*) to get the row count.

I am using Catalog views to get this information by using below query. This is quick way to find the row count for all the tables in a database. The Cursor with Count(*) can be slow as it has to count rows for each of the table.


USE YourDBName
GO
SELECT OBJECT_NAME(id) AS TableName,
       rowcnt          AS [RowCount]
FROM   sysindexes s
       INNER JOIN sys.tables t
               ON s.id = t.OBJECT_ID
WHERE  s.indid IN ( 0, 1, 255 )
       AND is_ms_shipped = 0

I ran above query on TestDB and got below information.

Fig 1: Get Row Count for all the Tables in SQL Server Database


Check out our other posts on Data Analysis / Data Validation / Data Cleansing

1 comment:

  1. While working on a SQL project to calculate the percentage of empty or null values across multiple columns, I found a helpful tutorial that streamlined my data analysis process. This efficiency in handling data led me to consider optimizing other aspects of my work, such as streaming services. I explored SoPlayer as a potential platform for flexible viewing options. To understand user experiences, I consulted https://soplayer.pissedconsumer.com/review.html , feedback highlighted the platform's variety of channels and user-friendly interface, which aligned with my entertainment needs. Embracing such solutions complements the ethos of enhancing efficiency and enjoyment in various facets of life.​

    ReplyDelete