sys.dm_os_volume_stats
was introduced in SQL Server 2008 R2 but not available in SQL Server 2008.
Is custom script the only option or are there any other system views that offer similar functionality?
Custom script is about all I can think of for 2008. If you have the ability to execute xp_cmdshell, I would probably do something like this...
DECLARE @recid INT
,@workstring VARCHAR(8000)
,@parsearrayxml XML
,@vexec_str VARCHAR(8000)
-- Create table variable to hold drive size info
DECLARE @drives TABLE (
[RecID] TINYINT IDENTITY(1,1) PRIMARY KEY -- Record ID
,[Drive] VARCHAR(10) -- Drive letter
,[Size] BIGINT NULL -- Drive size
)
-- Create table variable for xp_cmdshell output
DECLARE @xp_cmdshell_output TABLE (
[output] VARCHAR(8000) NULL -- Raw text returned from xp_cmdshell execution
)
INSERT INTO @xp_cmdshell_output ([output])
EXEC [master].[dbo].[xp_cmdshell] N'FSUTIL FSINFO DRIVES'
IF (SELECT COUNT(1)
FROM @xp_cmdshell_output
WHERE [output] = 'The FSUTIL utility requires that you have administrative privileges.') > 0
RAISERROR ('SQL Server Service account not an admin on this computer.', 11, 1);
ELSE
BEGIN
SELECT @workstring = [output]
FROM @xp_cmdshell_output
WHERE [output] LIKE 'Drives:%'
DELETE FROM @xp_cmdshell_output
SELECT @workstring = REPLACE(@workstring, 'Drives', '')
SELECT @workstring = REPLACE(@workstring, ':', '')
SELECT @workstring = REPLACE(@workstring, ' ', '')
IF CHARINDEX(CHAR(0), @workstring) != 0
SELECT @workstring = REPLACE(@workstring, CHAR(0), ',')
END
SELECT @parsearrayxml = CAST(
('<X>'
+ REPLACE(@workstring
,'\'
,'</X><X>')
+ '</X>')
AS XML)
INSERT INTO @drives ([Drive])
SELECT LTRIM(N.value('.', 'VARCHAR(4000)'))
FROM @parsearrayxml.nodes('X') AS T(N)
WHERE ASCII(LTRIM(N.value('.', 'VARCHAR(4000)'))) != 0
-- Get size for each drive
SELECT @recid = 1
WHILE @recid <= (SELECT MAX([RecID]) FROM @drives)
BEGIN
SELECT @workstring = ''
,@vexec_str = 'EXEC [master].[dbo].[xp_cmdshell] '
+ QUOTENAME('FSUTIL VOLUME DISKFREE '
+ [Drive]
+ ':'
,CHAR(39))
FROM @drives
WHERE [RecID] = @recid
INSERT INTO @xp_cmdshell_output ([output])
EXEC (@vexec_str)
SELECT @workstring = [output]
FROM @xp_cmdshell_output
WHERE [output] LIKE '%Total # of bytes%'
IF @workstring IS NOT NULL AND LEN(@workstring) > 0
BEGIN
SELECT @workstring = LTRIM(
SUBSTRING(@workstring
,CHARINDEX(':'
,@workstring
) + 1
,LEN(@workstring)
)
)
SELECT @workstring = LEFT(@workstring, LEN(@workstring)-1)
UPDATE @drives
SET [Size] = (CONVERT(BIGINT, @workstring)/1024)/1024
WHERE [RecID] = @recid
END
ELSE
DELETE
FROM @drives
WHERE [RecID] = @recid
DELETE FROM @xp_cmdshell_output
SELECT @recid = @recid + 1
END
SELECT * FROM @drives
You may have to debug my code a little but the concept should get you going in the right direction.
Cheers, Ken
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.