IAN ESCARRO

Counting Rows

May 22, 2024

Not the band, Counting Crows. Rows, like database rows. I love the band, btw. 90s, yeah! Anyway, there are several reasons why we need to count rows in SQL Server. Most important for me is knowing that a certain process affects certain database tables. Disregarding the update for now but what changes. What are new inserted rows? And what are deleted. This way we know (roughly) database tables that are affected, especially if the call in code is too big. So here is how we do it

SELECT
	t.name AS TableName,
	SUM(p.rows)
	FROM
	sys.tables AS t
INNER JOIN
	sys.partitions AS p ON t.object_id = p.object_id
WHERE
	t.is_ms_shipped = 0
	AND p.index_id IN (0, 1)
GROUP BY
	t.name;

ABOUT IAN ESCARRO

Founded ICE Solutions.

Powered by cmsInferno with ❤️!