Passing a parameter as a filename

  • 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...

  • 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

  • Why are you not getting the file name and path from msdb?

    Are you comfortable with the security issues related with using xp_cmdshell?

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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...

  • 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...

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply