Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The actual problem here is that the try and catch only gives you the last error message 3013 "backup terminating abnormally", but does not give you the lower level error for the reason the 3013 error was triggered. </p> <p>If you execute a backup command such as with an incorrect databasename, you will get 2 errors. backup database incorrect_database_name to disk = 'drive:\path\filename.bak'</p> <pre><code>Msg 911, Level 16, State 11, Line 1 Could not locate entry in sysdatabases for database 'incorrect_database_name'. No entry found with that name. Make sure that the name is entered correctly. Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally. </code></pre> <p>If you want to know the actual error for why you backup is failing within a try a catch, the stored procedure is masking it.</p> <p>Now, on to your question.. what I would do is when a restore succeeds, I would immediately delete or move the .bak to a new location, thereby removing it from the directory you stated in your parameter. Upon a failure, your catch statement can contain a GOTO that takes you back to before the BEGIN TRY and starts executing where it left off because it will not recursively detect the files you have moved from the directory.</p> <pre><code>RUN_AGAIN: BEGIN TRY RECURSIVE DIR FOR FILENAMES RESTORE DATABASE... ON SUCCEED, DELETE .BAK FILE END TRY BEGIN CATCH ON FAILURE, MOVE .BAK to A SAFE LOCATION FOR LATER ANALYSIS GOTO RUN_AGAIN END CATCH </code></pre> <p>I'm not saying it is pretty, but it will work. You cannot put a GOTO reference within a TRY/CATCH block, so it has to be outside of it. </p> <p>Anyway, I just thought I would add my thoughts to this even though the question is old, just to help out others in the same situation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload