Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-02
3
Medium Priority
?
682 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 1200 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

718 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