How to get the table row count for all tables in a SQL Server database (T-SQL)

Introduction

This T-SQL script will demonstrate how to get the table row count for all tables in a SQL Server database.  

Scenarios

As many people asked how to get the table row count for all tables in a SQL Server database, we provide this script for reference. Getting the row count is based on a condition: index_id < 2 (0: heap, 1: clustered index).

 

Script

You can use this script in this way:
1. Open SQL Server Management Studio (SSMS) and connect to SQL Server.
2. Copy the code from GetTableRowsCount.sql, paste it in a new query and run the script.

After the script finishes running, we’ll get the following figure:

Here are some code snippet for your reference: 

SQL
Edit|Remove
SELECTsc.name +'.'ta.nameASTableName 
 ,SUM(pa.rowsASCountRowsFROMsys.tablestaINNERJOINsys.partitionspaONpa.OBJECT_ID = ta.OBJECT_IDINNERJOINsys.schemasscONta.schema_id = sc.schema_idWHEREta.is_ms_shipped = 0ANDpa.index_id <2GROUPBYsc.name,ta.name

Prerequisites

SQL Server 2005 or higher version