Skip to content

Instantly share code, notes, and snippets.

@mikeblas
Created July 15, 2024 15:18
Show Gist options
  • Save mikeblas/da086ba431c94cf7a1329985d851ab48 to your computer and use it in GitHub Desktop.
Save mikeblas/da086ba431c94cf7a1329985d851ab48 to your computer and use it in GitHub Desktop.
get SQL Server database list with files, and file sizes
-- get files for each database (both log and rows)
-- including the sizes of those files
-- and growth settings
SELECT
db.name AS [Database Name],
mf.name AS [Logical Name],
mf.type_desc AS [File Type],
mf.physical_name AS [Path],
CAST(
(mf.Size * 8
) / 1024.0 AS DECIMAL(18, 1)) AS [Initial Size (MB)],
'By '+IIF(
mf.is_percent_growth = 1, CAST(mf.growth AS VARCHAR(10))+'%', CONVERT(VARCHAR(30), CAST(
(mf.growth * 8
) / 1024.0 AS DECIMAL(18, 1)))+' MB') AS [Autogrowth],
IIF(mf.max_size = 0, 'No growth is allowed', IIF(mf.max_size = -1, 'Unlimited', CAST(
(
CAST(mf.max_size AS BIGINT) * 8
) / 1024 AS VARCHAR(30))+' MB')) AS [MaximumSize],
mf.size [SizePages],
mf.size * (8192.0/(1024*1024)) [SizeMiB]
FROM
sys.master_files AS mf
INNER JOIN sys.databases AS db ON
db.database_id = mf.database_id
ORDER BY db.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment