Obtain Row count of all the tables in Sql Server

Niladri.Biswas
Posted by Niladri.Biswas under Sql Server category on | Points: 40 | Views : 1588
Hi,
The below code will help us for getting the count of rows of all tables in a particular Database. Thought it may be helpful for others

-- Create a temp table for storing the information
CREATE TABLE #temp
(
[Table Name] VARCHAR(100),
[Number Of Rows] INT
)
--Execute the query and insert into the temp table
EXEC sp_msForEachTable 'INSERT INTO #temp SELECT ''?'' AS TableName,COUNT(*) AS NoOfRows FROM ?'

-- Project the result
SELECT * FROM #temp ORDER BY [Number Of Rows] DESC

-- Drop the temp table once the operation is over
DROP TABLE #temp

Comments or Responses

Login to post response