SQL Server - Other Scripts
Useful SQL Server Table & Record Count Scripts
Practical T-SQL scripts for listing tables, schemas, and record counts
Introduction
During SQL Server administration, migration, data validation, cleansing, or troubleshooting activities, we often need quick scripts to understand the tables available in a database and their approximate record counts.
This page contains a small collection of reusable T-SQL scripts that can help with common validation activities such as:
- Listing all tables with their schema names
- Checking record counts for all tables
- Checking record counts for selected schemas
- Excluding backup or temporary tables from the result
Contents
1. List All Tables with Schema Name 2. Record Count for All Tables 3. Record Count for Selected Schemas 4. Practical Notes1. List All Tables with Schema Name
This query lists all user tables in the current database together with their associated schema names.
This is particularly useful when the same table name may exist under different schemas.
dbo.Customers
dbo.Employees
sales.Orders
staging.Customer_Load
2. Record Count for All Tables
The following query returns the approximate number of rows for every user table in the current database.
It uses SQL Server metadata rather than executing COUNT(*) against every table, which makes it useful for quickly reviewing large databases.
Exclude Backup Tables
In many DBA or data-cleansing activities, backup tables are created using naming patterns such as:
Orders_bckp
Employee_bckp
If you want to exclude these tables, add the following condition:
[_]?In a SQL Server LIKE pattern, underscore (
_) is a wildcard
representing any single character. Using [_] matches the
underscore character itself.
Complete Query Excluding Backup Tables
3. Record Count for Selected Schemas
Sometimes you do not need the counts for the entire database. For example, you may only want to validate tables belonging to selected application or staging schemas.
Replace SchemaName1 and SchemaName2 with the actual schema names.
Selected Schemas and Exclude Backup Tables
The two filters can also be combined. This is particularly useful during data validation or cleansing activities where backup tables should not be included in the comparison.
When You Need an Exact Record Count
The metadata queries above are excellent for quickly reviewing many tables. However, if you need an exact count for validation or reconciliation of a specific table, use COUNT_BIG(*) directly against that table.
Practical Notes
Instead of referring to a table only as
Customers, prefer dbo.Customers.
Schema-qualified names remove ambiguity and make validation scripts
easier to understand.
The condition p.index_id IN (0,1) restricts the metadata
calculation to the heap or clustered index, preventing rows from
nonclustered indexes from being counted again.
These scripts are useful when comparing source and target databases during SQL Server migration, data cleansing, restore validation, or environment refresh activities.
Matching record counts do not guarantee that the data itself is identical. For critical validation, follow the row-count comparison with key-level reconciliation, aggregate comparisons, checksums, or business-rule validation.
Quick Reference
| Requirement | SQL Server Object / Method |
|---|---|
| List tables | sys.tables |
| Get schema names | sys.schemas |
| Fast metadata row count | sys.partitions |
| Heap | index_id = 0 |
| Clustered index | index_id = 1 |
| Exact row count | COUNT_BIG(*) |
Key Takeaways
-
Use
sys.tablesandsys.schemasto identify schema-qualified user tables. -
Use
sys.partitionswhen you need a fast metadata-based row count across many tables. -
Filter on
index_id IN (0,1)to avoid counting rows from additional indexes. - Apply schema filters when validation is limited to specific application areas.
- Exclude backup tables when they should not participate in reconciliation.
-
Use
COUNT_BIG(*)when an exact table-level row count is required. - Treat matching row counts as an initial validation, not proof that two datasets are identical.
Comments
Post a Comment