In an older post, we have discussed about, how you could rebuild all the indexes of a database in SQL Server by making use of the undocumented stored procedure “sp_MSforeachtable”.
Retrieving Table Size Info using sp_MSforeachtable
Another common task is when you want to retrieve size information for all the tables in a database.
Again, by using “sp_MSforeachtable“, you can easily do that in three simple steps:
--Step 1: --Create temporaty table for the session create table #tblInfo( [name] nvarchar (255), rows int, reserved varchar(100), data varchar(100), index_size varchar(100), unused varchar(100) ); --Step 2: --Using the stored procedure sp_spaceused retrieve --the size information for all tables and store it in the temporary table EXEC sp_MSforeachtable @command1="INSERT #tblInfo EXEC sp_spaceused '?'"; --Step 3: --Access the results select * from #tblInfo;
Note: Because sp_spaceused returns the size information as a string (except the number of rows), you will have to manipulate the data in the temporary table using casting prior to run sorting operations etc.
Examples
--Sort the results by unused space (descending) select * from #tblInfo order by cast(substring(unused,0,charindex(' ',unused)) as int) desc; --Sort the results by reserved space (descending) select * from #tblInfo order by cast(substring(reserved,0,charindex(' ',reserved)) as int) desc;
Additionally, you can create another temporary table which can contain the converted values (i.e. in MB instead of KB) of the first table.
Let’s see a relevant example:
select [name], rows, cast(substring(reserved,0,charindex(' ',reserved)) as int)/1024 as reserved_in_MB, cast(substring(data,0,charindex(' ',data)) as int)/1024 as data_in_MB, cast(substring(index_size,0,charindex(' ',index_size)) as int)/1024 as index_size_in_MB, cast(substring(unused,0,charindex(' ',unused)) as int)/1024 as unused_in_MB into #tblInfoConverted from #tblInfo; --and ... voila! select * from #tblInfoConverted order by reserved_in_MB desc;
Enroll to our Online Course and Learn More about SQL Server Administration
Check our online course on Udemy titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).
Via our course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!
Featured Online Courses:
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
Read Also:
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners (Course Preview)
- The TempDB System Database in SQL Server
- SQL Server Installation and Setup Best Practices
- The feature you are trying to use is on a network resource that is unavailable
- SQL Server 2016: TempDB Enhancements
- tempdb growth
- Introduction to SQL Server Machine Learning Services
- Essential SQL Server Administration Tips
- What are SQL Server Statistics and Where are they Stored?
- Tip of the Week No.1 – SQL Server Always Encrypted
- Tip of the Week No.3 – TempDB Settings During Installation
- Tip of the Week No.6 – About SQL Server Temporary Tables
- Tip of the Week No.18 – Benefits of Using Database Stored Procedures
- Check all our Weekly Tips!
Subscribe to our newsletter and stay up to date!
Subscribe to our YouTube channel (SQLNetHub TV)
Easily generate snippets with Snippets Generator!
Secure your databases using DBA Security Advisor!
Generate dynamic T-SQL scripts with Dynamic SQL Generator!
Check our latest software releases!
Check our eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.