Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. -- Get row counts for tables by wildcard
  2.  
  3. -- 2 stages: 1) execute the metadata query, 2) plug those results into the query proper, and optionally
  4. -- copy one of the [Select All] result columns to drill more into a found table
  5.  
  6. -- Stage 1
  7. -- Into SSMS, paste this SQL, adjusting for database name and wildcards...
  8.  
  9. use AdventureWorks2016
  10. go
  11. select 'select ''['+s.name+'].['+t.name+']'' [TableName], count(1) [CountOfRows] ' +
  12. ',''select * from ['+s.name+'].['+t.name+']'' [Select All]' +
  13. ' from ['+s.name+'].['+t.name+'] union all '
  14. from sys.tables t join sys.Schemas s
  15. on t.schema_id = s.schema_id
  16. where (
  17. -- filter below
  18. t.name like '%person%' or
  19. t.name like '%phone%' or
  20. -- filter above
  21. 1 = 0)
  22.  
  23. -- Stage 2
  24. -- Then select the result from that, and open a new window
  25. -- First past in this boilerplate:
  26.  
  27. ;with result_tables as (
  28. -- metadata below
  29.  
  30. -- metadata above
  31. select '1','1','1'
  32. )
  33. select * from result_tables r
  34. order by r.CountOfRows desc
  35.  
  36. select * from [Person].[Person]
Add Comment
Please, Sign In to add comment