Skip to content

Commit 18e8045

Browse files
committed
#2700 sp_DatabaseRestore added CommandExecute
sp_executesql was replaced by CommandExecute so there would be some logging in case of error.
1 parent 136049a commit 18e8045

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

sp_DatabaseRestore.sql

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ DECLARE @cmd NVARCHAR(4000) = N'', --Holds xp_cmdshell command
230230
@LogLastNameInMsdbAS NVARCHAR(MAX) = N'', -- Holds last TRN file name already restored
231231
@FileListParamSQL NVARCHAR(4000) = N'', --Holds INSERT list for #FileListParameters
232232
@BackupParameters NVARCHAR(500) = N'', --Used to save BlockSize, MaxTransferSize and BufferCount
233-
@RestoreDatabaseID SMALLINT; --Holds DB_ID of @RestoreDatabaseName
233+
@RestoreDatabaseID SMALLINT, --Holds DB_ID of @RestoreDatabaseName
234+
@UnquotedRestoreDatabaseName nvarchar(128); --Holds the unquoted @RestoreDatabaseName
234235

235236
DECLARE @FileListSimple TABLE (
236237
BackupFile NVARCHAR(255) NOT NULL,
@@ -465,6 +466,8 @@ END
465466

466467
SET @RestoreDatabaseID = DB_ID(@RestoreDatabaseName);
467468
SET @RestoreDatabaseName = QUOTENAME(@RestoreDatabaseName);
469+
SET @UnquotedRestoreDatabaseName = PARSENAME(@RestoreDatabaseName,1);
470+
468471
--If xp_cmdshell is disabled, force use of xp_dirtree
469472
IF NOT EXISTS (SELECT * FROM sys.configurations WHERE name = 'xp_cmdshell' AND value_in_use = 1)
470473
SET @SimpleFolderEnumeration = 1;
@@ -752,10 +755,8 @@ BEGIN
752755
IF @sql IS NULL PRINT '@sql is NULL for SINGLE_USER';
753756
PRINT @sql;
754757
END;
755-
IF @Debug IN (0, 1) AND @Execute = 'Y'
756-
EXECUTE master.sys.sp_executesql @stmt = @sql;
757758
IF @Debug IN (0, 1) AND @Execute = 'Y' AND DATABASEPROPERTYEX(@RestoreDatabaseName,'STATUS') != 'RESTORING'
758-
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'ALTER DATABASE SINGLE_USER', @Mode = 1, @DatabaseName = @Database, @LogToTable = 'Y', @Execute = 'Y';
759+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'ALTER DATABASE SINGLE_USER', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
759760
END
760761
IF @ExistingDBAction IN (2, 3)
761762
BEGIN
@@ -774,7 +775,7 @@ BEGIN
774775
PRINT @sql;
775776
END;
776777
IF @Debug IN (0, 1) AND @Execute = 'Y'
777-
EXECUTE master.sys.sp_executesql @stmt = @sql;
778+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'KILL CONNECTIONS', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
778779
END
779780
IF @ExistingDBAction = 3
780781
BEGIN
@@ -787,7 +788,7 @@ BEGIN
787788
PRINT @sql;
788789
END;
789790
IF @Debug IN (0, 1) AND @Execute = 'Y'
790-
EXECUTE master.sys.sp_executesql @stmt = @sql;
791+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'DROP DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
791792
END
792793
IF @ExistingDBAction = 4
793794
BEGIN
@@ -800,7 +801,7 @@ BEGIN
800801
PRINT @sql;
801802
END;
802803
IF @Debug IN (0, 1) AND @Execute = 'Y' AND DATABASEPROPERTYEX(@RestoreDatabaseName,'STATUS') != 'RESTORING'
803-
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'OFFLINE DATABASE', @Mode = 1, @DatabaseName = @Database, @LogToTable = 'Y', @Execute = 'Y';
804+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'OFFLINE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
804805
END;
805806
END
806807
ELSE
@@ -854,7 +855,7 @@ BEGIN
854855
END;
855856

856857
IF @Debug IN (0, 1) AND @Execute = 'Y'
857-
EXECUTE master.sys.sp_executesql @stmt = @sql;
858+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
858859

859860
-- We already loaded #Headers above
860861

@@ -1037,7 +1038,7 @@ BEGIN
10371038
PRINT @sql;
10381039
END;
10391040
IF @Debug IN (0, 1) AND @Execute = 'Y'
1040-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1041+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
10411042

10421043
--get the backup completed data so we can apply tlogs from that point forwards
10431044
SET @sql = REPLACE(@HeadersSQL, N'{Path}', @CurrentBackupPathDiff + @LastDiffBackup);
@@ -1358,7 +1359,7 @@ WHERE BackupFile IS NOT NULL;
13581359
END;
13591360

13601361
IF @Debug IN (0, 1) AND @Execute = 'Y'
1361-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1362+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE LOG', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
13621363
END;
13631364

13641365
SET @LogRestoreRanking += 1;
@@ -1383,7 +1384,7 @@ IF @RunRecovery = 1
13831384
END;
13841385

13851386
IF @Debug IN (0, 1) AND @Execute = 'Y'
1386-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1387+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RECOVER DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
13871388
END;
13881389

13891390
-- Ensure simple recovery model
@@ -1398,7 +1399,7 @@ IF @ForceSimpleRecovery = 1
13981399
END;
13991400

14001401
IF @Debug IN (0, 1) AND @Execute = 'Y'
1401-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1402+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'SIMPLE LOGGING', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
14021403
END;
14031404

14041405
-- Run checkdb against this database
@@ -1413,7 +1414,7 @@ IF @RunCheckDB = 1
14131414
END;
14141415

14151416
IF @Debug IN (0, 1) AND @Execute = 'Y'
1416-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1417+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'DBCC CHECKDB', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
14171418
END;
14181419

14191420

@@ -1432,7 +1433,7 @@ IF @DatabaseOwner IS NOT NULL
14321433
END;
14331434

14341435
IF @Debug IN (0, 1) AND @Execute = 'Y'
1435-
EXECUTE (@sql);
1436+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'ALTER AUTHORIZATION', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
14361437
END
14371438
ELSE
14381439
BEGIN
@@ -1457,7 +1458,7 @@ IF @TestRestore = 1
14571458
END;
14581459

14591460
IF @Debug IN (0, 1) AND @Execute = 'Y'
1460-
EXECUTE master.sys.sp_executesql @stmt = @sql;
1461+
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'DROP DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
14611462

14621463
END;
14631464

0 commit comments

Comments
 (0)