Count all rows in all tables in a database

This script comes in very handy when you need to analyse database tables. Simply change the use statement to point to your database.

USE [master]
GO
SELECT obj.name AS TableName, prt.rows AS TotalRows
FROM sys.objects obj
JOIN sys.indexes idx on obj.object_id = idx.object_id
JOIN sys.partitions prt on obj.object_id = prt.object_id
WHERE obj.type = ‘U’ AND idx.index_id IN (0, 1)
GROUP BY obj.name, prt.rows
ORDER BY TableName

You should get a resultset like this (I have cut my recordset down as it’s just an example)

TableName TotalRows
Asset 1142
AssetType 16
Company 424
CompanyVersion 0

Comments are closed.