Sunday, September 7, 2014

Get Total Size and Free Space of disks hosting SQL Server database files

I have always wanted to know the disk size and free space available using TSQL and not use xp_cmdshell. I have hated to use xp_fixeddrives since it only gives the free space and not the total size of the volume. Just learnt about the DMV: sys.dm_os_volume_stats and works great. This works only on SQL Server 2008 R2 SP1 and higher though.


SELECT       DISTINCT volume_mount_point as Drive,
             logical_volume_name as Volume_Name,
            CAST(total_bytes/1024./1024./1024. as decimal(20,2)) 
as Total_Size_GB,
             CAST(available_bytes/1024./1024./1024. as decimal(20,2)) 
as Free_Space_GB,
            CAST((CAST(available_bytes as decimal(20,2))*100/total_bytes) as decimal(5,2)) 
as Percent_Free
FROM        sys.master_files AS f
CROSS APPLY sys.dm_os_volume_stats(f.database_id, f.file_id)
ORDER BY    1


OUTPUT




SQL Server fails to start. Error: 912, Severity: 21, State: 2. Script level upgrade for database 'master' failed.

Issue Description:
        SQL Server services not coming online because TempDB Tlog files go full due to Active_Transaction during startup.

Error Message (ERRORLOG):
Error: 9002, Severity: 17, State: 4.
The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'.
Error: 912, Severity: 21, State: 2.
Script level upgrade for database 'master' failed because upgrade step 'msdb110_upgrade.sql' encountered error 9002, state 4, severity 17. This is a serious error condition which might interfere with regular operation and the database will be taken offline. If the error happened during upgrade of the 'master' database, it will prevent the entire SQL Server instance from starting. Examine the previous errorlog entries for errors, take the appropriate corrective actions and re-start the database so that the script upgrade steps run to completion.
Error: 3417, Severity: 21, State: 3.
Cannot recover the master database. SQL Server is unable to run. Restore master from a full backup, repair it, or rebuild it. For more information about how to rebuild the master database, see SQL Server Books Online.
SQL Server shutdown has been initiated

Fix/Resolution:

  • Start SQL Server services from command line using the below parameters:
C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn>sqlservr.exe -sMSSQLSERVER -f -T3608
  • Open connection to SQL Server and execute the below (SSMS GUI will not work. Will have to use SQLCMD or Query Window within SSMS):
--To confirm we have the connection on the correct server
        select serverproperty('servername')

--Executed the below to resize the TempDB Log file
        USE [master]
      GO
        ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'templog', SIZE = 10GB )
        GO

--From ERRORLOG found the below:
Clearing tempdb database.
Starting up database 'model'.
CHECKDB for database 'model' finished without errors on 2014-09-06 17:00:05.970 (local time). This is an informational message only; no user action is required.
Starting up database 'tempdb'
  • Stop SQL Server service that was started from command line (Ctrl + C).
  • Restart SQL Server service from SQL Server configuration manager.
  • The upgrade script should run successfully and user connections should now be successful.