SQJ Server: How do I retrieve database files paths with Transact sql?

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-05-05
I need a T-SQL statement that returns paths of (.MDF, .LDF) files for a given database name. I need to be able to use them PROGRAMMATICALLY in
RESTORE DATABASE ... WITH MOVE ... TO <retrieved path>
I need this to prevent an automated C#, ADO.NET procedure from  throwing exception:
'Directory lookup for the file "..." failed with the operating system error 3(The system cannot find the path specified.).'
"File 'such-n-such' cannot be restored..."
because of organizational mess (somebody moved files to a new location), and aforementioned procedure fails with its hard-coded paths.

cmd = new SqlCommand("RESTORE DATABASE " + dbName + " FROM DISK='" + backupFN + "'"
                        + withMove
                        , conn);
                    cw("\n" + cmd.CommandText + "\n");
                    cmd.CommandTimeout = 60 * 60 * 3; //expected RESTORE time as ~1 hour (07/2008)
                    /**/            cmd.ExecuteNonQuery();                        /**/
This is an excerpt from the automated procedure output of today.
Database restore - BEGIN: 11/10/2008 4:22:35 AM
Decompressed backup [\\newmail\igor$\MondayBackup.bak] file size is 7,314,078,208
connString = "Server=pac4\sql2005;Database=tempdb;Integrated Security=SSPI;"
RESTORE DATABASE [ContinuumDB] FROM DISK='\\newmail\igor$\MondayBackup.bak' WITH 
move 'ContinuumDev' TO 'D:\program files\microsoft SQL server\MSSQL\data\ContinuumDev.mdf',
move 'ContinuumLog' TO 'D:\program files\microsoft SQL server\MSSQL\data\ContinuumLog.ldf'
Directory lookup for the file "D:\program files\microsoft SQL server\MSSQL\data\ContinuumDev.mdf" failed with the operating system error 3(The system cannot find the path specified.). 
File 'ContinuumDev' cannot be restored to 'D:\program files\microsoft SQL server\MSSQL\data\ContinuumDev.mdf'. Use WITH MOVE to identify a valid location for the file. 
Directory lookup for the file "D:\program files\microsoft SQL server\MSSQL\data\ContinuumLog.ldf" failed with the operating system error 3(The system cannot find the path specified.). 
File 'ContinuumLog' cannot be restored to 'D:\program files\microsoft SQL server\MSSQL\data\ContinuumLog.ldf'. Use WITH MOVE to identify a valid location for the file. 
Problems were identified while planning for the RESTORE statement. Previous messages provide details. 
RESTORE DATABASE is terminating abnormally.
Call stack for the current thread:at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) 
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 
at CU_Restore.Program.Main(String[] args) 
Database restore - END: 11/10/2008 4:22:40 AM
Elapsed Time - 00:00:04 (01:22:38)

Open in new window

Question by:midfde
1 Comment
LVL 60

Accepted Solution

chapmandew earned 2000 total points
ID: 22921004
select * from sys.database_files

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question