Technical Article

Get Record count of all tables of current database

,

Either create a procedure and add this code in it or just execute it directly as a batch to get the data.

--Get all table names 
BEGIN
  Select Name into #tableNames 
  from sysobjects where xtype = 'U' order by 1

  Create Table #TableCount (TableName Varchar(100), NoOfRowCount bigint)

  declare @name varchar(100)

--declare a cursor to loop through all tables 
  declare cur cursor for select * from #tableNames
  open cur
  fetch next from cur into @name
  while @@fetch_status=0
  begin
    Insert #TableCount
    exec ('select ''' + @name + ''' , count(1) from ' + @name)
    print 'Fetching count of table : ' + @name
    fetch next from cur into @name
  end 
  close cur
  deallocate cur
  --show the data 
  Select * from #TableCount
  drop table #tableNames
  drop table #TableCount
END

Rate

4.5 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

4.5 (4)

You rated this post out of 5. Change rating