查询以列出数据库中每个表中的记录数


196

如何列出数据库中每个表的行数。相当于

select count(*) from table1
select count(*) from table2
...
select count(*) from tableN

我将发布解决方案,但欢迎其他方法

Answers:


311

如果您使用的是SQL Server 2005及更高版本,则还可以使用以下命令:

SELECT 
    t.NAME AS TableName,
    i.name as indexName,
    p.[Rows],
    sum(a.total_pages) as TotalPages, 
    sum(a.used_pages) as UsedPages, 
    sum(a.data_pages) as DataPages,
    (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
    (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
    (sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY 
    object_name(i.object_id) 

我认为,它比sp_msforeachtable输出要容易处理。


1
知道为什么要过滤出以“ dt”开头的名称的表吗?我已经在网上看到了这个脚本,但是没有关于该标准的解释。我们都被巨魔了吗?
2013年

6
@Skaue:如果将“数据库图”功能安装到您自己的数据库中,则将有一些表dtProperties,依此类推;由于这些是“系统”表,因此我不想对此进行报告。
marc_s

1
是否可以在此脚本中为表名加上模式名?
gh0st 2015年

由于某种原因,此查询未返回所有表。我在数据库中有382张表。但是此查询仅返回270行(表信息)。除去where条件后,我得到302行。是否由于某个SYS表中缺少某些表信息而使联接省略了这一事实。该数据库不包含任何系统表。
Ankesh Kushwah,2015年

可以了 您可以修改它以比较两个数据库吗?
sanjeewa

107

我在http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=21021上找到的一个片段对我有所帮助:

select t.name TableName, i.rows Records
from sysobjects t, sysindexes i
where t.xtype = 'U' and i.id = t.id and i.indid in (0,1)
order by TableName;

5
我喜欢这个解决方案,尽管我会使用JOIN语法from sysobjects t inner join sysindexes i on i.id = t.id and i.indid in (0,1) where t.xtype = 'U'
Shnugo

我也更喜欢使用JOIN语句,但是在找到它时发布了代码段。:)
Erik Anderson

32

要在SQL Management Studio中获取该信息,请右键单击数据库,然后选择“报告->标准报告->按表的磁盘使用率”。


6
被低估的方法会快速生成可排序的报告,其中显示#行和数据大小。
tbone

8
SELECT 
    T.NAME AS 'TABLE NAME',
    P.[ROWS] AS 'NO OF ROWS'
FROM SYS.TABLES T 
INNER JOIN  SYS.PARTITIONS P ON T.OBJECT_ID=P.OBJECT_ID;

3
该查询将为每个表上的每个索引返回行结果。添加WHERE P.INDEX_ID IN(0,1)以仅在适当的情况下将返回结果集限制为堆或聚集索引。
Rasmus Remmer Bielidt '16

6

如此处所示,这将返回正确的计数,其中使用元数据表的方法将仅返回估计值。

    CREATE PROCEDURE ListTableRowCounts 
    AS 
    BEGIN 
        SET NOCOUNT ON 

        CREATE TABLE #TableCounts
        ( 
            TableName VARCHAR(500), 
            CountOf INT 
        ) 

        INSERT #TableCounts
            EXEC sp_msForEachTable 
                'SELECT PARSENAME(''?'', 1), 
                COUNT(*) FROM ? WITH (NOLOCK)' 

        SELECT TableName , CountOf 
            FROM #TableCounts
            ORDER BY TableName 

        DROP TABLE #TableCounts
    END
    GO

因此,听起来像是使用未记录的sproc proc sp_msForEachTable与使用有时不是最新信息的系统表的折衷方案。+1,并感谢您的链接
kristof

3
sp_MSForEachTable 'DECLARE @t AS VARCHAR(MAX); 
SELECT @t = CAST(COUNT(1) as VARCHAR(MAX)) 
+ CHAR(9) + CHAR(9) + ''?'' FROM ? ; PRINT @t'

输出:

在此处输入图片说明


我需要Sql Server 2000的东西。谢谢!
Alrekr

3

幸运的是,SQL Server管理工作室为您提供了有关如何执行此操作的提示。做这个,

  1. 启动SQL Server跟踪并打开您正在执行的活动(如果您并不孤单,请通过登录ID进行过滤,并将应用程序名称设置为Microsoft SQL Server Management Studio),暂停跟踪并丢弃您到目前为止记录的所有结果;
  2. 然后,右键单击一个表,然后从弹出菜单中选择属性;
  3. 重新开始跟踪;
  4. 现在,在SQL Server Management Studio中,选择左侧的存储属性项。

暂停跟踪,并查看Microsoft生成了什么TSQL。

在可能的最后一个查询中,您将看到一条以 exec sp_executesql N'SELECT

当您将执行的代码复制到Visual Studio时,您会注意到该代码生成了Microsoft工程师用来填充属性窗口的所有数据。

当您对该查询进行适度的修改时,您将得到如下内容:

SELECT
SCHEMA_NAME(tbl.schema_id)+'.'+tbl.name as [table], --> something I added
p.partition_number AS [PartitionNumber],
prv.value AS [RightBoundaryValue],
 fg.name AS [FileGroupName],
CAST(pf.boundary_value_on_right AS int) AS [RangeType],
CAST(p.rows AS float) AS [RowCount],
p.data_compression AS [DataCompression]
FROM sys.tables AS tbl
INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < 2
INNER JOIN sys.partitions AS p ON p.object_id=CAST(tbl.object_id AS int) AND p.index_id=idx.index_id
LEFT OUTER JOIN sys.destination_data_spaces AS dds ON dds.partition_scheme_id = idx.data_space_id and dds.destination_id = p.partition_number
LEFT OUTER JOIN sys.partition_schemes AS ps ON ps.data_space_id = idx.data_space_id
LEFT OUTER JOIN sys.partition_range_values AS prv ON prv.boundary_id = p.partition_number and prv.function_id = ps.function_id
LEFT OUTER JOIN sys.filegroups AS fg ON fg.data_space_id = dds.data_space_id or fg.data_space_id = idx.data_space_id
LEFT OUTER JOIN sys.partition_functions AS pf ON  pf.function_id = prv.function_id

现在查询不是完美的,您可以更新它以满足您可能遇到的其他问题,关键是,您可以使用Microsoft的知识通过执行您感兴趣的数据并跟踪来解决您遇到的大多数问题使用事件探查器生成的TSQL。

我有点想MS工程师知道SQL Server是如何工作的,它将生成TSQL,该TSQL可以在您使用的SSMS版本上可以使用的所有项目上运行,因此在各种发行版本,当前版本和最新版本中都非常有用。未来。

请记住,不要只是复制,也要尝试理解它,否则可能会得到错误的解决方案。

沃尔特


2

这种方法使用字符串连接来动态生成所有表及其计数的语句,就像原始问题中给出的示例一样:

          SELECT COUNT(*) AS Count,'[dbo].[tbl1]' AS TableName FROM [dbo].[tbl1]
UNION ALL SELECT COUNT(*) AS Count,'[dbo].[tbl2]' AS TableName FROM [dbo].[tbl2]
UNION ALL SELECT...

最后,使用以下命令执行EXEC

DECLARE @cmd VARCHAR(MAX)=STUFF(
                    (
                        SELECT 'UNION ALL SELECT COUNT(*) AS Count,''' 
                              + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) 
                              + ''' AS TableName FROM ' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME)
                        FROM INFORMATION_SCHEMA.TABLES AS t
                        WHERE TABLE_TYPE='BASE TABLE'
                        FOR XML PATH('')
                    ),1,10,'');
EXEC(@cmd);

请注意,此解决方案包括模式名称(可能有用)
gordon613 '19


1

首先想到的是使用sp_msForEachTable

exec sp_msforeachtable 'select count(*) from ?'

虽然它没有列出表名,所以可以扩展为

exec sp_msforeachtable 'select parsename(''?'', 1),  count(*) from ?'

这里的问题是,如果数据库有100个以上的表,您将收到以下错误消息:

查询已超过可在结果网格中显示的结果集的最大数量。网格中仅显示前100个结果集。

所以我最终使用表变量存储结果

declare @stats table (n sysname, c int)
insert into @stats
    exec sp_msforeachtable 'select parsename(''?'', 1),  count(*) from ?'
select 
    * 
from @stats
order by c desc

1

可接受的答案在Azure SQL上对我不起作用,以下是一个答案,它非常快速,并且完全符合我的要求:

select t.name, s.row_count
from sys.tables t
join sys.dm_db_partition_stats s
  ON t.object_id = s.object_id
    and t.type_desc = 'USER_TABLE'
    and t.name not like '%dss%'
    and s.index_id = 1
order by s.row_count desc

1

此sql脚本提供了所选数据库中每个表的模式,表名和行数:

SELECT SCHEMA_NAME(schema_id) AS [SchemaName],
[Tables].name AS [TableName],
SUM([Partitions].[rows]) AS [TotalRowCount]
FROM sys.tables AS [Tables]
JOIN sys.partitions AS [Partitions]
ON [Tables].[object_id] = [Partitions].[object_id]
AND [Partitions].index_id IN ( 0, 1 )
-- WHERE [Tables].name = N'name of the table'
GROUP BY SCHEMA_NAME(schema_id), [Tables].name
order by [TotalRowCount] desc

参考:https : //blog.sqlauthority.com/2017/05/24/sql-server-find-row-count-every-table-database-ficiently/

另一种方法是:

SELECT  o.NAME TABLENAME,
  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
ORDER BY i.rowcnt desc

0

我认为最短,最快和最简单的方法是:

SELECT
    object_name(object_id) AS [Table],
    SUM(row_count) AS [Count]
FROM
    sys.dm_db_partition_stats
WHERE
    --object_schema_name(object_id) = 'dbo' AND 
    index_id < 2
GROUP BY
    object_id

0

您可以尝试以下方法:

SELECT  OBJECT_SCHEMA_NAME(ps.object_Id) AS [schemaname],
        OBJECT_NAME(ps.object_id) AS [tablename],
        row_count AS [rows]
FROM sys.dm_db_partition_stats ps
WHERE OBJECT_SCHEMA_NAME(ps.object_Id) <> 'sys' AND ps.index_id < 2
ORDER BY 
        OBJECT_SCHEMA_NAME(ps.object_Id),
        OBJECT_NAME(ps.object_id)

0
USE DatabaseName
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

0

从这个问题开始:https : //dba.stackexchange.com/questions/114958/list-all-tables-from-all-user-databases/230411#230411

我将记录计数添加到@Aaron Bertrand提供的答案中,其中列出了所有数据库和所有表。

DECLARE @src NVARCHAR(MAX), @sql NVARCHAR(MAX);

SELECT @sql = N'', @src = N' UNION ALL 
SELECT ''$d'' as ''database'', 
    s.name COLLATE SQL_Latin1_General_CP1_CI_AI as ''schema'',
    t.name COLLATE SQL_Latin1_General_CP1_CI_AI as ''table'' ,
    ind.rows as record_count
  FROM [$d].sys.schemas AS s
  INNER JOIN [$d].sys.tables AS t ON s.[schema_id] = t.[schema_id]
  INNER JOIN [$d].sys.sysindexes AS ind ON t.[object_id] = ind.[id]
  where ind.indid < 2';

SELECT @sql = @sql + REPLACE(@src, '$d', name)
  FROM sys.databases
  WHERE database_id > 4
    AND [state] = 0
    AND HAS_DBACCESS(name) = 1;

SET @sql = STUFF(@sql, 1, 10, CHAR(13) + CHAR(10));

PRINT @sql;
--EXEC sys.sp_executesql @sql;

0

您可以复制,粘贴并执行这段代码,以将所有表记录计数都放入表中。注意:代码带有说明

create procedure RowCountsPro
as
begin
--drop the table if exist on each exicution
IF OBJECT_ID (N'dbo.RowCounts', N'U') IS NOT NULL 
DROP TABLE dbo.RowCounts;
-- creating new table
CREATE TABLE RowCounts 
( [TableName]            VARCHAR(150)
, [RowCount]               INT
, [Reserved]                 NVARCHAR(50)
, [Data]                        NVARCHAR(50)
, [Index_Size]               NVARCHAR(50)
, [UnUsed]                   NVARCHAR(50))
--inserting all records
INSERT INTO RowCounts([TableName], [RowCount],[Reserved],[Data],[Index_Size],[UnUsed])
--  "sp_MSforeachtable" System Procedure, 'sp_spaceused "?"' param to get records and resources used
EXEC sp_MSforeachtable 'sp_spaceused "?"' 
-- selecting data and returning a table of data
SELECT [TableName], [RowCount],[Reserved],[Data],[Index_Size],[UnUsed]
FROM RowCounts
ORDER BY [TableName]
end

我已经测试了此代码,并且在SQL Server 2014上运行良好。


0

我想分享对我有用的东西

SELECT
      QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName]
      , SUM(sdmvPTNS.row_count) AS [RowCount]
FROM
      sys.objects AS sOBJ
      INNER JOIN sys.dm_db_partition_stats AS sdmvPTNS
            ON sOBJ.object_id = sdmvPTNS.object_id
WHERE 
      sOBJ.type = 'U'
      AND sOBJ.is_ms_shipped = 0x0
      AND sdmvPTNS.index_id < 2
GROUP BY
      sOBJ.schema_id
      , sOBJ.name
ORDER BY [TableName]
GO

该数据库托管在Azure中,最终结果是: 在此处输入图片说明

信用:https : //www.mssqltips.com/sqlservertip/2537/sql-server-row-count-for-all-tables-in-a-database/


-1

如果您使用MySQL> 4.x,则可以使用以下命令:

select TABLE_NAME, TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA="test";

请记住,对于某些存储引擎,TABLE_ROWS是一个近似值。


6
他在帖子(作为标记)中提到了“ sql-server”,它是Microsoft SQL Server
marc_s

-1
select T.object_id, T.name, I.indid, I.rows 
  from Sys.tables T 
  left join Sys.sysindexes I 
    on (I.id = T.object_id and (indid =1 or indid =0 ))
 where T.type='U'

此处indid=1表示聚集索引,indid=0是HEAP


4
嗨,欢迎来到Stack Overflow。此答案与已经使用一年的答案相同...无需再次发布。
2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.