31 January,2017 by Tom Collins
Question: I need some sql code to record the row count of every table in a database.
I only need to run this code once , but it’s on a database with hundreds of tables
Answer: There are numerous ways of getting the row count for every table in a database.
If I require a quick method , just to simply record every table row count , I’ll use some code used in this example.
This code utilises the sp_msforeachtable stored procedure . As the input parameter, I include an INSERT INTO .. construct.
The sp_msforeachtable stored procedure iterates through every table – and records the count.
If you want some code with more detail - read more on SQL Server – Get sql table size and last updated for all tables
CREATE TABLE #TblRowCounts(TableName VARCHAR(128),NumberOfRows BIGINT) use mydatabase go sp_msforeachtable 'INSERT INTO #TblRowCounts select ''?'' as ''Table'', count(*) as ''Rows'' from ? ' select * from #TblRowCounts GO DROP TABLE #TblRowCounts
Read More
SQL Server - How to find the largest sql index and table size
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |