Home > Database > Mysql Tutorial > body text

SQL小技巧 又快又简单的得到你的数据库每个表的记录数

WBOY
Release: 2016-06-07 17:58:54
Original
885 people have browsed it

说到如何得到表的行数,大家首先想到的应该是select count(*) from table1....

但是如何得到某个数据库所有的表的记录数,你要是用上面的方法估计得累死了。呵呵
下面提供如何借用sysindexes和sysobjects表来得到某个数据库每个表记录数的方法:
先给出SQL Server 2000版本的:
代码如下:
SELECT o.NAME,
i.rowcnt
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME

SQL Server2005/8版本的SQL 语句:
代码如下:
SELECT o.name,
ddps.row_count
FROM sys.indexes AS i
INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID
INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID
AND i.index_id = ddps.index_id
WHERE i.index_id AND o.is_ms_shipped = 0
ORDER BY o.NAME

赶快试试吧,肯定和你count(*)得到的结果相同。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!