Code Snippet SQL – Get total number of records count for all the tables

Code Snippet SQL – Get total number of records count for all the tables
7 votes, 5.00 avg. rating (98% score)

Introduction

SQL Code snippet how to return total number of records count for each table in a database

Get all tables and count of records in each table




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

DB Tables Count

DB Tables Count

February 21, 2014 В· Adi В· No Comments
Posted in: Code Snippet, SQL

Leave a Reply

What is 12 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)