293

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

这个想法是在存在任何行(在任何数据库中)的情况下重新体现数据库。

所说的数据库是Microsoft SQL SERVER.

有人可以建议一个示例脚本吗?

4

13 回答 13

444

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

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

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

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

SELECT SUM(row_count) AS total_row_count FROM #counts

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

于 2010-02-08T13:38:42.140 回答
227

如果您想通过计算(*)您的 300 万行表所需的时间和资源。根据 Kendal Van Dyke 的 SQL SERVER Central 试试这个。


使用 sysindexes 进行行计数 如果您使用的是 SQL 2000,则需要像这样使用 sysindexes:

-- Shows all user tables and row counts for the current database 
-- Remove OBJECTPROPERTY function call to include system objects 
SELECT o.NAME,
  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 o.NAME

如果您使用 SQL 2005 或 2008 查询 sysindexes 仍然可以工作,但 Microsoft 建议可能会在 SQL Server 的未来版本中删除 sysindexes,因此您应该使用 DMV 来代替,如下所示:

-- Shows all user tables and row counts for the current database 
-- Remove is_ms_shipped = 0 check to include system objects 
-- i.index_id < 2 indicates clustered index (1) or hash table (0) 
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 < 2  AND o.is_ms_shipped = 0 ORDER BY o.NAME 
于 2010-05-14T18:55:53.467 回答
122

在 Azure 上工作,不需要存储过程。

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 IN (0,1)

信用

于 2014-06-09T11:23:11.910 回答
65

这个看起来比我认为的其他更好。

USE  [enter your db name here]
GO

SELECT      SCHEMA_NAME(A.schema_id) + '.' +
        --A.Name, SUM(B.rows) AS 'RowCount'  Use AVG instead of SUM
          A.Name, AVG(B.rows) AS 'RowCount'
FROM        sys.objects A
INNER JOIN sys.partitions B ON A.object_id = B.object_id
WHERE       A.type = 'U'
GROUP BY    A.schema_id, A.Name
GO
于 2013-03-19T10:37:16.823 回答
26

短而甜

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

输出:

在此处输入图像描述

于 2013-10-28T10:36:16.317 回答
16
SELECT 
    sc.name +'.'+ ta.name TableName, SUM(pa.rows) RowCnt
FROM 
    sys.tables ta
INNER JOIN sys.partitions pa
    ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc
    ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name
ORDER BY SUM(pa.rows) DESC
于 2013-10-28T09:24:27.513 回答
15

SQL Server 2005 或更高版本提供了一个相当不错的报告,显示表大小 - 包括行数等。它在标准报告中 - 它是表的磁盘使用情况。

以编程方式,有一个很好的解决方案: http ://www.sqlservercentral.com/articles/T-SQL/67624/

于 2010-02-08T12:40:43.827 回答
9

不要使用SELECT COUNT(*) FROM TABLENAME,因为这是一项资源密集型操作。应该使用SQL Server 动态管理视图系统目录来获取数据库中所有表的行数信息。

于 2011-11-15T08:20:48.297 回答
3

我会对 Frederik 的解决方案做一个小的改动。我将使用 sp_spaceused 系统存储过程,它还将包括数据和索引大小。


declare c_tables cursor fast_forward for 
select table_name from information_schema.tables 

open c_tables 
declare @tablename varchar(255) 
declare @stmt nvarchar(2000) 
declare @rowcount int 
fetch next from c_tables into @tablename 

while @@fetch_status = 0 
begin 

    select @stmt = 'sp_spaceused ' + @tablename 

    exec sp_executesql @stmt

    fetch next from c_tables into @tablename 

end 

close c_tables 
deallocate c_tables 

于 2010-02-08T16:05:54.777 回答
3

这是一种动态 SQL 方法,它也为您提供架构:

DECLARE @sql nvarchar(MAX)

SELECT
    @sql = COALESCE(@sql + ' UNION ALL ', '') +
        'SELECT
            ''' + s.name + ''' AS ''Schema'',
            ''' + t.name + ''' AS ''Table'',
            COUNT(*) AS Count
            FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name)
    FROM sys.schemas s
    INNER JOIN sys.tables t ON t.schema_id = s.schema_id
    ORDER BY
        s.name,
        t.name

EXEC(@sql)

如果需要,扩展它以在实例中的所有数据库上运行(加入sys.databases)将是微不足道的。

于 2011-07-12T14:03:28.063 回答
2

从 information_schema.tables 视图中选择所有行,并为从该视图返回的每个条目发出 count(*) 语句。

declare c_tables cursor fast_forward for
select table_name from information_schema.tables

open c_tables
declare @tablename varchar(255)
declare @stmt nvarchar(2000)
declare @rowcount int
fetch next from c_tables into @tablename

while @@fetch_status = 0
begin

    select @stmt = 'select @rowcount = count(*) from ' + @tablename

    exec sp_executesql @stmt, N'@rowcount int output', @rowcount=@rowcount OUTPUT

    print N'table: ' + @tablename + ' has ' + convert(nvarchar(1000),@rowcount) + ' rows'

    fetch next from c_tables into @tablename

end

close c_tables
deallocate c_tables
于 2010-02-08T12:42:27.887 回答
1

这是我最喜欢的 SQL 2008 解决方案,它将结果放入一个“TEST”临时表中,我可以使用它来排序并获得我需要的结果:

SET NOCOUNT ON 
DBCC UPDATEUSAGE(0) 
DROP TABLE #t;
CREATE TABLE #t 
( 
[name] NVARCHAR(128),
[rows] CHAR(11),
reserved VARCHAR(18), 
data VARCHAR(18), 
index_size VARCHAR(18),
unused VARCHAR(18)
) ;
INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' 
SELECT * INTO TEST FROM #t;
DROP TABLE #t;
SELECT  name, [rows], reserved, data, index_size, unused FROM TEST \
WHERE ([rows] > 0) AND (name LIKE 'XXX%')
于 2011-10-31T16:54:56.893 回答
1
    SELECT
          SUM(sdmvPTNS.row_count) AS [DBRows]
    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 = 0
          AND sdmvPTNS.index_id < 2
    GO
于 2014-04-10T20:43:37.927 回答