Navigation

Friday, May 14, 2010

How to get row counts for all tables in a database

I often need to clean out sample data from a database but need to leave static reference data.  I find this script very useful to ensure that I have removed table data that should not remain.

SELECT '[' + SCHEMA_NAME(t.schema_id) + '].[' + t.name + ']' AS fulltable_name, SCHEMA_NAME(t.schema_id) AS schema_name, t.name AS table_name,
i.rows
FROM sys.tables AS t INNER JOIN
sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2 order by rows desc

No comments: