Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database

Record Count of Tables in SQL Server

4.86/5 (22 votes)
16 Aug 2011CPOL 56.3K   1  
The below query can be used to get the record count of all tables in the current database.

The query below can be used to get the record count of all the tables in the current database.


Code


SQL
SELECT 
T.TABLE_NAME AS [TABLE NAME], MAX(I.ROWS) AS [RECORD COUNT]
FROM SYSINDEXES I, INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_NAME = OBJECT_NAME(I.ID)
      AND T.TABLE_TYPE = 'BASE TABLE'
GROUP BY T.TABLE_SCHEMA, T.TABLE_NAME

Result


Example output against Northwind database:



TABLE NAMERECORD COUNT
Categories8
CustomerCustomerDemo0
CustomerDemographics0
Customers91
Employees9
EmployeeTerritories49
Order Details2155
Orders830
Products97
Region4
Shippers3
Suppliers29
Territories53

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)