Technical Article

Table count for non-system dbs

,

This script returns the table count for all non-system dbs on the server it is run against.

set nocount on

if (select object_id('tempdb.dbo.#dbs')) > 0
   exec ('Drop table #dbs')
create table #dbs(name sysname null, value int null)

declare @db sysname
declare @cnt int
declare @cmd varchar(255)

insert into #dbs(name) select name from sysdatabases
where name not in ('master','model','pubs','northwind','msdb','tempdb')

select @db = min(name) from #dbs
while @db is not null
   begin
      select @cmd = 'use ' + @db + ' update #dbs set value = (select count(*) from sysobjects where type = "U") where name = "' + @db + '"'
      exec (@cmd)
      select @db = min(name) from #dbs where name > @db
   end
select convert(varchar(20), name) [Database],
       value [Table cnt]
  from #dbs

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating