Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active March 29, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmc3/c2dbc6e10a09c1002f2a99a53dcbfb6a to your computer and use it in GitHub Desktop.
Save mattmc3/c2dbc6e10a09c1002f2a99a53dcbfb6a to your computer and use it in GitHub Desktop.
SQL Generate count(*) for all tables
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