Get The Size & Record Count Of All Tables In A SQL Server Database
Share
Use the SQL query below to display the size and record count of all tables in an SQL Server database.
This is an update of the previous post: How To Find The Physical Size Of SQL Server Tables And Indexes
SELECT t.NAME AS TableName ,s.Name AS SchemaName ,p.rows ,CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB ,CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB ,CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.NAME NOT LIKE 'dt%' AND t.is_ms_shipped = 0 AND i.OBJECT_ID > 255 and s.Name='PRODDTA' and t.name like '%[_]%' GROUP BY t.Name, s.Name, p.Rows ORDER BY TotalSpaceMB DESC, t.Name
Author
Stewart Schatz
More Stories
How to Print a Directory Structure Using PowerShell: Step-by-Step Guide
If you need to print out the directory structure and list the files within, this PowerShell script will help you...
Windows God Mode… What!?!
Windows God Mode is a hidden feature in the Windows operating system that allows users to access all of the system’s control panel options and settings in a single place.
How To Test A SQL Server Connection
There is an easy way to test your SQL Server connection when running Windows without any special software. I found...
4 Lines To Export Outlook Rules To Excel/CSV Using PowerShell
I used to use a ton of Outlook Rules to organize the thousands of emails that I receive each day....
How To Get AS400/iSeries Job Queue Information Using SQL
I received a request the other day about the JD Edwards EnterpriseOne job queues and what the maximum number of...
The EnterpriseOne CNC Job Description
The JD Edwards ERP Specialist is responsible for maintaining the JD Edwards EnterpriseOne platform and auxiliary systems from a development, CNC, system administration, and functional consultant perspective to create and maintain business solutions.
Average Rating