2010-02-11 46 views
4

我需要確定數據庫所在驅動器上的可用空間。 我知道xp_fixeddrives程序,但是如何獲取數據庫所在的特定驅動器的信息?用sql查詢確定可用磁盤大小

+0

數據庫的選擇是在我的控制之下,所以將SQL Server 2005或更高版本。 – Ries 2010-02-11 09:08:25

回答

6

像這樣的東西?

declare @DatabaseName sysname 
set @DatabaseName = 'master' 

declare @Drive table(DriveName char, FreeSpaceInMegabytes int) 
insert @Drive execute xp_fixeddrives 

select 
    mas.type_desc FileType, 
    mas.name FileName, 
    mas.physical_name PhysicalFileName, 
    mas.size * 8/1024 FileSizeInMegabytes, 
    drv.DriveName, 
    drv.FreeSpaceInMegabytes 
from sys.master_files mas 
    left join @Drive drv on 
     left(mas.physical_name, 1) = drv.DriveName 
where database_id = db_id(@DatabaseName) 

相應地設置@DatabaseName

1

您還沒有提及您的SQL Server版本。從SQL Server 2005開始,您可以從sys.database_files獲得大量信息,並將其與xp_fixeddrives的輸出關聯起來。

0
SELECT Drive 
    , TotalSpaceGB 
    , FreeSpaceGB 
    , PctFree 
    , PctFreeExact 
    FROM 
    (SELECT DISTINCT 
     SUBSTRING(dovs.volume_mount_point, 1, 10) AS Drive 
    , CONVERT(INT, dovs.total_bytes/1024.0/1024.0/1024.0) AS TotalSpaceGB 
    , CONVERT(INT, dovs.available_bytes/1048576.0)/1024 AS FreeSpaceGB 
    , CAST(ROUND((CONVERT(FLOAT, dovs.available_bytes/1048576.0)/CONVERT(FLOAT, dovs.total_bytes/1024.0/
         1024.0) * 100), 2) AS NVARCHAR(50)) + '%' AS PctFree 
    , CONVERT(FLOAT, dovs.available_bytes/1048576.0)/CONVERT(FLOAT, dovs.total_bytes/1024.0/1024.0) * 100 AS PctFreeExact     
    FROM sys.master_files AS mf 
    CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.file_id) AS dovs) AS DE 
+0

考慮在您的步驟中添加信息以解決此問題。 – scharette 2017-11-28 22:50:10