Skip to content

Commit 3a94b58

Browse files
authored
Merge pull request #2982 from jesusnac/dev
Issue #2981 Add "SET TRUSTWORTHY ON" option on SP_DATABASERESTORE
2 parents 9659395 + 28949d9 commit 3a94b58

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

sp_DatabaseRestore.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ALTER PROCEDURE [dbo].[sp_DatabaseRestore]
2929
@SimpleFolderEnumeration BIT = 0,
3030
@SkipBackupsAlreadyInMsdb BIT = 0,
3131
@DatabaseOwner sysname = NULL,
32+
@SetTrustworthyON BIT = 0,
3233
@Execute CHAR(1) = Y,
3334
@Debug INT = 0,
3435
@Help BIT = 0,
@@ -1470,6 +1471,34 @@ IF @DatabaseOwner IS NOT NULL
14701471
END
14711472
END;
14721473

1474+
IF @SetTrustworthyON = 1
1475+
BEGIN
1476+
IF @RunRecovery = 1
1477+
BEGIN
1478+
IF IS_SRVROLEMEMBER('sysadmin') = 1
1479+
BEGIN
1480+
SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + N' SET TRUSTWORTHY ON;';
1481+
1482+
IF @Debug = 1 OR @Execute = 'N'
1483+
BEGIN
1484+
IF @sql IS NULL PRINT '@sql is NULL for SET TRUSTWORTHY ON';
1485+
PRINT @sql;
1486+
END;
1487+
1488+
IF @Debug IN (0, 1) AND @Execute = 'Y'
1489+
EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'ALTER DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y';
1490+
END
1491+
ELSE
1492+
BEGIN
1493+
PRINT 'Current user''s login is NOT a member of the sysadmin role. Database TRUSTWORHY bit has not been enabled.';
1494+
END
1495+
END
1496+
ELSE
1497+
BEGIN
1498+
PRINT @RestoreDatabaseName + ' is still in Recovery, so we are unable to enable the TRUSTWORHY bit.';
1499+
END
1500+
END;
1501+
14731502
-- If test restore then blow the database away (be careful)
14741503
IF @TestRestore = 1
14751504
BEGIN

0 commit comments

Comments
 (0)