Technical Article

Get Record Count for each table in a database.

,

I wrote this script to compare record counts between a live database and a restored copy to test backups, I thought people might find it useful.
What You need is to just copy the script and run it against your database.

/**********************************************************
SCRIPT WRITTEN BY  : HARI NARAYAN SHARMA
SCRIPT WRITTEN DATE: 15 AUGUST 2007
PURPOSE   : TO GET REOCRD COUNTS FOR EACH TABLE IN A DB.
**********************************************************/DECLARE @Table TABLE (Table_Name VARCHAR(100), Record_Count INT)
DECLARE @Table_Name VARCHAR(100)
DECLARE CUR CURSOR FOR 
SELECT name FROM SYSOBJECTS WHERE xtype='U'
OPEN CUR
FETCH NEXT FROM CUR INTO @Table_Name
WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO @Table 
EXEC('SELECT '''+@Table_Name+''',(SELECT COUNT(*) FROM ['+@Table_Name+'])')
FETCH NEXT FROM CUR INTO @Table_Name
END
CLOSE CUR
DEALLOCATE CUR

SELECT * FROM @Table
GO

Rate

1 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (1)

You rated this post out of 5. Change rating