2015-02-24 102 views
0

sys.dm_os_volume_stats在SQL Server 2008 R2中引入,但在SQL Server 2008中不可用。SQL Server 2008中SQL Server 2008 R2中dm_os_volume_stats的等效物是什麼?

是自定義腳本唯一選項還是存在提供類似功能的任何其他系統視圖?

+0

量統計信息提供有關數據文件所在驅動器資料a是有辦法得到這個信息。什麼是您的確切要求 – Shanky 2015-02-24 06:58:15

+0

需要生成包含列的報告 - 數據庫名稱,數據庫的大小,事務日誌的大小,存儲它的驅動器,總驅動器空間,可用的可用空間 – psam 2015-02-24 07:01:41

+0

查看sys.master_files和xp_fixed驅動器。我知道你有足夠的競爭力來編寫你自己的查詢 – Shanky 2015-02-24 07:44:00

回答

0

自定義腳本是所有我能想到的在2008年如果執行xp_cmdshell的能力,我可能會做這樣的事......

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 

您可能需要調試我的代碼一點點,但這個概念應該讓你朝着正確的方向前進。

乾杯, 肯