SQL Backup File Path

Hi Guys,
I have the following query which gets me the last backup date for FULL, DIFF and LOG jobs for each database.
I would like to get physical_device_name as well located in msdb.dbo.backupmediafamily.
How can I get this into the below code?
SELECT s.name AS 'Database Name',
b.backup_start_date AS 'Full DB Backup Status',
c.backup_start_date AS 'Differential DB Backup Status',
d.backup_start_date AS 'Transaction Log Backup Status'
FROM MASTER..sysdatabases s
LEFT OUTER JOIN msdb..backupset b ON s.name = b.database_name AND b.backup_start_date = (SELECT MAX(backup_start_date) AS 'Full DB Backup Status'
FROM msdb..backupset
WHERE database_name = b.database_name AND TYPE = 'D')
LEFT OUTER JOIN msdb..backupset c ON s.name = c.database_name AND c.backup_start_date = (SELECT MAX(backup_start_date) AS 'Differential DB Backup Status'
FROM msdb..backupset
WHERE database_name = c.database_name AND TYPE = 'I')
LEFT OUTER JOIN msdb..backupset d ON s.name = d.database_name AND d.backup_start_date = (SELECT MAX(backup_start_date) AS 'Transaction Log Backup Status'
FROM msdb..backupset
WHERE database_name = d.database_name AND TYPE = 'L')
WHERE s.name NOT IN ('tempdb','master','model','msdb')
ORDER BY s.name

Open in new window

ColoplastAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
I've changed a little bit your original query to use CTE, so it will work on SQL Server 2008 and higher versions:
;WITH CTE_LastBackup
AS (
	SELECT database_name, type, MAX(backup_start_date) AS BackupStatus
	FROM msdb..backupset
	GROUP BY database_name, type
	),
CTE_Backups
AS (
	SELECT l.database_name, l.type, l.BackupStatus, f.physical_device_name
	FROM msdb..backupset b
        	INNER JOIN CTE_LastBackup l ON b.database_name = l.database_name
					AND b.type = l.type
					AND b.backup_start_date = l.BackupStatus
                INNER JOIN msdb..backupmediafamily f ON b.media_set_id = f.media_set_id
	)
SELECT s.name AS 'Database Name',
	d.BackupStatus AS 'Full DB Backup Status',
	d.physical_device_name,
	i.BackupStatus AS 'Differential DB Backup Status',
	i.physical_device_name,
	l.BackupStatus AS 'Transaction Log Backup Status',
	l.physical_device_name
FROM MASTER..sysdatabases s
	LEFT OUTER JOIN CTE_Backups d ON s.name = d.database_name AND d.TYPE = 'D'
	LEFT OUTER JOIN CTE_Backups i ON s.name = i.database_name AND i.TYPE = 'I'
	LEFT OUTER JOIN CTE_Backups l ON s.name = l.database_name AND l.TYPE = 'L'
WHERE s.name NOT IN ('tempdb','master','model','msdb')
ORDER BY s.name

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ColoplastAuthor Commented:
Thanks Vitor.
It works just fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.