I would like to compare the records count for each view in given schema between the databases: DEV and PROD. The goal is to verify if the corresponding views (in DEV and PROD) have the same record count.
Let say we have something like this:
Database_DEV.Schema.View01
Database_DEV.Schema.View02
Database_DEV.Schema.View03
and
Database_PROD.Schema.View01
Database_PROD.Schema.View02
Database_PROD.Schema.View03
And as a result I want to have a table like:
| ViewName | Database_DEV | Database_PROD |
|---|---|---|
| View01 | 345 | 345 |
| View02 | 450 | 450 |
| View03 | 555 | 666 |
The view name should be taken dynamically from INFORMATION_SCHEMA table:
SELECT TABLE_NAME
FROM Database_PROD.INFORMATION_SCHEMA.TABLES
WHERE 1=1
AND TABLE_CATALOG = 'Database_PROD'
AND TABLE_TYPE = 'VIEW'
AND TABLE_SCHEMA = 'Schema'
I assume that I can get the result using cursor?
Thanks for any help.
You could, but it would probably be easier to use a SQL generator and UNION ALL.
The SQL generator would look like this (change pattern to suit):
The SQL generator will generate a single row and column to copy and paste or use in a script. It will look like this:
Running it produced output like this:
You could even have the SQL generator add another column that compares the PROD_COUNT and DEV_COUNT and report true or false if they match or do not match.