--Postgres v10 Version
CREATE OR REPLACE FUNCTION sp_Add_PersonId_to_private_report (
p_PersonId integer) RETURNS VOID

AS
'
DECLARE  MyCursor CURSOR FOR
              SELECT DISTINCT EntityId 
              FROM AccessLevel 
              WHERE EntityTypeCode = ''PRIVATEREPORT'';
        v_MyField Integer;

BEGIN

  OPEN MyCursor;
  	FETCH NEXT FROM MyCursor
	INTO v_MyField;

  WHILE (FOUND) LOOP
    INSERT INTO AccessLevel (EntityId, EntityTypeCode, AccessLevelCode, AccessSubjectCode, AccessGroupId)
	VALUES (v_MyField, ''PRIVATEREPORT'', ''EDIT'', ''PERSON'', p_PersonId);

	FETCH NEXT FROM MyCursor
	INTO v_MyField;
  END LOOP;

  CLOSE MyCursor;


END;
'
LANGUAGE plpgsql;
