Solved

Using %ProgramFiles(x86) in "Restore Database" SQL Command

Posted on 2012-04-02
3
664 Views
Last Modified: 2012-04-02
Experts,

I am trying to run a batch file that has the following command on a 64-bit Win7 Machine:

RESTORE DATABASE Sample FROM DISK = '%ProgramFiles(x86)%\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Sample.bak' WITH REPLACE;

Open in new window


And am getting the following response:

Cannot open backup device 'c:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\%programfiles(x86)%\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\PTMethod.bak'. Operating system error 3(The system cannot find the path specified.).

I tried looking up the proper syntax for using Environment Variable in SQL commands to no avail.

Your help is deeply appreciated.

Tairo
0
Comment
Question by:Tairo
  • 2
3 Comments
 
LVL 14

Expert Comment

by:theras2000
ID: 37798361
I'm not a big SQL guy, but by simply comparing your path to the error, it seems that you don't need the path at all, because it's already there.  Try just doing 'samplePTMethod.bak' instead of your whole path.
0
 

Author Comment

by:Tairo
ID: 37798394
theras2000 - Thank you for your prompt response. When trying your suggestion, I get the following:

Line 1
Directory lookup for the file "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PTMethod.mdf" failed with the operating system error 3(The system cannot find the path specified.).

Being a 64-bit machine, the back-ups are located in the Program Files( x86) folder which was what prompted me to use an Environment Variable to begin with. I also tried the full path and it did not work.
0
 
LVL 14

Accepted Solution

by:
theras2000 earned 400 total points
ID: 37798541
Ahhh.  Did you put a spurious "(x86)" in your 1st error then?  Because now your error just has "\Program Files\".  Anyway, it seems that that path "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\" is being defined somewhere else, possibly inside the .bak file.  Sorry but I don't know enough about SQL but I can muddle through it with you.

Are you saying that your last attempt was this:
  RESTORE DATABASE Sample FROM DISK = 'Sample.bak' WITH REPLACE;
and it gave you:
  Directory lookup for the file "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PTMethod.mdf" failed...

That would tell me that it found your .bak file, and somewhere inside that it was defined to look for/create a .mdf in the regular Program Files dir.  I think you need to use commands MOVE and TO in your statement, to send it to the Program Files (x86) dir.

Do these help?
http://stackoverflow.com/questions/3345563/sql-server-restore-database
http://blog.lavablast.com/post/2008/10/14/SQL-Server-Restore-a-bak-to-a-database-in-command-line.aspx
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Freeze portion of datamart 2 22
Can I skip a node in XML? 9 32
disk usage reporting tools 27 52
install sql server management studio 2008 express error 3 31
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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