如何获取 SQL SERVER 数据库中所有表的行数 [英] How to fetch the row count for all tables in a SQL SERVER database

查看:127
本文介绍了如何获取 SQL SERVER 数据库中所有表的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索可用于确定给定数据库的任何表中是否有任何数据(即行数)的 SQL 脚本.

I am searching for a SQL Script that can be used to determine if there is any data (i.e. row count) in any of the tables of a given database.

这个想法是重新化身数据库,以防万一(在任何数据库中)存在任何行.

The idea is to re-incarnate the database in case there are any rows existing (in any of the database).

所说的数据库是Microsoft SQL SERVER.

有人可以推荐一个示例脚本吗?

Could someone suggest a sample script?

推荐答案

以下 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.

这篇关于如何获取 SQL SERVER 数据库中所有表的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆