Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

16 Experts available now in Live!

Get 1:1 Help Now