Skip to content

Commit d752cd5

Browse files
authored
Merge pull request #3095 from bwiggins10/dev
sp_DatabaseRestore - add check for CommandExec in current database fix
2 parents aab7192 + c3abc93 commit d752cd5

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

sp_DatabaseRestore.sql

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,20 @@ BEGIN
212212
RETURN;
213213
END;
214214

215-
IF NOT EXISTS (SELECT name FROM sys.objects WHERE type = 'P' AND name = 'CommandExecute')
215+
BEGIN TRY
216+
DECLARE @CurrentDatabaseContext AS VARCHAR(128) = (SELECT DB_NAME());
217+
DECLARE @CommandExecuteCheck VARCHAR(315)
218+
219+
SET @CommandExecuteCheck = 'IF NOT EXISTS (SELECT name FROM ' +@CurrentDatabaseContext+'.sys.objects WHERE type = ''P'' AND name = ''CommandExecute'')
216220
BEGIN
217-
RAISERROR('DatabaseRestore requires the CommandExecute stored procedure from the OLA Hallengren Maintenance solution, are you using the correct database?', 15, 1);
221+
RAISERROR (''DatabaseRestore requires the CommandExecute stored procedure from the OLA Hallengren Maintenance solution, are you using the correct database?'', 15, 1);
218222
RETURN;
219-
END;
223+
END;'
224+
EXEC (@CommandExecuteCheck)
225+
END TRY
226+
BEGIN CATCH
227+
THROW;
228+
END CATCH
220229

221230
DECLARE @cmd NVARCHAR(4000) = N'', --Holds xp_cmdshell command
222231
@sql NVARCHAR(MAX) = N'', --Holds executable SQL commands

0 commit comments

Comments
 (0)