Quantcast
Channel: Red Gate forums: SQL Backup 7
Viewing all articles
Browse latest Browse all 713

RE: PowerShell and DB restore exitcode management with sqlcmd

$
0
0
I believe I figured it out, after spending a lot more time with this. I'll post what I came up with here, just in case it helps someone else in the future:

1. Added the -b switch when calling sqlcmd, which forces to terminate the batch if there is an error.

2. Modified the restore script so that if the exitcode and sqlerrorcode are anything but 0, it will go to RAISERROR within the script (exact message does not appear to matter). Fortunately, the RedGate sqlbackup proc does allow for the exitcode and sqlerrorcode to be generated into a declared value, as such:

Quote:
DECLARE @exitcode INT
DECLARE @sqlerrorcode INT
EXECUTE master..sqlbackup '-SQL "RESTORE DATABASE [DB_NAME] FROM DISK = ''BACKUP_FILE_PATH'' WITH RECOVERY, DISCONNECT_EXISTING, REPLACE"', @exitcode OUTPUT, @sqlerrorcode OUTPUT


3. Once sqlcmd processes RAISERROR in a script, with the -b switch, the $LASTEXITCODE in PowerShell appears to go from 0 to 1, which indicates a restore error. I can then have PowerShell stop the process there, or run an alternate process, which is what I needed.

Viewing all articles
Browse latest Browse all 713

Trending Articles