Skip to content

Instantly share code, notes, and snippets.

@mwinckler
Created August 18, 2011 16:23
Show Gist options
  • Save mwinckler/1154438 to your computer and use it in GitHub Desktop.
Save mwinckler/1154438 to your computer and use it in GitHub Desktop.
sqlserver: find table names having more than X rows
/*
Find all tables having more than X rows
-----
Prints table name if the table has more than @minRows rows
Replace #{DBNAME} with database name
Edit (or remove) where clause as desired
*/
select 'declare @name varchar(50), @ct int, @minRows int;
set @minRows = 0;'
union
select 'select @name = ''' + [name] + ''', @ct = count(*)
from ' + [name] + '
having count(*) > @minRows;
if @ct > 0 print @name;
set @ct = 0;
set @name = null;'
from #{DBNAME}.sys.tables t
where
[name] like 'sb_tmp%'
or [name] like 'sb2_tmp%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment