How to count all rows in a particular database in SQL Server?

There are many ways to find out of this result, here we mentioned 2 best methods among them.

  1. First of all you run this query for fetching all table names and using these table names you can make a query for fetching all rows count of a particular database.

    use [<Database_Name>]
    select 'selct @cnt = @cnt + count(*) from '+ name from sysobjects where xtype='u'

    by using this you can generate some queries for fetching rows count of every table, copy its result and paste on query editor window and then you can use a simple trick to add all table’s rows count like this…

    declare @cnt int
    set @cnt = 0
    select @cnt = @cnt + count(*) from tbl1
    select @cnt = @cnt + count(*) from tlb2
    select @cnt = @cnt + count(*) from tlb3
    select @cnt = @cnt + count(*) from tlb4
    select @cnt = @cnt + count(*) from tlb5
    select @cnt = @cnt + count(*) from tlb6
    select @cnt = @cnt + count(*) from tlb7
    print @cnt

    by using this method you will find all database’s tables row count.

  2. This method is commented on my blog by Astha Mundra my friend, and i like her concept to find out rows count. and i.e.

    use [<Database_Name>]
    select sum(si.rows) from sysindexes si inner join sysobjects so on si.id = so.id where si.indid < 2 and so.xtype = 'U'

    It will give you all database tables rows count.

Tagged . Bookmark the permalink.

Leave a Reply