统计数据库中所有表的总行数 SQL 服务器

Count total rows of all tables in a database SQL Server

我想计算 SQL 服务器数据库所有表的总行数,有人知道我该怎么做查询吗?

我将在作业中使用此信息在数据湖的各层之间传输信息。

使用下面的查询来计算数据库所有表的所有行数。

SELECT SUM(i.rowcnt)
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2
    AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0

你可以看看下面的文章;

Different approaches of counting number of rows in a table

这是我最喜欢的一个;

 SELECT SCHEMA_NAME(t.[schema_id]) AS [table_schema]
          ,OBJECT_NAME(p.[object_id]) AS [table_name]
          ,SUM(p.[rows]) AS [row_count]
    FROM [sys].[partitions] p
    INNER JOIN [sys].[tables] t ON p.[object_id] = t.[object_id]
    WHERE p.[index_id] < 2
    GROUP BY p.[object_id]
        ,t.[schema_id]
    ORDER BY 1, 2 ASC

这个找出SQL数据库的总数

SELECT 
      SUM(p.[rows]) AS [row_count]
FROM [sys].[partitions] p
INNER JOIN [sys].[tables] t ON p.[object_id] = t.[object_id]
WHERE p.[index_id] < 2