Created
September 16, 2014 07:36
-
-
Save lantrix/6258c3ea8ea89452449f to your computer and use it in GitHub Desktop.
database file size statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Copy of script from Jugal Shah | |
| -- http://sqldbpool.com/2013/05/26/t-sql-script-to-find-out-the-database-file-size-space-used-and-available-free-space/ | |
| set nocount on | |
| create table #dbfileInfo( | |
| name varchar(300), | |
| location varchar(300), | |
| filesizeMB decimal(9,2), | |
| spaceUsedMB decimal(9,2), | |
| FreespaceMB decimal(9,2)) | |
| declare @mySQL nvarchar(2000) | |
| DECLARE @dbName varchar(MAX) | |
| DECLARE @cur_DBName CURSOR | |
| SET @cur_DBName = CURSOR FOR | |
| select name from sys.databases | |
| OPEN @cur_DBName | |
| FETCH NEXT | |
| FROM @cur_DBName INTO @dbName | |
| WHILE @@FETCH_STATUS = 0 | |
| BEGIN | |
| PRINT @dbName | |
| if DATABASEPROPERTYEX(@dbName, 'status') = 'ONLINE' | |
| begin | |
| select @mySQL = | |
| ' | |
| use ' + @dbname + ' | |
| INSERT INTO #dbfileInfo | |
| select | |
| name | |
| , 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 | |
| from dbo.sysfiles a | |
| ' | |
| exec sp_executesql @mySQL | |
| end | |
| FETCH NEXT | |
| FROM @cur_DBName INTO @dbName | |
| END | |
| CLOSE @cur_DBName | |
| DEALLOCATE @cur_DBName | |
| GO | |
| select * from #dbfileInfo | |
| drop table #dbfileInfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment