Solved

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

Posted on 2012-04-02
3
667 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

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…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

738 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