Restore Procedure not working

  • I created a small procedure that reads the backup file and restores the DB daily. But when I execute the procedure  Exec RestoreDatabaseFromLatestBackup  @BackupPath = 'D:\Backup', @DatabaseName = 'TestDB', I get the following error

    Error occurred: The name 'Restore Database TestDB from Disk = 'D:\Backup\Backup.bak' With Replace, keep_cdc' is not a valid identifier

    But when I copy the error and run it in a different window, I am able to restore the DB successfully. Can someone help me troubleshoot the issue?

    The code is attached with this question.

     

    CREATE PROCEDURE RestoreDatabaseFromLatestBackup 
    @BackupPath NVARCHAR(500),
    @DatabaseName NVARCHAR(128)
    AS
    BEGIN
    SET NOCOUNT ON;

    DECLARE @LatestBackupFile NVARCHAR(500);
    DECLARE @cmd NVARCHAR(MAX);
    DECLARE @RestoreSQL NVARCHAR(MAX);

    BEGIN TRY

    IF @BackupPath IS NULL OR @BackupPath = ''
    BEGIN
    RAISERROR('Backup path is not provided.', 16, 1);
    RETURN;
    END

    CREATE TABLE #BackupFiles (FileName NVARCHAR(500));

    SET @cmd = 'DIR "' + @BackupPath + '\*.bak" /B';

    INSERT INTO #BackupFiles (FileName)
    EXEC xp_cmdshell @cmd;

    SELECT TOP 1 @LatestBackupFile = FileName
    FROM #BackupFiles
    WHERE FileName IS NOT NULL
    AND FileName <> ''
    AND FileName LIKE '%.bak';

    DROP TABLE #BackupFiles;

    IF @LatestBackupFile IS NULL
    BEGIN
    RAISERROR('No backup file found in the specified directory.', 16, 1);
    RETURN;
    END

    SET @LatestBackupFile = CONCAT(@BackupPath, '\', @LatestBackupFile);

    SET @RestoreSQL = N'
    RESTORE DATABASE [' + @DatabaseName + N']
    FROM DISK = ''' + @LatestBackupFile + N'''
    WITH REPLACE, Keep_cdc';

    EXEC(@RestoreSQL);


    END TRY
    BEGIN CATCH
    PRINT 'Error occurred: ' + ERROR_MESSAGE();
    END CATCH
    END;
    GO
  • This smells like a permissions issue for the SP. I am not anywhere I can test this but I suspect you will need to sign the SP and then have a certificate login in the master database that is a member of dbcreator.

     

  • I have admin rights on the test server.

  • For starters, change this...

    DECLARE @cmd NVARCHAR(MAX);

    ... to this ...

    DECLARE @cmd NVARCHAR(4000);

    Then see the following for why...

    https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql#arguments

    • This reply was modified 8 hours, 15 minutes ago by  Jeff Moden. Reason: Correct to suggestion. Op used NVARCHAR, not VARCHAR

    --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)

  • Is CDC active on the database in the backup file ?

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 5 posts - 1 through 4 (of 4 total)

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