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

 


    • Related Articles

    • Audit Scripts: Row Counts and Progress Monitoring

      This article shows you a script that can be used to monitor the progress of both a Backup and a Restore. It can also be used to just review row counts on all the Universal Database tables. The first code block is used to populate a SQL Server Temp ...
    • Other: Obtaining Record Counts for all Tables

      The following script will count all records in all tables. DECLARE @TableRowCounts TABLE ( [TableName] VARCHAR(128), [Batch_Number] INT, [Incomplete] INT, [Complete] INT, [Total] INT ) ; INSERT INTO @TableRowCounts ( [TableName], [Batch_Number], ...
    • Audit Scripts: Activity Billed/Unbilled row counts

      After you have backed up financial information like Time Entries, Expense Entries, and Fee Entries it is a good idea to audit the results prior to restoring data to the destination system. For example, some Universal Destinations support restoring ...
    • Audit Scripts: General Audit

      After completing a backup there are a number of things that are good to review prior to restoring into the destination system. The script below reviews several of the data points that should be looked at prior to restoring. This script is not ...
    • Detecting Duplicates by Reference Code

      In some scenarios, you need to ensure that a reference code is completely unique among a record type. To do this, use the following SQL script: --Look for duplicate records in the __M_Matters table SELECT * FROM --Change this to the table we want to ...