How to retrieve the number of rows in each table?

Found a nice way to retrieve a list of tables in a certain database with their actual number of rows (instead of just looking at the meta-data that’s stored in indexes).
Source: http://stackoverflow.com/questions/1443704/query-to-list-number-of-records-in-each-table-in-a-database

Replaced the #Temp-table with a @Table-var, but it’s pretty straight-forward:

DECLARE @TableCounts TABLE
(
TableName VARCHAR(500),
NrOfRows INT
)

INSERT @TableCounts
EXEC sp_msForEachTable
'SELECT PARSENAME(''?'', 1),
COUNT(*) FROM ? WITH (NOLOCK)'

SELECT TableName, NrOfRows
FROM @TableCounts
ORDER BY TableName








Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s