Search

Monday, August 25, 2014

Query to return database files and free space

Use below query to get database files and free space:

WITH C AS (
SELECT G.Name AS [FileGroupName],
       S.Type_Desc,
       S.Physical_Name AS [FileName],
       S.Size * CONVERT(FLOAT, 8) AS [Size],
       CAST(CASE S.Type
               WHEN 2 THEN 0
               ELSE CAST(FILEPROPERTY(S.Name, 'SpaceUsed') AS FLOAT) * CONVERT(FLOAT, 8)
             END AS FLOAT) AS [UsedSpace]
FROM Sys.filegroups AS G
INNER JOIN Sys.Master_Files AS S ON (( S.Type = 2 OR S.Type = 0)
                                      AND S.database_ID = DB_ID()
                                      AND (S.drop_lsn IS NULL))
                                    AND (S.Data_Space_ID = G.Data_Space_ID)
)
SELECT *, [Size] - [UsedSpace] AS RemainingSpace FROM C

No comments:

Post a Comment