Skip to content

Commit 79faa96

Browse files
authored
Fix for Issue #3
This fixes issue #3 : When running the removeAllUsersFromDB proc, it will throw an error if it tries to remove a user that is the owner of a schema. This was fixed by resetting any "non-dbo" schema owner back to "dbo". Since all users are going to be dropped anyway, this is necessary to 1) prevent the error, and 2) ensure that the schema and any database objects (tables, procs, etc...) possibly assigned to the schema remain intact.
1 parent 8cf2073 commit 79faa96

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

permissions-manager-install.sql

+25
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,31 @@ BEGIN
12231223
PRINT ''Server: '' + @@servername
12241224
PRINT ''Database: '' + DB_NAME()
12251225
1226+
--clean up schemas before trying to drop users
1227+
DECLARE SchemaResetCursor CURSOR LOCAL FAST_FORWARD FOR
1228+
SELECT name
1229+
FROM sys.schemas
1230+
WHERE SCHEMA_ID BETWEEN 5 AND 1000
1231+
AND principal_id <> 1
1232+
1233+
1234+
OPEN SchemaResetCursor
1235+
FETCH NEXT FROM SchemaResetCursor INTO @UserID
1236+
WHILE @@FETCH_STATUS = 0
1237+
BEGIN
1238+
IF EXISTS (SELECT * FROM sys.schemas WHERE name = @UserID)
1239+
BEGIN
1240+
SELECT @SQLstmt = ''ALTER AUTHORIZATION ON SCHEMA:: ['' + @UserID + ''] TO dbo;''
1241+
PRINT @SQLstmt
1242+
EXEC (@SQLstmt)
1243+
END
1244+
FETCH NEXT FROM SchemaResetCursor INTO @UserID
1245+
END
1246+
1247+
CLOSE SchemaResetCursor
1248+
DEALLOCATE SchemaResetCursor
1249+
1250+
12261251
--avoid dropping users that were creating using certificates
12271252
DECLARE DropUserCursor CURSOR LOCAL FAST_FORWARD FOR
12281253
SELECT p.name FROM sys.database_principals p

0 commit comments

Comments
 (0)