Skip to content

Instantly share code, notes, and snippets.

@spaghettidba
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spaghettidba/2a79d1698b1bc7847df6 to your computer and use it in GitHub Desktop.
Save spaghettidba/2a79d1698b1bc7847df6 to your computer and use it in GitHub Desktop.
SQL Server file used space
-- This is captured from SSMS (shrink file dialog)
use [yourDatabase];
select s.Name, CAST(CASE s.type WHEN 2 THEN s.size * CONVERT(float,8)
ELSE dfs.allocated_extent_page_count*convert(float,8) END AS float) AS [UsedSpaceKB]
,CASE s.type WHEN 2 THEN 0
ELSE 5120 - dfs.allocated_extent_page_count*convert(float,8) END AS [AvailableSpaceKB]
from sys.filegroups AS g
inner join sys.database_files AS s on ((s.type = 2 or s.type = 0) and (s.drop_lsn IS NULL)) AND (s.data_space_id=g.data_space_id)
left outer join sys.dm_db_file_space_usage as dfs ON dfs.database_id = db_id() AND dfs.file_id = s.file_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment