查找数据库的可用空间(SQL Server)

时间:2018-09-17 06:40:43

标签: sql-server

我正在尝试匹配以下两个查询的结果,以查找数据库的可用空间

 SELECT 
    sum(size/128.0) AS CurrentSizeMB,  
    sum(size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 )AS FreeSpaceMB
    FROM sys.database_files

GO
    sp_spaceused

能不能建议我如何修改第一个查询,使其与sp_spaceused计算的可用空间相匹配?

1 个答案:

答案 0 :(得分:0)

尝试

    BEGIN
        DECLARE @CONST FLOAT -- To convert the pages to MB
        SELECT @CONST = CONVERT(FLOAT,8)/CONVERT(FLOAT,1024)

        SELECT
        DB_NAME() AS [database],
        f.name AS [file],
        f.size * @CONST AS size,
        FILEPROPERTY(f.name,'spaceused') * @CONST AS allocated,
        (f.size - FILEPROPERTY(f.name,'spaceused')) * @CONST AS unallocated
        FROM sys.master_files f
        JOIN sys.databases d ON d.database_id = f.database_id
        WHERE
        d.name = DB_NAME()

   END