ptrn
ptrn

Reputation: 5202

Count number of entries in whole SQL Server database

Is there any way I can count the number of all entries in a SQL Server database? Preferably with only one query.

Edit: The total amount of entries in all tables in a given database.

Upvotes: 1

Views: 248

Answers (2)

Daniel Vassallo
Daniel Vassallo

Reputation: 344521

This query will return a list of all the tables, with an approximate row count for each table:

SELECT 
    [TableName] = sysobjects.name, 
    [RowCount] = MAX(sysindexes.rows) 
FROM 
    sysobjects, 
    sysindexes 
WHERE 
    (sysobjects.xtype = 'U') AND (sysindexes.id = OBJECT_ID(sysobjects.name))
GROUP BY 
    sysobjects.name 
ORDER BY 
    2 DESC;

Upvotes: 3

Remus Rusanu
Remus Rusanu

Reputation: 294427

select sum(rows) from sys.partitions;

This is a correct answer, for a conveniently definition of 'entry' (chosen by me): a row in a heap or a b-tree.

Upvotes: 3

Related Questions