Ad Code

How to get Table Names with Record Count in SQL Server Database

How to get Table Names with Record Count in SQL Server Database

Consider the following scenarios: Our company is working on an audit process and wants to record table names and record counts in the audit table at various times throughout the day; alternatively, we are planning for storage and want to know which table has the most records so that we can plan the storage accordingly.

Using the record count, the following query will return all of the tables from the SQL Server database. To acquire our record count and table names, we'll use the System tables sysindexes and sys.tables.

 

SELECT DISTINCT t.name AS TableName,

                i.rows AS RecordCnt 

FROM   sysindexes i

       INNER JOIN sys.tables t

               ON i.id = t.OBJECT_ID 

WHERE  t.is_ms_shipped = 0  

ORDER BY t.name

 

 

 

Post a Comment

0 Comments

Close Menu