I know, there are standard reports and other GUI tools for doing this task for SQL Server 2005 or later but what about when you need to extract such information from a SQL Server 2000 instance or when you just want to write some code? 🙂
Get the disk usage statistics using the undocumented stored procedure sp_msforeachtable
The undocumented stored procedure sp_msforeachtable is here to help you (along with the stored procedure sp_spaceused)!
--Select the database to be scanned for table disk usage USE [DatabaseName] GO --Create temporary table 1 - Sizes will be in strings/KB create table #tmpSizes( [name] nvarchar(200), [rows] varchar(50), reserved varchar(50), data varchar(50), index_size varchar(50), unused varchar(50) ) --Create temporary table 2 - Sizes will be in KB create table #tmpSizesFinalKB( [name] nvarchar(200), [rows] int, reserved int, data int, index_size int, unused int ) --Get the disk usage per table and store in temp table 1 INSERT INTO #tmpSizes EXEC sp_MSforeachtable @command1="sp_spaceused '?'" --Indirect casting and copying of the information in temp table 2 --This is only when you want to store the disk usage statistics --in a form that allows sorting operations etc. insert into #tmpSizesFinalKB select [name], replace([rows],'KB',''), replace(reserved,'KB',''), replace(data,'KB',''), replace(index_size,'KB',''), replace(unused,'KB','') from #tmpSizes --Access the disk usage results (in KB) select * from #tmpSizesFinalKB order by reserved desc
Now you can easily manipulate the disk usage statistics in temp table #tmpSizesFinalKB in order to represent them in the form you may like!
Strengthen your SQL Server Database Administration Skills – Enroll to our Course!
If you really want to learn sophisticated SQL Server administration techniques, then you should check our on-demand online course titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).
Via the 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:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to Data Science and SQL Server Machine Learning
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Related SQL Server Administration Articles:
- Essential SQL Sever Administration Tips
- How to Patch a Standalone SQL Server Instance
- The SQL Server Browser Service and UDP Port 1434
- The Maximum Number of Concurrent Connections Setting in SQL Server
- Top 10 SQL Server DBA Daily Tasks List
- There is no SQL Server Failover Cluster Available to Join
- Encrypting a SQL Server Database Backup
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018) and a Udemy Instructor. He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.