Try this one -
DECLARE
@SQL NVARCHAR(1000)
, @DB_NAME NVARCHAR(100) = 'AdventureWorks2008R2'
SELECT TOP 1 @SQL = '
BACKUP DATABASE [' + @DB_NAME + ']
TO DISK = ''' + REPLACE(mf.physical_name, '.mdf', '.bak') + ''''
FROM sys.master_files mf
WHERE mf.[type] = 0
AND mf.database_id = DB_ID(@DB_NAME)
PRINT @SQL
EXEC sys.sp_executesql @SQL
Output -
BACKUP DATABASE [AdventureWorks2008R2]
TO DISK = 'D:DATABASESQL2012AdventureWorks2008R2.bak'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…