如何获取 SQL SERVER 数据库中所有表的行数
我正在寻找一个可用于确定给定数据库的任何表中是否有任何数据(即行数)的 SQL 脚本。
这个想法是在存在任何行(在任何数据库中)的情况下重新体现数据库。
这里所说的数据库是Microsoft SQL Sserver。
有人可以建议一个示例脚本吗?
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).
The database being spoken of is Microsoft SQL Sserver.
Could someone suggest a sample script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
以下 SQL 将获取数据库中所有表的行数:
输出将是表及其行数的列表。
如果您只需要整个数据库的总行数,则附加:
将为您提供整个数据库中的总行数的单个值。
The following SQL will get you the row count of all tables in a database:
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:
will get you a single value for the total number of rows in the whole database.
如果您想节省计算 (*) 300 万行表所需的时间和资源。请尝试 Kendal Van Dyke 的 SQL SERVER Central。
使用 sysindexes 进行行计数
如果您使用的是 SQL 2000,则需要像这样使用 sysindexes:
如果您使用的是 SQL 2005 或 2008,查询 sysindexes 仍然有效,但 Microsoft 建议在 SQL Server 的未来版本中可能会删除 sysindexes,以便作为一个好的选择。实践中你应该使用 DMV,如下所示:
If you want to by pass the time and resources it takes to count(*) your 3million row tables. Try this per SQL SERVER Central by Kendal Van Dyke.
Row Counts Using sysindexes
If you're using SQL 2000 you'll need to use sysindexes like so:
If you're using SQL 2005 or 2008 querying sysindexes will still work but Microsoft advises that sysindexes may be removed in a future version of SQL Server so as a good practice you should use the DMVs instead, like so:
在 Azure 上工作,不需要存储过程。
信用。
Works on Azure, doesn't require stored procs.
Credit.
这个看起来比我认为的其他更好。
This one looks better than the others I think.
简短而甜蜜的
输出:
更新:
如果数据库非常大,此查询可能会占用大量资源。 (根据评论)
Short and sweet
Output:
Update:
This query may be resource-intensive if the database is very large. (As per comment)
SQL Server 2005 或更高版本提供了一个相当不错的报告,显示表大小 - 包括行数等。它位于标准报告中 - 它是按表列出的光盘使用情况。
从编程角度来说,有一个很好的解决方案:
http://www.sqlservercentral.com/articles/T-SQL/67624/
SQL Server 2005 or later gives quite a nice report showing table sizes - including row counts etc. It's in Standard Reports - and it is Disc Usage by Table.
Programmatically, there's a nice solution at:
http://www.sqlservercentral.com/articles/T-SQL/67624/
不要使用
SELECT COUNT(*) FROM TABLENAME
,因为这是一个资源密集型操作。应使用 SQL Server 动态管理视图 或 系统目录获取某个表中所有表的行计数信息数据库。Don't use
SELECT COUNT(*) FROM TABLENAME
, since that is a resource intensive operation. One should use SQL Server Dynamic Management Views or System Catalogs to get the row count information for all tables in a database.这里有一个动态 SQL 方法,它还为您提供了架构:
如果需要,可以轻松地将其扩展为在实例中的所有数据库上运行(连接到 sys.databases )。
Here's a dynamic SQL approach that also gives you the schema as well:
If needed, it would be trivial to extend this to run over all databases in the instance (join to
sys.databases
).我会对 Frederik 的解决方案做一些小小的改变。我将使用 sp_spaceused 系统存储过程,其中还包括数据和索引大小。
I would make a minor change to Frederik's solution. I would use the sp_spaceused system stored procedure which will also include data and index sizes.
从 information_schema.tables 视图中选择所有行,并为从该视图返回的每个条目发出 count(*) 语句。
select all rows from the information_schema.tables view, and issue a count(*) statement for each entry that has been returned from that view.
这是我最喜欢的 SQL 2008 解决方案,它将结果放入“TEST”临时表中,我可以用它来排序并获取我需要的结果:
This is my favorite solution for SQL 2008 , which puts the results into a "TEST" temp table that I can use to sort and get the results that I need :