Other: Obtaining Record Counts for all Tables
The following script will count all records in all tables.
- DECLARE
- @TableRowCounts
TABLE (
- [TableName] VARCHAR(128),
- [Action_Group] INT,
- [Incomplete] INT,
- [Complete] INT,
- [Total] INT
- )
- ;
-
- INSERT INTO
- @TableRowCounts (
- [TableName],
- [Action_Group],
- [Incomplete],
- [Complete],
- [Total]
- )
- EXEC
sp_MSforeachtable '
- SELECT
- ''?'' AS
[TableName],
- [Action_Group],
- SUM(CASE WHEN
Result_Id = '''' THEN 1 ELSE 0 END) AS [Incomplete],
- SUM(CASE WHEN
Result_Id = '''' THEN 0 ELSE 1 END) AS [Complete],
- COUNT(*) as [Total]
- FROM ?
- GROUP BY Action_Group
- ';
-
- SELECT
- Action_Group,
TableName, Incomplete, Complete, Total
- FROM
- @TableRowCounts
- WHERE 1=1
- AND Total > 0
- ORDER BY
- Action_Group,
- [TableName]
-
- GO