473,805 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transact-SQL for Restore Filelistonly using a variable

I am trying to script the process RESTORE FILELISTONLY using an Exec()
statement. I cant seem to get it to work with a variable. Here is
what I have.

Declare @Sql varchar(100)
Declare @File varchar(100)
Select @File = 'C:\atest\RO_Ba ckup.bak'
SELECT @Sql = ''''+ 'RESTORE FILELISTONLY FROM DISK=''' + '''' + @File
+ '''' + '''' + ''''
Exec (@Sql)

The above gives me an error: Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'RESTORE FILELISTONLY FROM DISK='C:\atest
\RO_Backup.bak' '.

However, the following DOES work:

Exec ('RESTORE FILELISTONLY FROM DISK=''c:\atest \RO_Backup.bak' '')

How can i get it to work with a variable in the Exec statement? If i
do a print statement for the @Sql variable, all the 'quotes' are in
the rite place and quantity as in the above Exec statement, but it
wont work.

Thanks!!

Greg
Jun 27 '08 #1
2 7711
(sp*****@gmail. com) writes:
I am trying to script the process RESTORE FILELISTONLY using an Exec()
statement. I cant seem to get it to work with a variable. Here is
what I have.

Declare @Sql varchar(100)
Declare @File varchar(100)
Select @File = 'C:\atest\RO_Ba ckup.bak'
SELECT @Sql = ''''+ 'RESTORE FILELISTONLY FROM DISK=''' + '''' + @File
+ '''' + '''' + ''''
Exec (@Sql)

The above gives me an error: Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'RESTORE FILELISTONLY FROM DISK='C:\atest
\RO_Backup.bak' '.
Make that a rule: when your dynamic SQL does not work out, add:

PRINT @Sql

Once you see the SQL statement, it may be all so apparent to you.

In this case the problem is that your SQL statement is a single
string literal which is not legal SQL. You should remove at least the
first and last set of ''''. Probably more, but you'll find out when
you add your debug PRINT.

Then again, why not try:

RESTORE FILELISTONLY FROM DISK=@File

Since a variable is permitted for the filename, why use dynamic SQL at
all?
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #2
>
Make that a rule: when your dynamic SQL does not work out, add:

PRINT @Sql

Once you see the SQL statement, it may be all so apparent to you.

In this case the problem is that your SQL statement is a single
string literal which is not legal SQL. You should remove at least the
first and last set of ''''. Probably more, but you'll find out when
you add your debug PRINT.

Then again, why not try:

RESTORE FILELISTONLY FROM DISK=@File

Since a variable is permitted for the filename, why use dynamic SQL at
all?
--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se

Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx
Ok thanks for your help. The reason I was using it in an Exec()
statement is because i was using it with an INSERT and storing the
results in a temp table.
After reading your post I tried Exec('Restore Filelistonly From Disk='
+ '' + @Sql + '') which worked great.
What also worked was Exec('' + @Sql + ''). Like you said, the fact
that it was a single string leteral was messing me up.

Thanks for your help!

Greg
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5357
by: Ing. Angel León | last post by:
http://wedoit4you.com/forums4you/display_message.php?Msg_pk_id=628 /** Author: Ing. Angel Leon Function: postHttps($url,$params) Given the full URL of the Target and The Params it will POST the params even if passing an HTTPS URL.
2
5620
by: Demetris | last post by:
Hello there. I have ORACLE 8.1.7.4 server on AIX 5.1. I have another server (BIZTALK) with Oracle client 8.1.7. The system is running ok. I can connect from the client on my database server just fine. However at certain stages we get the error below when it tries to open a connection.Please note that we are loading 150,000 records currently and we open / close a connection for every call. ORA-12154: TNS:could not resolve service name
29
4653
by: Paul | last post by:
Hi, I'd like to limit the number of selections a user can make in a multiple select listbox. I have a note on the interface to say that only x no. of items should be selected and I check the number server side but I'd like to implement some javascript to do the same on the client side. Ideally I'd like the javascript to work in IE5+ and Netscape6+. Thanks,
2
1879
by: pierig.gueguen | last post by:
Hello, I encounter a problem with a small portion of sqlcode. I try to go on database using "use dbname" but i always stay in master. I execute script with the sa user. declare @dbname sysname declare @ret_code int DECLARE db_cursor CURSOR FOR
4
12286
by: bbdobuddy | last post by:
Hi, How do I open a Microsoft Access 2003 form from Visual Basic.net Thanks in advance bbdobuddy
0
1431
by: et_ck | last post by:
Hi, We have a web services running to facilitate our clients in performing transactions. Below is the sample message flow: 1) App -> WS WS - perform transact WS - send response 2)
11
14674
by: moondaddy | last post by:
I have a .net 2.0 smarclient app and am trying to deploy it to IIS where users can access it from. I created an application folder in IIS where I'm trying to deployee to. 1) When the application folder was set to 'Allow scripts only', I get an error: Failed to connect to 'http://192.168.0.5/TransAct/' with the following error:
1
23755
dmjpro
by: dmjpro | last post by:
I have performed a COMMIT operation.But I want to rollback the same.Is there any timestamp in which i can perform the ROLLBACK after COMMIT.
23
5268
by: Ciara9 | last post by:
I have a table of static values that I am acessing from another table to populate combo boxes. Next I have a form where this information is selected by the user and updates directly to the table. Everything works fine. I can create simple queries and reports with no problem as well as update the data. If I try to add any criteria, even as simple as using "like" to identify a value in a field of the table that has been populated by the combo...
2
2128
by: Barry | last post by:
After reading Andrei Alexandrescu and Petru Marginean's "Generic: Change the Way You Write Exception-Safe Code ¡ª Forever" http://www.ddj.com/cpp/184403758 I borrow Boost.Function, which makes the implementation much simpler. here goes the demo: #include <boost/function.hpp> #include <boost/bind.hpp>
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.