Skip to main content

SQL Server query to get a count or all rows for each table in a database.

-- use <database_name>;

select schm.name + '.' + objt.name as TableName,
    SUM(row_count) as [TotalRows]
from sys.dm_db_partition_stats as stat
join sys.objects as objt
    on stat.object_id = objt.object_id
join sys.schemas as schm
    on objt.schema_id = schm.schema_id
where stat.index_id < 2
    and schm.name <> 'sys'
group by schm.name + '.' + objt.name;