所有数据库中所有 table 的列表,每个 table 的行数

List of all tables across all databases with row count of each table

我想获取服务器中所有数据库的所有 table 的列表以及每个 table.

的行数

现在,我开发了一个代码,它给出了所有数据库的所有 table 的列表。但是,我需要帮助来添加每个 table 的行数。

CREATE  PROCEDURE [dbo].[List_of_tables_in_All_Databases]
    (@clean BIT = 1, 
     @debugEnabled BIT = 0)
AS
BEGIN 
    DECLARE @message  NVARCHAR(MAX)
    DECLARE @procName NVARCHAR(250) = object_name(@@procid);

    DECLARE @processingProcStart DATETIME = GETDATE()
    DECLARE @processingprocTime  INT = 0

    SET @message = 'List_of_tables_in_All_Databases START: ' + CONVERT(VARCHAR(23), GETDATE(), 121);

    EXEC [dbo].[usp_KREOInsertLog] @type = 'INFO', @source = 'Analysis', @storedProcedure = @procName, @debugLevel = 1, @message = @message, @show = @debugEnabled

IF OBJECT_ID (N'tempdb.dbo.#AllTables ') IS NOT NULL
     DROP TABLE #AllTables 

SET NOCOUNT ON

CREATE TABLE #AllTables 
(
    ServerName NVARCHAR(200),
    DBName NVARCHAR(200),
    SchemaName NVARCHAR(200),
    TableName NVARCHAR(200),
    [RowCount] INT 
)

DECLARE @SearchSvr NVARCHAR(200),
        @SearchDB NVARCHAR(200),
        @SearchS NVARCHAR(200),
        @SearchTbl NVARCHAR(200),
        @SQL NVARCHAR(4000)

SET @SearchSvr = NULL  --Search for Servers, NULL for all Servers
SET @SearchDB = NULL   --Search for DB, NULL for all Databases
SET @SearchS = 'dbo'   --Search for Schemas, NULL for all Schemas
SET @SearchTbl = NULL  --Search for Tables, NULL for all Tables

SELECT @SQL = 'SELECT @@SERVERNAME
        ,''?''
        ,s.name
        ,t.name
        FROM sys.tables t 
         JOIN sys.schemas s on t.schema_id=s.schema_id 
         WHERE @@SERVERNAME LIKE ''%' + ISNULL(@SearchSvr, '') + '%''
         AND ''?'' LIKE ''%' + ISNULL(@SearchDB, '') + '%''
         AND s.name LIKE ''%' + ISNULL(@SearchS, '') + '%''
         AND t.name LIKE ''%' + ISNULL(@SearchTbl, '') + '%''
         AND ''?'' NOT IN (''master'',''model'',''msdb'',''tempdb'',''SSISDB'')
           '
-- Remove the '--' from the last statement in the WHERE clause to exclude system tables

INSERT INTO #AllTables (ServerName, DBName, SchemaName, TableName)
    EXEC sp_MSforeachdb @SQL

SELECT * FROM #AllTables

有人可以帮助编写给出这些 table 的行数的代码吗?

有一种使用 SQL 服务器元数据获取行数的快速方法。您可以将其添加到 @SQL:

中的查询中
SELECT [Rows] = SUM(row_count)
FROM sys.dm_db_partition_stats
WHERE object_id=@YourObjectId   
AND (index_id=0 or index_id=1);

我相信完整的 @SQL 如下。未经测试,但至少应该非常接近:

SELECT @SQL = 'SELECT @@SERVERNAME
        ,''?''
        ,s.name
        ,t.name
        ,SUM(p.row_count) as [rows]
        FROM sys.tables t 
         JOIN sys.schemas s on t.schema_id=s.schema_id 
         LEFT JOIN sys.dm_db_partition_stats p
           ON p.object_id = t.object_id
             and (p.index_id = 0 or p.index_id = 1)
         WHERE @@SERVERNAME LIKE ''%' + ISNULL(@SearchSvr, '') + '%''
         AND ''?'' LIKE ''%' + ISNULL(@SearchDB, '') + '%''
         AND s.name LIKE ''%' + ISNULL(@SearchS, '') + '%''
         AND t.name LIKE ''%' + ISNULL(@SearchTbl, '') + '%''
         AND ''?'' NOT IN (''master'',''model'',''msdb'',''tempdb'',''SSISDB'')
        GROUP BY s.name, t.name
           '

您还可以显示以 MB 为单位的大小以及该信息,我总是为此使用此查询

select t.NAME AS TableName,
       i.name as indexName,
       sum(p.rows) / count(a.total_pages) as RowCounts,
       count(a.total_pages) as page_count,
       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
order by sum(p.rows) / count(a.total_pages) DESC