473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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_Backup.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 7687
(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_Backup.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****@sommarskog.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...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/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
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...
2
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...
29
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...
2
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...
4
by: bbdobuddy | last post by:
Hi, How do I open a Microsoft Access 2003 form from Visual Basic.net Thanks in advance bbdobuddy
0
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
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...
1
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
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. ...
2
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.