I would like some advice on how to use a query to get the total size of each disk drive.

준영 이 40 Reputation points
2024-03-15T06:49:13.85+00:00

When I try to get the total size of each disk drive using sp_OAGetProperty in mssql, I cannot get a value greater than 17,592,186,040,320 for large drives. I would like to ask for advice on a solution that can be checked using a query.

And I don't want to use xp_cmdshell.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,004 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. Greg Low 1,770 Reputation points Microsoft Regional Director
    2024-03-15T07:20:42.0433333+00:00

    Don't use the OA procedures in new code. They are all deprecated.

    Generally you can use xp_fixeddrives to grt drive info but if you are on 2017 or later, you can query sys.dm_os_enumerate_fixed_drives


3 additional answers

Sort by: Most helpful
  1. LucyChenMSFT-4874 5,060 Reputation points
    2024-03-15T09:53:55.7666667+00:00

    Hi @준영 이 ,Based on the message you provided, here are some steps you can follow to check the size of disk drive if your server version is SQL Server 2008 R2 SP1 and later:

    1. The following example returns the total space and available space (in bytes) for all database files in the instance of SQL Server.
         
         
      
    2. The following example returns the total space and available space (in bytes) for the database files in the current database.
         
         
      
    3. The following script returns the total space, used space and free space in Gigabyte for the used disk drive.
         WITH T1 AS (
         SELECT DISTINCT
         REPLACE(vs.volume_mount_point,':\','') AS Drive_Name,
         CAST(vs.total_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)) AS Total_Space_GB ,
         CAST(vs.available_bytes / 1024.0 / 1024 / 1024  AS NUMERIC(18,2)) AS Free_Space_GB
         FROM    sys.master_files AS f
         CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) AS vs
         )
         SELECT
         Drive_Name,
         Total_Space_GB,
         Total_Space_GB-Free_Space_GB AS Used_Space_GB,
         Free_Space_GB,
         CAST(Free_Space_GB*100/Total_Space_GB AS NUMERIC(18,2)) AS Free_Space_Percent
         FROM T1
         
      
    4. You can use ‘EXEC master.dbo.xp_fixeddrives’ to get the free size of each disk drive.
    5. Hope these articles can help you understand the functions well. sys.dm_os_volume_stats (Transact-SQL) - SQL Server | Microsoft Learn , sys.master_files (Transact-SQL) - SQL Server | Microsoft Learn, sys.database_files (Transact-SQL) - SQL Server | Microsoft Learn

    Best regards,

    Lucy Chen


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    https://docs.microsoft.com/en-us/answers/support/email-notifications

    0 comments No comments

  2. LucyChenMSFT-4874 5,060 Reputation points
    2024-03-15T10:02:32.11+00:00

    Hi @준영 이 ,

    Based on the message you provided, here are some steps you can follow to check the size of disk drive if your server version is SQL Server 2008 R2 SP1 and later:

    1. The following example returns the total space and available space (in bytes) for all database files in the instance of SQL Server.
         SELECT f.database_id, f.file_id, volume_mount_point, total_bytes, available_bytes  
         FROM sys.master_files AS f  
         CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id);
         
      
    2. The following example returns the total space and available space (in bytes) for the database files in the current database.
         SELECT database_id, f.file_id, volume_mount_point, total_bytes, available_bytes  
         FROM sys.database_files AS f  
         CROSS APPLY sys.dm_os_volume_stats(DB_ID(f.name), f.file_id);
         
      
    3. The following script returns the total space, used space and free space in Gigabyte for the used disk drive.
         WITH T1 AS (
         SELECT DISTINCT
         REPLACE(vs.volume_mount_point,':\','') AS Drive_Name ,
         CAST(vs.total_bytes / 1024.0 / 1024 / 1024 AS NUMERIC(18,2)) AS Total_Space_GB ,
         CAST(vs.available_bytes / 1024.0 / 1024 / 1024  AS NUMERIC(18,2)) AS Free_Space_GB
         FROM    sys.master_files AS f
         CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) AS vs
         )
         SELECT
         Drive_Name,
         Total_Space_GB,
         Total_Space_GB-Free_Space_GB AS Used_Space_GB,
         Free_Space_GB,
         CAST(Free_Space_GB*100/Total_Space_GB AS NUMERIC(18,2)) AS Free_Space_Percent
         FROM T1
         
      
    4. You can use ‘EXEC master.dbo.xp_fixeddrives’ to get the free size of each disk drive.
    5. Hope these articles can help you understand the functions well. sys.dm_os_volume_stats (Transact-SQL) - SQL Server | Microsoft Learn , sys.master_files (Transact-SQL) - SQL Server | Microsoft Learn, sys.database_files (Transact-SQL) - SQL Server | Microsoft Learn .

    Best regards,

    Lucy Chen


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    https://docs.microsoft.com/en-us/answers/support/email-notifications


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.