查詢數據庫大小的代碼

SELECT a.name [文件名稱]  
    ,cast(a.[size]*1.0/128 as decimal(12,1)) AS [文件設置大小(MB)]  
    ,CAST( fileproperty(s.name,'SpaceUsed')/(8*16.0) AS DECIMAL(12,1)) AS [文件所佔空間(MB)]  
    ,CAST( (fileproperty(s.name,'SpaceUsed')/(8*16.0))/(s.size/(8*16.0))*100.0  AS DECIMAL(12,1)) AS [所佔空間率%]  
    ,CASE WHEN A.growth =0 THEN '文件大小固定,不會增加' ELSE '文件將自動增加' end [增加模式]  
    ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN '增量爲固定大小'  
        WHEN A.growth > 0 AND is_percent_growth = 1 THEN '增量將用整數百分比表示'  
        ELSE '文件大小固定,不會增加' END AS [增量模式]  
    ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN cast(cast(a.growth*1.0/128as decimal(12,0)) AS VARCHAR)+'MB'  
        WHEN A.growth > 0 AND is_percent_growth = 1 THEN cast(cast(a.growth AS decimal(12,0)) AS VARCHAR)+'%'  
        ELSE '文件大小固定,不會增加' end AS [增加值(%或MB)]  
    ,a.physical_name AS [文件所在目錄]  
    ,a.type_desc AS [文件類型]  
FROM sys.database_files  a  
INNER JOIN sys.sysfiles AS s ON a.[file_id]=s.fileid  
LEFT JOIN sys.dm_db_file_space_usage b ON a.[file_id]=b.[file_id]
相關文章
相關標籤/搜索