Quantcast
Channel: SQL Server
Viewing all articles
Browse latest Browse all 3819

Blog Post: Use FILEPROPERTY to find free space in all the database

$
0
0
The use of SpaceUsed property of the FILEPROPERTY function gives how much space is used also we can derive lot of other attributes of it such as free space and percentage of free space. For other versions of SQL you can refer the below SQL. DECLARE @command VARCHAR( 5000 )    DECLARE @DBSpaceInfo TABLE    ( ServerName VARCHAR( 100 ),    DatabaseName VARCHAR( 100 ),    PhysicalFileName NVARCHAR( 520 ),    FileSizeMB DECIMAL( 10 , 2 ),    SpaceUsedMB DECIMAL( 10 , 2 ),    FreeSpaceMB DECIMAL( 10 , 2 ),  FreeSpacePct varchar( 8 )  )    SELECT @command =  'Use ['  +  '?'  + '] SELECT    @@servername as ServerName,    ' + ' '' ' + ' ? ' + ' '' ' + '  AS DatabaseName   , name      , convert(decimal( 12 , 2 ),round(a.size/ 128.000 , 2 )) as FileSizeMB      , convert(decimal( 12 , 2 ),round(fileproperty(a.name, '+' '' '+' SpaceUsed '+' '' '+' )/ 128.000 , 2 )) as SpaceUsedMB      , convert(decimal( 12 , 2 ),round((a.size-fileproperty(a.name, '+' '' '+' SpaceUsed '+' '' '+' ))/ 128.000 , 2 )) as FreeSpaceMB,      CAST( 100  * (CAST (((a.size/ 128.0  -CAST(FILEPROPERTY(a.name,' + '''' + 'SpaceUsed' + '''' + ' ) AS int)/ 128 .0 ) /(a.size/ 128.0 )) AS decimal( 4 , 2 ))) AS varchar( 8 )) +  ' + ' '' ' + ' % ' + ' '' ' + '  AS FreeSpacePct  from sys.database_files a'    INSERT INTO @DBSpaceInfo  EXEC sp_MSForEachDB @command      SELECT * from @DBSpaceInfo  For SQL 2000 you can refer the below sql  using sysfiles system table. DECLARE @command VARCHAR( 5000 )    DECLARE @DBInfo TABLE    ( ServerName VARCHAR( 100 ),    DatabaseName VARCHAR( 100 ),    PhysicalFileName NVARCHAR( 520 ),    FileSizeMB DECIMAL( 10 , 2 ),    SpaceUsedMB DECIMAL( 10 , 2 ),    FreeSpaceMB DECIMAL( 10 , 2 ),  FreeSpacePct varchar( 8 )  )    SELECT @command =  'Use ['  +  '?'  + '] SELECT    @@servername as ServerName,    ' + ' '' ' + ' ? ' + ' '' ' + '  AS DatabaseName   ,  filename      , convert(decimal( 12 , 2 ),round(a.size/ 128.000 , 2 )) as FileSizeMB      , convert(decimal( 12 , 2 ),round(fileproperty(a.name, '+' '' '+' SpaceUsed '+' '' '+' )/ 128.000 , 2 )) as SpaceUsedMB      , convert(decimal( 12 , 2 ),round((a.size-fileproperty(a.name, '+' '' '+' SpaceUsed '+' '' '+' ))/ 128.000 , 2 )) as FreeSpaceMB,      CAST( 100  * (CAST (((a.size/ 128.0  -CAST(FILEPROPERTY(a.name,' + '''' + 'SpaceUsed' + '''' + ' ) AS int)/ 128 .0 ) /(a.size/ 128.0 )) AS decimal( 4 , 2 ))) AS varchar( 8 )) +  ' + ' '' ' + ' % ' + ' '' ' + '  AS FreeSpacePct  from dbo. sysfiles  a'    INSERT INTO @DBInfo  EXEC sp_MSForEachDB @command      SELECT * from @DBInfo

Viewing all articles
Browse latest Browse all 3819

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>