Counting rows for all tables at once

前端 未结 6 1978
梦毁少年i
梦毁少年i 2020-12-09 10:28

I\'m using SQL Server 2005 and would like to know how I can get a list of all tables with the number of records in each.

I know I can get a list of tables using the

6条回答
  •  伪装坚强ぢ
    2020-12-09 11:24

    For what it's worth, the sysindexes system table is deprecated in SQL 2008. The above still works, but here's query that works going forward with SQL 2008 system views.

    select
    schema_name(obj.schema_id) + '.' + obj.name,
    row_count
    from (
        select
            object_id,
            row_count = sum(row_count)
        from sys.dm_db_partition_stats
        where index_id < 2  -- heap or clustered index
        group by object_id
    ) Q
    join sys.tables obj on obj.object_id = Q.object_id
    

提交回复
热议问题