How to get row count for every table in database

31 January,2017 by Jack Vamvas

Question: I need some sql code to record the row count of every table in a database.

I only need to run this code once , but it’s on a database with hundreds of tables

Answer: There are numerous ways of getting the row count for every table in a database.

If I require a quick method , just to simply record every table row count , I’ll use some code used in this example.

This code utilises the sp_msforeachtable stored procedure . As the input parameter, I include an INSERT INTO .. construct.

The sp_msforeachtable stored procedure iterates through every table – and records the count.

If you want some code with more detail - read more on SQL Server – Get sql table size and last updated for all tables

 

 

CREATE TABLE #TblRowCounts(TableName VARCHAR(128),NumberOfRows BIGINT)

use mydatabase
go
sp_msforeachtable 'INSERT INTO #TblRowCounts select ''?'' as ''Table'', count(*) as ''Rows'' from ? '

select * from #TblRowCounts
GO
DROP TABLE #TblRowCounts




 

Read More

Calculate sql row size

SQL Server - How to find the largest sql index and table size


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to get row count for every table in database


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer