Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Count All Rows In All Tables

Posted on May 27, 2014October 26, 2015 By David Kittell

There are a few ways to count all of the rows in all of your tables in a database.

The “Fastest Option” actually pulls the information from the database where the numbers are stored rather than taking time to count rows.

SELECT t.NAME AS table_name
	,i.rows
FROM sys.tables AS t
INNER JOIN sys.sysindexes AS i ON t.object_id = i.id
	AND i.indid < 2
ORDER BY table_name

--OR
SELECT @@ServerName AS SERVER
	,DB_NAME() AS DBName
	,OBJECT_SCHEMA_NAME(p.object_id) AS SchemaName
	,OBJECT_NAME(p.object_id) AS TableName
	,i.Type_Desc
	,i.NAME AS IndexUsedForCounts
	,SUM(p.Rows) AS Rows
FROM sys.partitions p
JOIN sys.indexes i ON i.object_id = p.object_id
	AND i.index_id = p.index_id
WHERE i.type_desc IN (
		'CLUSTERED'
		,'HEAP'
		)
	-- This is key (1 index per table)
	AND OBJECT_SCHEMA_NAME(p.object_id) <> 'sys'
GROUP BY p.object_id
	,i.type_desc
	,i.NAME
ORDER BY SchemaName
	,TableName;
Originally Posted on May 27, 2014
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL MSSQL - Audit SQL

Post navigation

Previous post
Next post

Related Posts

Strong Password

Posted on May 15, 2017

Need help with what a strong password is? Strong password recommendations are as follows: Must contain 14 or more characters Must contain upper and lower case letters Must contain a number or special character (e.g. !@#$%^) Must NOT be the same as your previous 12 passwords Must NOT be similar…

Read More

Proxy Switch

Posted on June 18, 2015October 26, 2015

Recently had a need to be able to switch on/off proxy at will and got tired of going to Internet Explorer options to set/change it. Hopefully someone else will get benefit from this nice tool. Open Visual Studio File -> New Project -> Visual C# – Windows -> Windows Forms…

Read More

UNIX – Display Network Information

Posted on March 6, 2017March 6, 2017

The following code will give you network specific information about your server/computer. All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes