#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog

Easily querying all the database and log file sizes on SQL Server

koskila
Reading Time 2 min
Word Count 295 words
Comments 12 comments
Rating 5 (1 votes)
View

Quite often you get the need to simply quickly query all the database sizes on MS SQL Server. Most typically, for me, this need arises when a development machine gets low on disk space, and I need to check if some database's log size has been growing, or if something else is wrong.

In my daily work, I rarely encounter this requirement anymore. Azure has almost entirely obliterated the need for such diagnostics, since it's so easy to spin up or scale up new development resources when need be.

Solution

Luckily, this is quite a simple task to accomplish, if you have high enough permissions on the SQL server. Essentially, you need to be able to query system tables - you'll need VIEW SERVER STATE permissions, which you should be able to grant with this permission:

GRANT VIEW SERVER STATE TO contosouser;

Then you can run the script below, which will query all database sizes (both Data and Log files) on SQL Server and return them in a neat table. Nice and easy!

with fs
as
(
    select database_id, type, size * 8.0 / 1024 size
    from sys.master_files
)
select 
    name,
    (select sum(size) from fs where type = 0 and fs.database_id = db.database_id) DataFileSizeMB,
    (select sum(size) from fs where type = 1 and fs.database_id = db.database_id) LogFileSizeMB
from sys.databases db

Postface

This post is kind of a classic being one of the first things I documented on my blog in 2014. But since it's almost a one-liner, I felt silly about publishing it... And whenever I might've needed the script again, I just accessed the draft version of the page.

That's cumbersome, though, so I finally thought it's worthwhile to publish it after all. Happy to see it's been helpful for a lot of people!

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Fred
2019-10-21 10:49:46)
Hey Anti, Thanks for the script! I've tried your script and all I get are NULL. Why is that ?
2019-10-22 03:11:58
Hi Fred, Which MS SQL Server version are you running and what's the whole output? Cheers!
MrB
2020-03-16 23:49:37
I'm guessing it's because you aren't getting values back from sys.master_files
Cathy
2020-04-29 23:11:37)
Your script was very helpful. Thanks Antti!
2020-05-03 23:28:37
Thanks for your comment, Cathy - happy to hear that!
Raj Kamal Singh
2020-05-01 12:06:11)
WITH fs AS ( SELECT db_name(database_id) AS DatabaseName ,type_desc AS TypeDesc, Physical_Name AS Location, (size * 8) /1024 AS SizeInMB, (size * 8) /1024/1024 AS SizeInGB FROM sys.master_files ) SELECT name, (SELECT SUM(SizeInMB) FROM fs WHERE TypeDesc = 'ROWS' AND fs.DatabaseName = db.name) DataFileSizeMB, (SELECT SUM(SizeInGB) FROM fs WHERE TypeDesc = 'ROWS' AND fs.DatabaseName = db.name) DataFileSizeGB, (SELECT SUM(SizeInMB) FROM fs WHERE TypeDesc = 'Log' AND fs.DatabaseName = db.name) LogFileSizeMB, (SELECT SUM(SizeInGB) FROM fs WHERE TypeDesc = 'Log' AND fs.DatabaseName = db.name) LogFileSizeGB FROM sys.databases db
2020-11-23 15:30:29)
Have you tried using Transaction Log - a really helpful tool for querying all the database and log file sizes on SQL Server
Antti K. Koskela
2020-11-29 10:30:37
Thanks for your comment, Andreas - I don't usually let self-promoting comments through the approval, but you did spark my interest. How would you justify a $600 license (or even a $40/mo subscription) for a task that can be easily done for free? ;)
Matthew Ellis
2021-01-28 16:55:37)
Hello, thank you for this script - we have found it extremely useful! I have one question, based on the existing script selecting sizes would it be possible to aggregate the numbers to provide an additional column providing total sizes? For example the script outputs give db name, log and file sizes so I think an additional column giving total of the two would be really helpful! Thank you again
Antti K. Koskela
2021-02-05 09:34:53
Hi Matthew, Happy to hear it's been helpful! Something like this should probably work?
WITH fs
   AS (SELECT database_id,
        type,
        size * 8.0 / 1024 size
     FROM  sys.master_files)
SELECT NAME,
    (SELECT Sum(size)
    FROM  fs
    WHERE type = 0
        AND fs.database_id = db.database_id) DataFileSizeMB,
    (SELECT Sum(size)
    FROM  fs
    WHERE type = 1
        AND fs.database_id = db.database_id) LogFileSizeMB,
		(SELECT Sum(size)
    FROM  fs
    WHERE (type = 0 OR type = 1)
        AND fs.database_id = db.database_id) TotalFileSizeMB
FROM  sys.databases db
Whitewater Magpie Ltd.
© 2025
Static Site Generation timestamp: 2025-08-21T07:25:08Z