Listing information about all database files in SQL Server

后端 未结 13 2115
遥遥无期
遥遥无期 2021-02-01 00:08

Is it possible to list information about the files (MDF/LDF) of all databases on an SQL Server?

I\'d like to get a list showing which database is using what files on th

13条回答
  •  一整个雨季
    2021-02-01 00:55

    Below script can be used to get following information: 1. DB Size Info 2. FileSpaceInfo 3. AutoGrowth 4. Recovery Model 5. Log_reuse_backup information

    CREATE TABLE #tempFileInformation
    (
    DBNAME          NVARCHAR(256),
    [FILENAME]      NVARCHAR(256),
    [TYPE]          NVARCHAR(120),
    FILEGROUPNAME   NVARCHAR(120),
    FILE_LOCATION   NVARCHAR(500),
    FILESIZE_MB     DECIMAL(10,2),
    USEDSPACE_MB    DECIMAL(10,2),
    FREESPACE_MB    DECIMAL(10,2),
    AUTOGROW_STATUS NVARCHAR(100)
    )
    GO
    
    DECLARE @SQL VARCHAR(2000)
    
    SELECT @SQL = '
     USE [?]
                INSERT INTO #tempFileInformation
                SELECT  
                    DBNAME          =DB_NAME(),     
                    [FILENAME]      =A.NAME,
                    [TYPE]          = A.TYPE_DESC,
                    FILEGROUPNAME   = fg.name,
                    FILE_LOCATION   =a.PHYSICAL_NAME,
                    FILESIZE_MB     = CONVERT(DECIMAL(10,2),A.SIZE/128.0),
                    USEDSPACE_MB    = CONVERT(DECIMAL(10,2),(A.SIZE/128.0 - ((A.SIZE - CAST(FILEPROPERTY(A.NAME,''SPACEUSED'') AS INT))/128.0))),
                    FREESPACE_MB    = CONVERT(DECIMAL(10,2),(A.SIZE/128.0 -  CAST(FILEPROPERTY(A.NAME,''SPACEUSED'') AS INT)/128.0)),
                    AUTOGROW_STATUS = ''BY '' +CASE is_percent_growth when 0 then cast (growth/128 as varchar(10))+ '' MB - ''
                                                                      when 1 then cast (growth as varchar(10)) + ''% - '' ELSE '''' END
                                                                      + CASE MAX_SIZE WHEN 0 THEN '' DISABLED '' 
                                                                                      WHEN -1 THEN '' UNRESTRICTED''
                                                                                      ELSE '' RESTRICTED TO '' + CAST(MAX_SIZE/(128*1024) AS VARCHAR(10)) + '' GB '' END
                                                                    + CASE IS_PERCENT_GROWTH WHEn 1 then '' [autogrowth by percent]'' else '''' end
        from sys.database_files A
        left join sys.filegroups fg on a.data_space_id = fg.data_space_id
        order by A.type desc,A.name
        ;
        '
    
        --print @sql
    
        EXEC sp_MSforeachdb @SQL
        go
    
        SELECT dbSize.*,fg.*,d.log_reuse_wait_desc,d.recovery_model_desc
        FROM #tempFileInformation fg
        LEFT JOIN sys.databases d on fg.DBNAME = d.name
        CROSS APPLY
        (
            select dbname,
                    sum(FILESIZE_MB) as [totalDBSize_MB],
                    sum(FREESPACE_MB) as [DB_Free_Space_Size_MB],
                    sum(USEDSPACE_MB) as [DB_Used_Space_Size_MB]
                from #tempFileInformation
                where  dbname = fg.dbname
                group by dbname
        )dbSize
    
    
    go
    DROP TABLE #tempFileInformation
    

提交回复
热议问题