Other: Total Record Counts

Other: Total Record Counts

 The following SQL script will count all records in all tables in a database.​

  1. DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowCount] INT) ;

  2. INSERT INTO @TableRowCounts ([TableName], [RowCount])
  3. EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ?' ;

  4. SELECT
  5.     [TableName], [RowCount]
  6. FROM
  7.     @TableRowCounts
  8. WHERE
  9.    [RowCount] > 0
  10. ORDER BY
  11.     [TableName]

  12. SELECT
  13.     SUM([RowCount]) as 'Total Records'
  14. FROM
  15.     @TableRowCounts