By using the following query you will be able to get all the tables with at least one record in the selected database. It will be handy when you needed to get an idea of the number of records in a database. This uses two SQL Server system views, namely SysObjects and SysIndexes.
- SELECT sysobjects.name AS [Table Name],
- MAX(sysindexes.rows) AS [Number of Records],
- sysobjects.crdate AS [Created Date],
- sysobjects.refdate AS [Referenced Date]
- FROM sysobjects
- INNER JOIN sysindexes ON sysobjects.id = sysindexes.id
- WHERE sysobjects.xtype = 'U' -- Filtering all the User Tables.
- AND sysindexes.rows > 0 -- Getting all the tables having at least one reoord.
- GROUP BY sysobjects.name,
- sysobjects.crdate,
- sysobjects.refdate
- ORDER BY 2 DESC -- Ordering by the number of records in table.
Above query will bring the following results on the Northwind database.