Friday, March 18, 2016

to get the information of used/free space of all SQL Server databases in an instance.


To get the information of used/free space of all SQL Server databases in an instance.


EXEC sp_msforeachdb
'USE [?];
SELECT DB_NAME() AS [Database Name],
    CAST(SUM(CASE WHEN type_desc = ''ROWS'' THEN size END) /128.0 AS DECIMAL(18,2)) AS [Data file size(MB)],
    CAST(SUM(CASE WHEN type_desc = ''LOG'' THEN size END) / 128.0 AS DECIMAL(18,2)) AS [Log file size(MB)],
    CAST(SUM(FILEPROPERTY(name, ''SpaceUsed''))/128.0 AS decimal(18,2)) AS [Used space(MB)],
    CAST(SUM(size)/128.0 AS DECIMAL(18,2)) - CAST(SUM(FILEPROPERTY(name,''SpaceUsed''))/128.0 AS decimal(18,2)) AS [Free space(MB)]  
FROM sys.database_files
GROUP BY type_desc'



No comments:

Post a Comment

Cannot access the specified path or file on the server. Verify that you have the necessary security privileges and that the path or file exists

If you received below error while attaching a .mdf file in cluster environment please follow below steps to resolve the issue ERROR Ca...