@manhnguyenv

Welcome to my blog!

Tables Row Counts

November 10, 2017 11:41

Tables Row Counts

https://www.mssqltips.com/sqlservertip/2537/sql-server-row-count-for-all-tables-in-a-database/

 

DECLARE @TableRowCounts TABLE
(
[TableName] VARCHAR(128) ,
[RowCount] INT
);
INSERT INTO @TableRowCounts
( [TableName] ,
[RowCount]
)
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ?';
SELECT [TableName] ,
[RowCount]
FROM @TableRowCounts
ORDER BY [TableName];
GO

Tables Row Counts

https://svangasql.wordpress.com/2012/08/14/script-find-the-row-count-of-all-tables-and-views/

;
WITH A AS ( SELECT TableName = O.Name ,
SchemaName = SCHEMA_NAME(O.Schema_id) ,
[Rows] = P.Rows
FROM sys.objects O
INNER JOIN sys.partitions P ON O.OBJECT_ID = P.OBJECT_ID
WHERE TYPE = 'U'
)
SELECT A.SchemaName ,
TableName ,
SUM([Rows]) RowsCount
FROM A
GROUP BY A.SchemaName ,
TableName;

Categories

Recent posts