One of the primary areas of responsibility for DBA's is maintenance, and one of the primary maintenance tasks is file-size management. First I'll present a list of the file-sizing tasks with short explanations for each one, then some background information, and then some T-SQL to give you the file sizes for all data and log files. Attached is a zip file that includes the T-SQL script and 4 policies you can use with Central Management Server if you're using SQL Server 2008 or later.
FILE-SIZE MANAGEMENT TASKS
NOTES
T-SQL SCRIPT
The following code will give you the total free disk space on each drive and a complete list of all database and log files on an instance of SQL Server from version 2005 to 2008 R2. The base query was derived from a SQL Server 2000 query by Michael Valentine Jones. In the results of the script below, a maximum_file value of "Unlimited" means it's not limited by the file settings. Every database file still has a size limited by the version of SQL Server and the file system.
--Show free space for each drive for an instance of SQL ServerIF OBJECT_ID('tempdb..#Disks') IS NOT NULL DROP TABLE #DisksCREATE TABLE #Disks ([drive] [sysname] NOT NULL,[free_space_mb] [int] NULL) ON [Primary] INSERT #Disks EXECUTE xp_fixeddrives SELECT * FROM #Disks DROP TABLE #Disks --Show file data for all database files in an instance of SQL ServerIF OBJECT_ID('tempdb..#Files') IS NOT NULL DROP TABLE #FilesCREATE TABLE #Files ([database_name] [sysname] NOT NULL,[file_size_mb] [decimal](12, 2) NULL,[space_used_mb] [decimal](12, 2) NULL,[free_space_mb] [decimal](12, 2) NULL,[free_space_percent] [decimal](12, 1) NULL,[autogrowth] [varchar](3) NULL,[growth_amount] [varchar](60) NULL,[maximum_size] [varchar](60) NULL,[file_control] [varchar](10) NULL,[file_density] [varchar](6) NULL,[file_id] [int] NULL,[file_type] [nvarchar](60) NULL,[file_state] [nvarchar](60) NULL,[logical_file_name] [sysname] NOT NULL,[physical_file_name] [nvarchar](260) NOT NULL,) ON [Primary] EXEC sp_msforeachdb'USE [?] INSERT #Files SELECT database_name = ''?'', file_size_mb = Convert(Decimal(12,2),Round(a.size/128.000,2)), space_used_mb = Convert(Decimal(12,2),Round(FileProperty(a.name, ''SpaceUsed'')/128.000,2)), free_space_mb = Convert(Decimal(12,2),Round((a.size - FileProperty(a.name, ''SpaceUsed''))/128.000,2)), free_space_percent = Convert(Decimal(12,1),100*(a.size - FileProperty(a.name,''SpaceUsed''))/Convert(Decimal(12,2),a.size)), autogrowth = Case When growth = 0 Then ''Off'' Else ''On'' End, growth_amount = Case When is_percent_growth = 1 Then Convert(varchar(25), growth) + ''%'' Else Convert(varchar(25), Convert(Decimal(12,1), a.growth/128.0)) + '' MB'' End, maximum_size = Case When a.max_size = 0 Then ''No Growth Allowed'' When a.max_size = -1 Or a.max_size = 268435456 Then ''Unlimited'' Else Convert(varchar(60), Convert(Decimal(12,2),Round(a.max_size/128.000,2))) + '' MB'' End, file_control = Case When is_media_read_only = 1 Or is_read_only = 1 Then ''Read-Only'' Else ''Read-Write'' End, file_density = Case When is_sparse = 1 Then ''Sparse'' Else ''Dense'' End, a.file_id, a.type_desc, a.state_desc, a.name, a.physical_nameFROM [?].sys.database_files a' SELECT file_type, total_file_size_mb = SUM(file_size_mb), total_space_used_mb = SUM(space_used_mb), total_free_space_mb = SUM(free_space_mb) FROM #Files GROUP BY file_type ORDER BY file_type DESC SELECT * FROM #Files ORDER BY database_name, file_id DROP TABLE #Files
--Show free space for each drive for an instance of SQL ServerIF OBJECT_ID('tempdb..#Disks') IS NOT NULL DROP TABLE #DisksCREATE TABLE #Disks ([drive] [sysname] NOT NULL,[free_space_mb] [int] NULL) ON [Primary]
INSERT #Disks EXECUTE xp_fixeddrives
SELECT * FROM #Disks
DROP TABLE #Disks
--Show file data for all database files in an instance of SQL ServerIF OBJECT_ID('tempdb..#Files') IS NOT NULL DROP TABLE #FilesCREATE TABLE #Files ([database_name] [sysname] NOT NULL,[file_size_mb] [decimal](12, 2) NULL,[space_used_mb] [decimal](12, 2) NULL,[free_space_mb] [decimal](12, 2) NULL,[free_space_percent] [decimal](12, 1) NULL,[autogrowth] [varchar](3) NULL,[growth_amount] [varchar](60) NULL,[maximum_size] [varchar](60) NULL,[file_control] [varchar](10) NULL,[file_density] [varchar](6) NULL,[file_id] [int] NULL,[file_type] [nvarchar](60) NULL,[file_state] [nvarchar](60) NULL,[logical_file_name] [sysname] NOT NULL,[physical_file_name] [nvarchar](260) NOT NULL,) ON [Primary]
EXEC sp_msforeachdb'USE [?] INSERT #Files SELECT database_name = ''?'', file_size_mb = Convert(Decimal(12,2),Round(a.size/128.000,2)), space_used_mb = Convert(Decimal(12,2),Round(FileProperty(a.name, ''SpaceUsed'')/128.000,2)), free_space_mb = Convert(Decimal(12,2),Round((a.size - FileProperty(a.name, ''SpaceUsed''))/128.000,2)), free_space_percent = Convert(Decimal(12,1),100*(a.size - FileProperty(a.name,''SpaceUsed''))/Convert(Decimal(12,2),a.size)), autogrowth = Case When growth = 0 Then ''Off'' Else ''On'' End, growth_amount = Case When is_percent_growth = 1 Then Convert(varchar(25), growth) + ''%'' Else Convert(varchar(25), Convert(Decimal(12,1), a.growth/128.0)) + '' MB'' End, maximum_size = Case When a.max_size = 0 Then ''No Growth Allowed'' When a.max_size = -1 Or a.max_size = 268435456 Then ''Unlimited'' Else Convert(varchar(60), Convert(Decimal(12,2),Round(a.max_size/128.000,2))) + '' MB'' End, file_control = Case When is_media_read_only = 1 Or is_read_only = 1 Then ''Read-Only'' Else ''Read-Write'' End, file_density = Case When is_sparse = 1 Then ''Sparse'' Else ''Dense'' End, a.file_id, a.type_desc, a.state_desc, a.name, a.physical_nameFROM [?].sys.database_files a'
SELECT file_type, total_file_size_mb = SUM(file_size_mb), total_space_used_mb = SUM(space_used_mb), total_free_space_mb = SUM(free_space_mb) FROM #Files GROUP BY file_type ORDER BY file_type DESC
SELECT * FROM #Files ORDER BY database_name, file_id
DROP TABLE #Files