使用 SQL 查询确定可用磁盘大小

发布于 2024-08-21 06:09:32 字数 83 浏览 11 评论 0原文

我需要确定数据库所在驱动器上的可用空间。 我了解 xp_fixeddrives 过程,但如何获取数据库所在的特定驱动器的信息?

I need to determine the available space on the drive where my database lives.
I know about the xp_fixeddrives procedure but how do I get the info for the specific drive where my database resides?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

递刀给你 2024-08-28 06:09:32

像这样的东西吗?

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

Something like this?

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)

Set @DatabaseName accordingly.

诗化ㄋ丶相逢 2024-08-28 06:09:32
    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
    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
回眸一笑 2024-08-28 06:09:32

您还没有提到您的 SQL Server 版本。从 SQL Server 2005 开始,您可以从 sys.database_files 获取大量信息,并将其与 xp_fixeddrives 的输出相关联。

You haven't mentioned your version of SQL Server. Starting with SQL Server 2005 you can get plenty of information from sys.database_files and relate that to the output of xp_fixeddrives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文