Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Passing a parameter as a filename Expand / Collapse
Author
Message
Posted Tuesday, December 18, 2012 10:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 5:25 AM
Points: 85, Visits: 357
Hi there,

I'm trying to pass a parameter as a filename in a restore database statement. In my example code I have set the parameter with the correct filename however I am unsure how to code the last statement to use my parameter as the file I want to restore.


create procedure GetbackupFilename

as

SET NOCOUNT ON

truncate TABLE DIRLIST

INSERT INTO dirList (line) EXEC xp_cmdshell 'dir C:\DB_backups'

declare @Filename varchar (200)

select @filename = SUBSTRING(line,37,100) FROM dirList where line like '%.bak'

restore database chillistore2
from disk = 'C:\DB_backups'@filename

The @filename is not recognised as a txt string and therefore fails to execute and so it is this part of the query I need a hand fixing....

I appreciate any help on this. Regards, Russell.



--------------------------------------------

Laughing in the face of contention...
Post #1397900
Posted Tuesday, December 18, 2012 11:07 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 9:02 PM
Points: 33,153, Visits: 15,284
You have a few problems. First if there is more than one .bak file, you have no guarantee of which one your script will find.

Second, you can't include a variable with a string without any operator. What you probably want is something more like this, assuming you fix the first item.

select @filename = 'c:\DB_Backups\'  + SUBSTRING(line,37,100) FROM dirList where line like '%.bak'

restore database chillistore2
from disk = @filename








Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1397941
Posted Tuesday, December 18, 2012 1:52 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:29 AM
Points: 272, Visits: 848
Why are you not getting the file name and path from msdb?

Are you comfortable with the security issues related with using xp_cmdshell?
Post #1398032
Posted Tuesday, December 18, 2012 1:58 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:29 AM
Points: 272, Visits: 848

Sample code:

declare
@max_backup_set_id int,
@db_name varchar(50)

select
@db_name = '<db_name>'

select
@max_backup_set_id = max(backup_set_id)
from
msdb..backupset

select
b.physical_device_name
from
msdb..backupset a
join msdb..backupmediafamily b
on a.media_set_id = b.media_set_id
where
backup_set_id > (@max_backup_set_id - 10000) and
type = 'D' and
database_name = @db_name and
b.physical_device_name not like 'VDI%' and
( b.physical_device_name like '%Full.LSbak' OR
b.physical_device_name like '%Full%bak' ) and
backup_finish_date > dateadd(hh,-(7*24),getdate())
order by
backup_finish_date DESC
Post #1398038
Posted Tuesday, December 18, 2012 11:24 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:18 PM
Points: 36,938, Visits: 31,441
arnipetursson (12/18/2012)
Are you comfortable with the security issues related with using xp_cmdshell?


There are no security issues related with using xp_CmdShell if your system has the proper security of no one (no login or user or group) having direct access to it and have no privs higher than DBO except for DBAs. Even turning it off won't help if any login, user or group has SA privs. Even if you delete the related dll, a hacker getting in as SA can still use a trick with OPENROWSET to get to the command line. If your system isn't properly locked down, you might as well turn on xp_CmdShell because that's what a hacker is going to do for you anyway.

xp_CmdShell is not a security problem. Having bad security is a security problem.

That, notwithstanding, I do agree that, in this particular case, the filenames should come from MSDB but not for the reason most people would think. You cannot rely on the dates embedded in the filenames especially if you have Point-in-Time log backups running. Instead, you must align the LSN's with the most recent full backup in order to select the correct log files to restore.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1398195
Posted Wednesday, December 19, 2012 3:30 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 5:25 AM
Points: 85, Visits: 357
There will always only be one .bak file as a previous process overwrites the previous days .bak file.

The solution works perfectly! Thanks.


--------------------------------------------

Laughing in the face of contention...
Post #1398264
Posted Wednesday, December 19, 2012 3:33 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 5:25 AM
Points: 85, Visits: 357
Thanks for all your responses.

I will use the solution Steve posted as this works.

Steve - there will always only be one .bak file in this folder at one time but thanks for making me aware.

Thanks again all.


--------------------------------------------

Laughing in the face of contention...
Post #1398267
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse