|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 29, 2013 8:55 AM
Points: 13,
Visits: 139
|
|
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...
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
|
|
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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 8:57 AM
Points: 143,
Visits: 452
|
|
Why are you not getting the file name and path from msdb?
Are you comfortable with the security issues related with using xp_cmdshell?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 8:57 AM
Points: 143,
Visits: 452
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 33,111,
Visits: 27,037
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 29, 2013 8:55 AM
Points: 13,
Visits: 139
|
|
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...
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 29, 2013 8:55 AM
Points: 13,
Visits: 139
|
|
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...
|
|
|
|