且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取 SQL SERVER 数据库中所有表的行数

更新时间:2023-01-31 13:30:03

以下 SQL 将获取数据库中所有表的行数:

The following SQL will get you the row count of all tables in a database:

CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC
DROP TABLE #counts

输出将是一个表格列表及其行数.

The output will be a list of tables and their row counts.

如果您只想要整个数据库的总行数,请附加:

If you just want the total row count across the whole database, appending:

SELECT SUM(row_count) AS total_row_count FROM #counts

将为您提供整个数据库中总行数的单个值.

will get you a single value for the total number of rows in the whole database.