title: List of all tables across all databases with row count of each table
body: <p>I want to get the list of ALL tables across ALL databases in a server along with row count of EACH table.</p>
  
  <p>For now, I have developed a code that gives the list of all tables with all databases. However, I need help in adding row count of each of these tables.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Can someone help with writing the code that gives the row counts for these tables?</p>