Tuesday, July 26, 2016

Cool way to get row counts for all tables in SQL Server DB

...Or other repetitive actions on all tables

CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC
DROP TABLE #counts


--Al-

No comments:

Post a Comment