UrbanPro
true

Learn MS SQL Development from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

SQL Server Row Count for all Tables in a Database

Amitava Majumder
25/05/2017 0 0

Problem
I am a database consultant and one of my tasks involves getting the row counts from all the tables in the source database and comparing it against the corresponding table row counts in the target database. How do I get the row counts from all the tables in a SQL Server Database? What are the different approaches to get this information?

Solution
It is a common step in any ETL project to validate the row counts between source and target databases
as part of the testing phase. Getting the row count from each table one by one and comparing and consolidating the results can be a tedious task. Hence any script/solution which can get the row count information from all the tables in a database can be really helpful and effective thereby considerably reducing the effort involved. In this tip we will see four different approaches to get the row counts from all the tables in a SQL Server database

How to Count Rows of Every Table in Database
I try to implement some of the faster ways for counting row number rather than the COUNT() that I'm using right now.I am not able to get the result I'm expecting.

I have table Sales.[Customer] and I take the number of the records inside the table simply by:

SELECT COUNT(*) as Row_count FROM Sales.[Customer]
And the result is:

Row_count
19820

Now I'm trying to count the records from the same table getting use of the sys.partitions

SELECT SCHEMA_NAME(schema_id) AS [Schema_Name],
[Tables].name AS [Table_Name],
SUM([Partitions].[rows]) AS [Total_Rowcount]
FROM sys.tables AS [Tables]
JOIN sys.partitions AS [Partitions]
ON [Tables].[object_id] = [Partitions].[object_id]
AND [Partitions].index_id IN ( 0, 1 )
GROUP BY SCHEMA_NAME(schema_id), [Tables].name;

sys.partitions Contains a row for each partition of all the tables and most types of indexes in the database.Special index types such as Full-Text, Spatial, and XML are not included in this view. All tables and indexes in SQL Server contain at least one partition, whether or not they are explicitly partitioned.
index_id Indicates the ID of the index within the object to which this partition belongs.

  • 0 = heap
  • 1 = clustered index
  • 2 or greater = nonclustered index

For a single table use WHERE [Tables].name = N'name of the table'

Schema_Name Table_Name                  Total_Rowcount
Person               Address                       19614
Person               AddressType                 6
Production         BillOfMaterials               2679
Person               BusinessEntity              20777
Person               BusinessEntityAddress   19614
Person               BusinessEntityContact    909
Person               ContactType                  20
Person               CountryRegion               238
Sales                 CountryRegionCurrency  109
Sales                 Customer                     19820

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Data Structure with C
#include #include #include #include struct node { int data; struct node *next; }*start=NULL; void create(); void display(); void search(); void main() { int ch; clrscr(); while(1) { printf("choose......
S

Protocol: Preparation for Ultra competent cells of E.Coli DH5-alpha
Preparation: Prepare LB media and Transformation Buffer. Transformation Buffer 10 mM Hepes15 mM CaCl2250 mM KCl (pH to 6.7 with KOH)MIX Thoroughly with constant stirring and than add 55 mM MnCl2 and...

5G Technology & Features
What Is 5G? 5G Technology stands for 5th Generation Mobile technology. 5G mobile technology has changed the means to use cell phones within very high bandwidth. User never experienced ever before...

MS-SQL Database
1. Select: 01-Select All Given a City table, whose fields are described as: +-------------+----------+ | Field | Type | +-------------+----------+ | ID | int(11) | | Name ...

E-magazine
E-magazine is a magazine published on the World Wide Web. Some online magazines may refer to themselves as electronic magazines or e- magazine to reflect their readership, demographics or to capture alternative...

Looking for MS SQL Development Training?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for MS SQL Development Classes?

The best tutors for MS SQL Development Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn MS SQL Development with the Best Tutors

The best Tutors for MS SQL Development Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more