Recently, I was working on a database performance improvement project. One stored procedure there was causing issues. In its code, a query populated the Count of the rows and stored the value in a local variable. That query was scanning a large table. Due to that, resource utilization became significantly higher. To fix the issue, we removed the faulty code and used the SQL Server catalog views to generate the Count of rows of the table.

There are various ways to count the number of rows in a table of SQL Server. This article will describe them for you to always choose the right way to do it.

We can get the Count of rows of the table with any of the following methods:

  1. Use COUNT() function.
  2. Combining SQL Server catalog views.
  3. Using sp_spaceused stored procedure.
  4. Using SQL Server Management studio.

Let us dig deeper.

#sql server #sql functions #sql

How to Count the Number of Rows in SQL Server Table
1.20 GEEK