CREATE PROCEDURE dbo.GDPRAddMissingDbUser AS
/*
  Created By Kent Johansen November 2017
  Returns true if db and server is ready for GDPR
*/
DECLARE @Containment BIT
EXEC GDPRModeIsSet @Containment OUTPUT, 0

IF @Containment = 0
BEGIN
  EXEC GDPRSetPartialMode
  EXEC GDPRModeIsSet @Containment OUTPUT, 0
END

IF @Containment = 1
BEGIN
  DECLARE @UserIdCode VARCHAR(20)

  DECLARE #Cur CURSOR LOCAL FAST_FORWARD FOR
    SELECT UserIdCode
    FROM Personel WITH (NOLOCK)
    WHERE UserIdCode COLLATE DATABASE_DEFAULT NOT IN (
      SELECT Name COLLATE DATABASE_DEFAULT FROM sys.database_principals WHERE Type = 'S'
    )
  OPEN #Cur

  FETCH NEXT FROM #CUR INTO @UserIdCode

  WHILE @@FETCH_STATUS = 0
  BEGIN
    EXEC GDPRCreateDbUser @UserIdCode
    FETCH NEXT FROM #CUR INTO @UserIdCode
  END

  CLOSE #Cur
  DEALLOCATE #Cur
END