Friday, March 13, 2015

Simple way to fetch the row count for all tables in a SQL SERVER database

This script uses some sys tables and the object_name function. It allows you to display the rowcount of all the tables in the active database. I've also included the schema name. Remove the 2 joins if you don't need it.

SELECT
    s.name SchemaName
    ,object_name(id) TableName
    ,Rows
FROM sysindexes i
JOIN sys.objects o ON o.object_id = i.id
JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE indid IN (0, 1)
ORDER BY rows DESC