Your output does not exactly mirror the script you posted - I wondered at first why I only get ROWS until I looked again and saw the WHERE filter that caused this...
How ever - you face a few challenges with this - you possibly could have multiple files - not just two - per database - e.g. multiple log-file files.. this can be pretty challenging..
How ever - I like challenges... look at the script below - I think that would help you...
USE [master]
SELECT
MYROWS.DATABASENAME,
SUM(MYROWS.FILESIZE) AS DATA_FILESIZE, SUM(MYROWS.USEDSPACE) AS DATA_USEDSPACE, SUM(MYROWS.FREESPACE) AS DATA_FREESPACE, SUM(MYROWS.[FREESPACE_%]) AS DATA_FREESPACE_PERCENT,
SUM(MYLOGS.FILESIZE) AS LOG_FILESIZE, SUM(MYLOGS.USEDSPACE) AS LOG_USEDSPACE, SUM(MYLOGS.FREESPACE) AS LOG_FREESPACE, SUM(MYLOGS.[FREESPACE_%]) AS LOG_FREESPACE_PERCENT
FROM
(
SELECT
DB_NAME() AS DATABASENAME
,[FILESIZE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0) * 1024 * 1024
,[USEDSPACE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0 - ((SIZE/128.0) - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0)) * 1024 * 1024
,[FREESPACE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0 - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0) * 1024 * 1024
,[FREESPACE_%] = CONVERT(DECIMAL(10,2),((A.SIZE/128.0 - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0)/(A.SIZE/128.0))*100)
FROM sys.database_files A LEFT JOIN sys.filegroups fg ON A.data_space_id = fg.data_space_id
WHERE A.type_desc LIKE 'ROWS'
) MYROWS
LEFT JOIN
(
SELECT
DB_NAME() AS DATABASENAME
,[FILESIZE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0) * 1024 * 1024
,[USEDSPACE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0 - ((SIZE/128.0) - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0)) * 1024 * 1024
,[FREESPACE] = CONVERT(DECIMAL(10,2),A.SIZE/128.0 - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0) * 1024 * 1024
,[FREESPACE_%] = CONVERT(DECIMAL(10,2),((A.SIZE/128.0 - CAST(FILEPROPERTY(A.NAME, 'SPACEUSED') AS INT)/128.0)/(A.SIZE/128.0))*100)
FROM sys.database_files A LEFT JOIN sys.filegroups fg ON A.data_space_id = fg.data_space_id
WHERE A.type_desc LIKE 'LOG'
) MYLOGS
ON MYROWS.DATABASENAME = MYROWS.DATABASENAME
GROUP BY
MYROWS.DATABASENAME
This first statement USE [master] - you could replace it with USE @PRTG (I think @prtg was the parameter injection for SQL but I might be wrong) and inject the DATABASE NAME as a parameter to the SQL script directly from PRTG...
what you get is this:
| DATABASENAME | DATA_FILESIZE | DATA_USEDSPACE | DATA_FREESPACE | DATA_FREESPACE_PERCENT | LOG_FILESIZE | LOG_USEDSPACE | LOG_FREESPACE | LOG_FREESPACE_PERCENT |
1 | master | 4194304.00 | 3019898.88 | 1184890.88 | 28.13 | 786432.00 | 398458.88 | 398458.88 | 50.00 |
Hope this helps... regards
Florian Rossmark
www.it-admins.com
Add comment