Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Tables with Record Count

Status
Not open for further replies.

bjr149

Programmer
Jul 18, 2005
26
US
I know this query is out there. Im looking to query a whole database and return a list of all tables with the count of records in each. Is this possible?
 
Hey BJR,

Try this - Not taking any credit as I believe it was another helpful tek-tipper that did this to help me some time ago!!

Code:
declare @tabcnt int
declare @printline char (60)
select @tabcnt = count (*) from sysobjects where type = 'U'
 
If @tabcnt != 0
BEGIN
  select 'TABLE NAME'= convert (varchar (50), o.name), ROWS=i.rows
    from sysobjects o, sysindexes i
    where o.type = 'U'
      and o.id = i.id
      and i.indid in (0,1)
  order by o.name
END
 
select @printline = '(' + convert (varchar(10), @tabcnt) +
                       ' tables in ' + DB_NAME() + ')'
 
print ''
print @printline
GO

Cheers,

M.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top