Solved

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

Posted on 2012-04-02
3
655 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now