SQL Records Per Table

SQL Records Per Table

Why you need to know

There are lots of reasons why you might want to know the number of records in your tables. Database maintenance is one; user and business interest is another – I have been asked how many records are in certain tables for sales calls, blog posts, invoicing and many other reasons.

How do you count the number of records?

If you only want to know the number of records in a single table, you just run this command: –

SELECT COUNT(1) FROM [Table]

If you want to know the number of records in all tables, you can run this: –

SELECT
    sysobjects.Name
    , sysindexes.Rows
FROM
    sysobjects
    INNER JOIN sysindexes
    ON sysobjects.id = sysindexes.id
WHERE
    type = 'U'
    AND sysindexes.IndId < 2
ORDER BY
    sysindexes.[Rows] Desc

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.