Technical Article

T-SQL Script for Monitoring Database Size in SQL Server

,

This T-SQL script helps database administrators monitor the size of all databases on a SQL Server instance. It retrieves the name of each database, its size in megabytes (MB), and its current status. This is useful for identifying which databases are consuming the most space and planning for storage optimization.

This T-SQL script helps you monitor the size and status of databases in SQL Server. To use it, follow these steps:

1. Open SQL Server Management Studio (SSMS).

2. Connect to your SQL Server instance.

3. Paste the script into a new query window.

4. Execute it (F5) to see the results.

-- Script to monitor database size in SQL Server
SELECT 
name AS DatabaseName,                -- Name of the database
SUM(size) * 8 / 1024 AS SizeInMB,    -- Total size in MB
state_desc AS Status                 -- Current status of the database
FROM sys.master_files
GROUP BY name, state_desc
ORDER BY SizeInMB DESC;                  -- Largest databases first

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating