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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Report the number of records in all tables in a database

Status
Not open for further replies.

philrm

MIS
Jun 1, 1999
58
0
0
AU
Can anyone tell me how to script or where i can find a script to report on howmany records are in each table of a Microsift SQL 2000 database. I'm sure this is fairly easy but can't seem to find it.

Thanks in advance
 
Try this:

DECLARE @Tab_Name As Varchar(100)
DECLARE @strSQL As Varchar(8000)
SET @strSQL = ''

DECLARE CUR_TAB CURSOR LOCAL
FOR SELECT Name
FROM sysobjects
where type = 'U'

OPEN CUR_TAB

FETCH NEXT FROM CUR_TAB
INTO @Tab_Name

WHILE @@FETCH_STATUS = 0
BEGIN
SET @strSQL = @strSQL + 'SELECT ''' + @Tab_Name + ''', COUNT(*) FROM ' + @Tab_Name + CHAR(13) + 'UNION' + CHAR(13)

FETCH NEXT FROM CUR_TAB
INTO @Tab_Name
END

CLOSE CUR_TAB
DEALLOCATE CUR_TAB

EXECUTE (@strSQL)

/* if the len of @strSQL exceeds 8000 characters then you may get errors */
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top