65.9K
CodeProject is changing. Read more.
Home

Find the Size of Database mdf and ldf File in sql server

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (9 votes)

Oct 30, 2014

CPOL
viewsIcon

55707

I show a SQL query which finds the size of database mdf and ldf file in SQL Server.

Introduction

I faced a situation where I needed to find the size of the log file. What was happening was that my database size was growing very large, so I wanted to see the size of my mdf and ldf file. I wrote a simple select statement to get it.

SELECT DB_NAME(database_id) AS DBName,Name AS Logical_Name, Physical_Name,(size*8)/1024 SizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = 'MyTestDB' --databasename

If we remove the WHERE condition, we will get the result for all the databases including system default database.

SELECT DB_NAME(database_id) AS DBName,Name AS Logical_Name,Physical_Name,(size*8)/1024 SizeMB
FROM sys.master_files

Points of Interest

We can do complex things in a simple manner.