CREATE OR REPLACE PROCEDURE rds.sp_exportfrllodataprocess(
    exportid uuid,
    infosyscode varchar(100),
    region varchar(5),
    withouterror boolean,
    OUT result text
)
    LANGUAGE plpgsql
AS
$$
DECLARE
    v_type                     varchar(100);
    p_exportid                 uuid         := exportid;
    p_infosyscode              varchar(100) := infosyscode;
    p_region                   varchar(5)   := region;
    p_withouterror             boolean      := withouterror;
    c_rr_patient               varchar(20)  := 'RR_PATIENT';
    c_bsk_register             varchar(20)  := 'BSK_REGISTER';
    c_palliative_register      varchar(30)  := 'PALLIATIVE_REGISTER';
    C_SPECIAL_BENEFITS_REGISTR varchar(50)  := 'SPECIAL_BENEFITS_REGISTR';

BEGIN
    CREATE TEMP TABLE temp_outputtable
    (
        val xml
    );
    
    SELECT ee.entname
    INTO v_type
    FROM rds.t_export_frllo e
    JOIN vclib.t_entities ee ON ee.entityid = e.entities
    WHERE e.exportfrlloid = p_exportid;
    
    IF (v_type = c_rr_patient) THEN
        INSERT INTO temp_outputtable(val)
        SELECT XMLELEMENT(
                   NAME "root",
                   XMLFOREST(
                       XMLELEMENT(NAME "info_sys_code", p_infosyscode),
                       XMLELEMENT(
                           NAME "documents",
                           (SELECT XMLAGG
                                   (
                                       XMLELEMENT
                                       (
                                           NAME "document",
                                           XMLFOREST(
                                               XMLELEMENT(NAME "document_id", efd.exportfrllodataid),
                                               XMLELEMENT(NAME "doc_date_time", ef.actualdate),
                                               (SELECT XMLELEMENT
                                                       (
                                                           NAME "citizen",
                                                           XMLFOREST
                                                           (
                                                               XMLELEMENT(NAME "ext_citizen_id", r.rrpatientid),
                                                               XMLELEMENT(NAME "name", r.firstname),
                                                               XMLELEMENT(NAME "surname", r.lastname),
                                                               XMLELEMENT(NAME "patronymic", r.fathername),
                                                               XMLELEMENT(NAME "firstsurname", r.birthsurname),
                                                               XMLELEMENT(NAME "birthdate", TO_CHAR(r.birthday, 'YYYY-MM-DD')),
                                                               XMLELEMENT(NAME "sex", esex.code),
                                                               XMLELEMENT(NAME "citizenship", 643),
                                                               XMLELEMENT(NAME "snils",
                                                                          REPLACE(REPLACE(r.snils, '-', ''), ' ', '')),
                                                               XMLELEMENT(NAME "policy_sn", r.enp),
                                                               XMLELEMENT(NAME "identify_docs",
                                                                          XMLELEMENT(
                                                                              NAME "doc",
                                                                              XMLFOREST
                                                                              (
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "doc_type",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN er.codeehr
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "serial",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docseries
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "num",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docnumber
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "date_issue",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN TO_CHAR(r.docissuedate, 'YYYY-MM-DD')
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "authority",
                                                                                      (CASE WHEN er.codeehr IS NOT NULL THEN r.docissueorg ELSE NULL END)
                                                                                  )
                                                                              )
                                                                          )
                                                               ),
                                                               XMLELEMENT(NAME "region", p_region)
                                                           )
                                                       )
                                                FROM rds.t_rr_patient r
                                                JOIN vclib.t_enums esex ON esex.enumid = r.sex
                                                LEFT JOIN ehr.t_ehr_rus_5o5e88vhc6 er
                                                    ON er.ehrrusid = r.ehrrus5o5e88vhc6 AND er.is_dul = 1
                                                WHERE r.id = efd.objid
                                               ),
                                               XMLELEMENT
                                               (
                                                   NAME "benefits",
                                                   (SELECT XMLAGG
                                                           (
                                                               XMLELEMENT(
                                                                   NAME "benefit",
                                                                   XMLFOREST(
                                                                       XMLELEMENT(NAME "benefit_code", ehrr.codeehr),
                                                                       XMLELEMENT(NAME "diagnosis", SUBSTR(rpb.mkb, 1, 6)),
                                                                       XMLELEMENT(NAME "receive_date",
                                                                                  TO_CHAR(rpb.begindate, 'YYYY-MM-DD')),
                                                                       XMLELEMENT(NAME "cancel_date", TO_CHAR(rpb.enddate, 'YYYY-MM-DD'))
                                                                   )
                                                               )
                                                           )
                                                    FROM rds.t_rr_patient_benefit rpb
                                                    JOIN rds.t_benefits b ON b.benefitsid = rpb.benefits
                                                    JOIN ehr.t_ehr_rus_sdo8u3n5ro ehrr ON ehrr.ehrrusid = b.ehrrussdo8u3n5ro
                                                    JOIN rds.t_export_frllo_data efdb
                                                        ON efdb.objid = rpb.rrpatientbenefitid AND
                                                           efdb.exportfrllo = efd.exportfrllo AND
                                                           ex.filenumber = efdb.filenumber AND
                                                           (p_withouterror = FALSE OR efdb.frlloerror IS NULL)
                                                    WHERE rpb.rrpatient = efd.objid
                                                   )
                                               )
                                           )
                                       )
                                   )
                            FROM rds.t_export_frllo_data efd
                            JOIN rds.t_export_frllo ef ON ef.exportfrlloid = efd.exportfrllo
                            WHERE efd.type = 0
                              AND ef.exportfrlloid = p_exportid
                              AND ex.filenumber = efd.filenumber
                              AND (p_withouterror = FALSE OR efd.frlloerror IS NULL)
                           )
                       )
                   )
               )
        FROM rds.t_export_frllo_data ex
        WHERE ex.type = 0
          AND ex.exportfrllo = p_exportid
          AND (p_withouterror = FALSE OR ex.frlloerror IS NULL)
        GROUP BY ex.filenumber;
    ELSIF (v_type = c_bsk_register) THEN
        
        INSERT INTO temp_outputtable(val)
        SELECT XMLELEMENT(
                   NAME "root",
                   XMLFOREST(
                       XMLELEMENT(NAME "info_sys_code", p_infosyscode),
                       XMLELEMENT(
                           NAME "documents",
                           (SELECT XMLAGG
                                   (
                                       XMLELEMENT
                                       (
                                           NAME "document",
                                           XMLFOREST(
                                               XMLELEMENT(NAME "document_id", efd.exportfrllodataid),
                                               XMLELEMENT(NAME "doc_date_time", ef.actualdate),
                                               (SELECT XMLELEMENT
                                                       (
                                                           NAME "citizen",
                                                           XMLFOREST
                                                           (
                                                               XMLELEMENT(NAME "ext_citizen_id", r.bskregisterid),
                                                               XMLELEMENT(NAME "name", r.firstname),
                                                               XMLELEMENT(NAME "surname", r.lastname),
                                                               XMLELEMENT(NAME "patronymic", r.fathername),
                                                               XMLELEMENT(NAME "birthdate", TO_CHAR(r.birthday, 'YYYY-MM-DD')),
                                                               XMLELEMENT(NAME "sex", esex.code),
                                                               XMLELEMENT(NAME "citizenship", 643),
                                                               XMLELEMENT(NAME "snils",
                                                                          REPLACE(REPLACE(r.snils, '-', ''), ' ', '')),
                                                               XMLELEMENT(NAME "policy_sn", r.enp),
                                                               XMLELEMENT(NAME "identify_docs",
                                                                          XMLELEMENT(
                                                                              NAME "doc",
                                                                              XMLFOREST
                                                                              (
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "doc_type",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN er.codeehr
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "serial",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docseries
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "num",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docnumber
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "date_issue",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN TO_CHAR(r.docissuedate, 'YYYY-MM-DD')
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "authority",
                                                                                      (CASE WHEN er.codeehr IS NOT NULL THEN r.docissueorg ELSE NULL END)
                                                                                  )
                                                                              )
                                                                          )
                                                               ),
                                                               XMLELEMENT(NAME "region", p_region)
                                                           )
                                                       )
                                                FROM rds.t_bsk_register r
                                                JOIN vclib.t_enums esex ON esex.enumid = r.sex
                                                LEFT JOIN ehr.t_ehr_rus_5o5e88vhc6 er
                                                    ON er.ehrrusid = r.ehrrus5o5e88vhc6 AND er.is_dul = 1
                                                WHERE r.bskregisterid = efd.objid
                                               ),
                                               XMLELEMENT
                                               (
                                                   NAME "benefits",
                                                   (SELECT XMLAGG
                                                           (
                                                               XMLELEMENT(
                                                                   NAME "benefit",
                                                                   XMLFOREST(
                                                                       XMLELEMENT(NAME "benefit_code", ehrr.codeehr),
                                                                       XMLELEMENT(NAME "diagnosis", SUBSTR(brd.mkb, 1, 6)),
                                                                       XMLELEMENT(NAME "receive_date",
                                                                                  TO_CHAR(brd.begindate, 'YYYY-MM-DD')),
                                                                       XMLELEMENT(NAME "cancel_date", TO_CHAR(brd.enddate, 'YYYY-MM-DD'))
                                                                   )
                                                               )
                                                           )
                                                    FROM rds.t_bsk_register_data brd
                                                    JOIN rds.t_benefits b ON b.benefitsid = brd.benefits
                                                    JOIN ehr.t_ehr_rus_sdo8u3n5ro ehrr ON ehrr.ehrrusid = b.ehrrussdo8u3n5ro
                                                    JOIN rds.t_export_frllo_data efdb
                                                        ON efdb.objid = brd.bskregisterdataid AND
                                                           efdb.exportfrllo = efd.exportfrllo AND
                                                           ex.filenumber = efdb.filenumber AND
                                                           (p_withouterror = FALSE OR efdb.frlloerror IS NULL)
                                                    WHERE brd.bskregister = efd.objid
                                                   )
                                               )
                                           )
                                       )
                                   )
                            FROM rds.t_export_frllo_data efd
                            JOIN rds.t_export_frllo ef ON ef.exportfrlloid = efd.exportfrllo
                            WHERE efd.type = 0
                              AND ef.exportfrlloid = p_exportid
                              AND ex.filenumber = efd.filenumber
                              AND (p_withouterror = FALSE OR efd.frlloerror IS NULL)
                           )
                       )
                   )
               )
        FROM rds.t_export_frllo_data ex
        WHERE ex.type = 0
          AND ex.exportfrllo = p_exportid
          AND (p_withouterror = FALSE OR ex.frlloerror IS NULL)
        GROUP BY ex.filenumber;
    
    ELSIF (v_type = c_palliative_register) THEN
        
        INSERT INTO temp_outputtable(val)
        SELECT XMLELEMENT(
                   NAME "root",
                   XMLFOREST(
                       XMLELEMENT(NAME "info_sys_code", p_infosyscode),
                       XMLELEMENT(
                           NAME "documents",
                           (SELECT XMLAGG
                                   (
                                       XMLELEMENT
                                       (
                                           NAME "document",
                                           XMLFOREST(
                                               XMLELEMENT(NAME "document_id", efd.exportfrllodataid),
                                               XMLELEMENT(NAME "doc_date_time", ef.actualdate),
                                               (SELECT XMLELEMENT
                                                       (
                                                           NAME "citizen",
                                                           XMLFOREST
                                                           (
                                                               XMLELEMENT(NAME "ext_citizen_id", r.palliativeregisterid),
                                                               XMLELEMENT(NAME "name", r.firstname),
                                                               XMLELEMENT(NAME "surname", r.lastname),
                                                               XMLELEMENT(NAME "patronymic", r.fathername),
                                                               XMLELEMENT(NAME "birthdate", TO_CHAR(r.birthday, 'YYYY-MM-DD')),
                                                               XMLELEMENT(NAME "sex", esex.code),
                                                               XMLELEMENT(NAME "citizenship", 643),
                                                               XMLELEMENT(NAME "snils",
                                                                          REPLACE(REPLACE(r.snils, '-', ''), ' ', '')),
                                                               XMLELEMENT(NAME "policy_sn", r.enp),
                                                               XMLELEMENT(NAME "identify_docs",
                                                                          XMLELEMENT(
                                                                              NAME "doc",
                                                                              XMLFOREST
                                                                              (
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "doc_type",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN er.codeehr
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "serial",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docseries
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "num",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docnumber
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "date_issue",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN TO_CHAR(r.docissuedate, 'YYYY-MM-DD')
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "authority",
                                                                                      (CASE WHEN er.codeehr IS NOT NULL THEN r.docissueorg ELSE NULL END)
                                                                                  )
                                                                              )
                                                                          )
                                                               ),
                                                               XMLELEMENT(NAME "region", p_region)
                                                           )
                                                       )
                                                FROM rds.t_palliative_register r
                                                JOIN vclib.t_enums esex ON esex.enumid = r.sex
                                                LEFT JOIN ehr.t_ehr_rus_5o5e88vhc6 er
                                                    ON er.ehrrusid = r.ehrrus5o5e88vhc6 AND er.is_dul = 1
                                                WHERE r.palliativeregisterid = efd.objid
                                               ),
                                               XMLELEMENT
                                               (
                                                   NAME "benefits",
                                                   (SELECT XMLAGG
                                                           (
                                                               XMLELEMENT(
                                                                   NAME "benefit",
                                                                   XMLFOREST(
                                                                       XMLELEMENT(NAME "benefit_code", ehrr.codeehr),
                                                                       XMLELEMENT(NAME "diagnosis", SUBSTR(rpb.mkb, 1, 6)),
                                                                       XMLELEMENT(NAME "receive_date",
                                                                                  TO_CHAR(rpb.begindate, 'YYYY-MM-DD')),
                                                                       XMLELEMENT(NAME "cancel_date", TO_CHAR(rpb.enddate, 'YYYY-MM-DD'))
                                                                   )
                                                               )
                                                           )
                                                    FROM rds.t_palliative_register_data rpb
                                                    JOIN rds.t_benefits b ON b.benefitsid = rpb.benefits
                                                    JOIN ehr.t_ehr_rus_sdo8u3n5ro ehrr ON ehrr.ehrrusid = b.ehrrussdo8u3n5ro
                                                    JOIN rds.t_export_frllo_data efdb
                                                        ON efdb.objid = rpb.palliativeregisterdataid AND
                                                           efdb.exportfrllo = efd.exportfrllo AND
                                                           ex.filenumber = efdb.filenumber AND
                                                           (p_withouterror = FALSE OR efdb.frlloerror IS NULL)
                                                    WHERE rpb.palliativeregister = efd.objid
                                                   )
                                               )
                                           )
                                       )
                                   )
                            FROM rds.t_export_frllo_data efd
                            JOIN rds.t_export_frllo ef ON ef.exportfrlloid = efd.exportfrllo
                            WHERE efd.type = 0
                              AND ef.exportfrlloid = p_exportid
                              AND ex.filenumber = efd.filenumber
                              AND (p_withouterror = FALSE OR efd.frlloerror IS NULL)
                           )
                       )
                   )
               )
        FROM rds.t_export_frllo_data ex
        WHERE ex.type = 0
          AND ex.exportfrllo = p_exportid
          AND (p_withouterror = FALSE OR ex.frlloerror IS NULL)
        GROUP BY ex.filenumber;
    
    ELSIF (v_type = C_SPECIAL_BENEFITS_REGISTR) THEN
    
            INSERT INTO temp_outputtable(val)
        SELECT XMLELEMENT(
                   NAME "root",
                   XMLFOREST(
                       XMLELEMENT(NAME "info_sys_code", p_infosyscode),
                       XMLELEMENT(
                           NAME "documents",
                           (SELECT XMLAGG
                                   (
                                       XMLELEMENT
                                       (
                                           NAME "document",
                                           XMLFOREST(
                                               XMLELEMENT(NAME "document_id", efd.exportfrllodataid),
                                               XMLELEMENT(NAME "doc_date_time", ef.actualdate),
                                               (SELECT XMLELEMENT
                                                       (
                                                           NAME "citizen",
                                                           XMLFOREST
                                                           (
                                                               XMLELEMENT(NAME "ext_citizen_id", r.SpecialBenefitsRegistrID),
                                                               XMLELEMENT(NAME "name", r.firstname),
                                                               XMLELEMENT(NAME "surname", r.lastname),
                                                               XMLELEMENT(NAME "patronymic", r.fathername),
                                                               XMLELEMENT(NAME "firstsurname", r.BirthdayLastName),
                                                               XMLELEMENT(NAME "birthdate", TO_CHAR(r.birthday, 'YYYY-MM-DD')),
                                                               XMLELEMENT(NAME "sex", esex.code),
                                                               XMLELEMENT(NAME "citizenship", 643),
                                                               XMLELEMENT(NAME "snils",
                                                                          REPLACE(REPLACE(r.snils, '-', ''), ' ', '')),
                                                               XMLELEMENT(NAME "policy_sn", r.enp),
                                                               XMLELEMENT(NAME "identify_docs",
                                                                          XMLELEMENT(
                                                                              NAME "doc",
                                                                              XMLFOREST
                                                                              (
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "doc_type",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN er.codeehr
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "serial",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docseries
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "num",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN r.docnumber
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "date_issue",
                                                                                      (CASE
                                                                                           WHEN
                                                                                               er.codeehr IS NOT NULL AND
                                                                                               r.docnumber IS NOT NULL AND
                                                                                               r.docissuedate IS NOT NULL
                                                                                               THEN TO_CHAR(r.docissuedate, 'YYYY-MM-DD')
                                                                                           ELSE NULL
                                                                                       END)
                                                                                  ),
                                                                                  XMLELEMENT
                                                                                  (
                                                                                      NAME "authority",
                                                                                      (CASE WHEN er.codeehr IS NOT NULL THEN r.docissueorg ELSE NULL END)
                                                                                  )
                                                                              )
                                                                          )
                                                               ),
                                                               XMLELEMENT(NAME "region", p_region)
                                                           )
                                                       )
                                                FROM RDS.T_SPECIAL_BENEFITS_REGISTR r
                                                JOIN vclib.t_enums esex ON esex.enumid = r.sex
                                                LEFT JOIN ehr.t_ehr_rus_5o5e88vhc6 er
                                                    ON er.ehrrusid = r.ehrrus5o5e88vhc6 AND er.is_dul = 1
                                                WHERE r.SpecialBenefitsRegistrID = efd.objid
                                               ),
                                               XMLELEMENT
                                               (
                                                   NAME "benefits",
                                                   (SELECT XMLAGG
                                                           (
                                                               XMLELEMENT(
                                                                   NAME "benefit",
                                                                   XMLFOREST(
                                                                       XMLELEMENT(NAME "benefit_code", ehrr.codeehr),
                                                                       XMLELEMENT(NAME "diagnosis", SUBSTR(r.mkb, 1, 6)),
                                                                       XMLELEMENT(NAME "receive_date",
                                                                                  TO_CHAR(r.DateOfDeclusion, 'YYYY-MM-DD')),
                                                                       XMLELEMENT(NAME "cancel_date", TO_CHAR(r.DateOfDeclusion, 'YYYY-MM-DD'))
                                                                   )
                                                               )
                                                           )
                                                    FROM rds.T_SPECIAL_BENEFITS_REGISTR r
                                                    JOIN rds.t_benefits b ON b.benefitsid = r.benefits
                                                    JOIN ehr.t_ehr_rus_sdo8u3n5ro ehrr ON ehrr.ehrrusid = b.ehrrussdo8u3n5ro
                                                    JOIN rds.t_export_frllo_data efdb
                                                        ON efdb.objid = r.SpecialBenefitsRegistrID  AND
                                                           efdb.exportfrllo = efd.exportfrllo AND
                                                           ex.filenumber = efdb.filenumber AND
                                                           (p_withouterror = FALSE OR efdb.frlloerror IS NULL)
                                                    WHERE r.SpecialBenefitsRegistrID  = efd.objid
                                                   )
                                               )
                                           )
                                       )
                                   )
                            FROM rds.t_export_frllo_data efd
                            JOIN rds.t_export_frllo ef ON ef.exportfrlloid = efd.exportfrllo
                            WHERE efd.type = 0
                              AND ef.exportfrlloid = p_exportid
                              AND ex.filenumber = efd.filenumber
                              AND (p_withouterror = FALSE OR efd.frlloerror IS NULL)
                           )
                       )
                   )
               )
        FROM rds.t_export_frllo_data ex
        WHERE ex.type = 0
          AND ex.exportfrllo = p_exportid
          AND (p_withouterror = FALSE OR ex.frlloerror IS NULL)
        GROUP BY ex.filenumber;
    
    END IF;
    
    SELECT XMLELEMENT(NAME "root_list", XMLAGG(val))::text
    INTO result
    FROM temp_outputtable;

END;
$$