Last active
March 29, 2018 17:04
-
-
Save mattmc3/c2dbc6e10a09c1002f2a99a53dcbfb6a to your computer and use it in GitHub Desktop.
SQL Generate count(*) for all tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select case when ist.ord = 1 then '' else 'union all ' end + 'select ''' + ist.TABLE_SCHEMA + '.' + ist.TABLE_NAME + ''' as table_name, count(*) as cnt from [' + ist.TABLE_SCHEMA + '].[' + ist.TABLE_NAME + ']' | |
from ( | |
select *, row_number() over (order by x.table_schema, x.table_name) as ord | |
from INFORMATION_SCHEMA.TABLES x | |
where x.TABLE_TYPE = 'BASE TABLE' | |
and x.TABLE_NAME not like 'MSreplication[_]%' | |
and x.TABLE_NAME not like 'MSsaved%' | |
and x.TABLE_NAME not like 'MSsubscription[_]%' | |
and x.TABLE_NAME not like 'MSsnapshot%' | |
) ist | |
order by ist.ord |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment