I'm working on a query that gathers some information related to DB restores, and I'm having trouble getting the correct DB size. The following query provides me with the DB name, last restore date, DB size, and user name of the last person who restored it.
WITH lastrestores AS
(
SELECT
DatabaseName = [d].[name],
[r].[restore_date],
[f].[size],
[r].[user_name],
RowNum = ROW_NUMBER() OVER (PARTITION BY d.NAME ORDER BY r.[restore_date] DESC)
FROM
master.sys.databases d
LEFT OUTER JOIN
msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.NAME
LEFT JOIN
master.sys.master_files f ON d.database_id = f.database_id
)
SELECT *
FROM [lastrestores]
WHERE [rownum] = 1
AND databasename LIKE 'stuff%'
ORDER BY restore_date DESC
However, this doesn't show the correct DB size. When I check the sizes in the .mdf
file and database properties, it shows a smaller size than this query returns. When I check the sp_databases
stored procedure, it shows this being done for the DB size:
DATABASE_SIZE = CONVERT(INT,
CASE -- more than 2TB(maxint) worth of pages (by 8K each) can not fit an int...
WHEN SUM(CONVERT(BIGINT, s_mf.size)) >= 268435456
THEN NULL
ELSE SUM(CONVERT(BIGINT, s_mf.size)) * 8 -- Convert from 8192 byte pages to Kb
END)
I tried incorporating this section into my original query, but I ran into the "not contained in aggregate function or group by" error:
WITH lastrestores AS
(
SELECT
DatabaseName = [d].[name],
[r].[restore_date],
CONVERT(INT,
CASE
WHEN SUM(CONVERT(BIGINT, [f].[size])) >= 268435456
THEN NULL
ELSE SUM(CONVERT(BIGINT, [f].[size])) * 8
END) AS DBSize,
[r].[user_name],
RowNum = ROW_NUMBER() OVER (PARTITION BY d.NAME ORDER BY r.[restore_date] DESC)
FROM
master.sys.databases d
LEFT OUTER JOIN
msdb.dbo.[restorehistory] r ON r.[destination_database_name] = d.NAME
LEFT JOIN
master.sys.master_files f ON d.database_id = f.database_id
)
SELECT *
FROM [lastrestores]
WHERE [rownum] = 1
AND databasename LIKE 'stuff%'
ORDER BY restore_date DESC
While I do understand the basics of this error, I'm not really sure how to adjust this so I can get what I need since this query is getting more complex than I'm used to. My ideal outcome would be the original query I posted up top, but with the correct DB sizes as shown in sp_databases
. How can I achieve this?