알 수 없는 이유로 데이터가 꼬여서 덤프 에러 Exception condition "NO_KEY" triggered 발생하곤 합니다.

가비지 데이터 정리 프로그램 만들어 보았습니다.

 

코드:

 

*&---------------------------------------------------------------------*
*& Report ZMDP_ADJUST_STAGING
*&---------------------------------------------------------------------*
*& MDG staging table 가비지 데이터 삭제 처리 프로그램
*&---------------------------------------------------------------------*
REPORT zmdp_adjust_staging LINE-SIZE 250.

DATA : lt_message           TYPE usmd_t_message,
       ls_message           TYPE usmd_s_message,
       lt_model             TYPE TABLE OF usmd_model,
       lv_model             TYPE usmd_model,
       lo_model_gen_adapter TYPE REF TO if_usmd_model_gen_adapter,
       ls_log_phys_name     TYPE if_usmd_model_gen_adapter=>s_log_phys_name,
       lt_log_phys_name     TYPE if_usmd_model_gen_adapter=>t_log_phys_name,
       lv_entity            TYPE string,
       lv_select            TYPE string,
       lv_table             TYPE string,
       lv_key_field         TYPE string,
*       lt_range_key_value   TYPE RANGE OF usmd_tech_key,
       lt_key_value         TYPE TABLE OF usmd_tech_key,
       lv_key_value         TYPE usmd_tech_key,
       lv_usmd_value        TYPE usmd_value,
       lt_cr                TYPE TABLE OF usmd_crequest,
       lv_cr                TYPE usmd_crequest,
       lt_usmd1213          TYPE TABLE OF usmd1213,
       ls_usmd1213          TYPE usmd1213,
       lv_flag_del          TYPE flag,
       lv_where             TYPE string,
       lv_key_json          TYPE string,
       lr_data              TYPE REF TO data,
       lv_error             TYPE char1,
       lv_index             TYPE i.

FIELD-SYMBOLS: <lt_data> TYPE tabel,
               <ls_data> TYPE data,
               <lv_data> TYPE data.

*--------------------------------------------------------------------*
" 모델 검색
SELECT-OPTIONS so_model FOR lv_model MATCHCODE OBJECT zmdph_model.



*--------------------------------------------------------------------*
START-OF-SELECTION.

*--------------------------------------
  " 활성 상태의 모델정보 검색
  SELECT usmd_model
    INTO TABLE lt_model
    FROM usmd001c
   WHERE usmd_model IN so_model
     AND usmd_objstat = 'A'.
  "
  IF sy-subrc NE 0.
    " 레코드 검색 실패
    MESSAGE s000(oo) WITH 'Data not found.' DISPLAY LIKE 'E'.
    " 작업 종료
    STOP.
  ENDIF.

*--------------------------------------
  " 모델별 작업
  LOOP AT lt_model INTO lv_model.
    "
    WRITE:/ ' *** ' , lv_model COLOR 7, ' *** '.

    "*----------------------------
    " 모델 정보로 활성된 테이블 정보 검색
    CALL METHOD cl_usmd_adapter_provider=>get_model_generation_adapter
      EXPORTING
        i_usmd_model         = lv_model
      IMPORTING
        eo_model_gen_adapter = lo_model_gen_adapter
        et_message           = lt_message.

    CLEAR lv_error.
    "
    LOOP AT lt_message TRANSPORTING NO FIELDS WHERE msgty CA 'AEX'.
      "*----------------------------
      " 오륲 메세지 처리
      MESSAGE ID ls_message-msgid TYPE 'S' NUMBER ls_message-msgno DISPLAY LIKE ls_message-msgty
        WITH ls_message-msgv1 ls_message-msgv2 ls_message-msgv3 ls_message-msgv4.
      " 오류 발생
      lv_error = abap_true.
    ENDLOOP.

    "*----------------------------
    " 오류 없을시 작업 진행
    CHECK lv_error IS INITIAL.

    "*----------------------------
    " 활성 테이블 정보 검색
    CALL METHOD lo_model_gen_adapter->get_generated_objects
      EXPORTING
        if_mdf_hry_tab   = abap_false
      IMPORTING
        et_message       = lt_message
        et_log_phys_name = lt_log_phys_name.    " 활성 테이블 정보

    "*----------------------------
    "
    LOOP AT lt_log_phys_name INTO ls_log_phys_name.

      CASE ls_log_phys_name-sub_kind.
        WHEN usmd1_cs_tabl_kind-check.
          "
          lv_entity    = ls_log_phys_name-entity.
          lv_table     = ls_log_phys_name-phys_name.
          lv_key_field = 'USMDK' && lv_model && lv_entity.

          "
          WRITE:/ '*', lv_entity, lv_table.

          TRY .
              "*----------------------------
              " 테이블 키 밸류 검색
              SELECT (lv_key_field)
                FROM (lv_table)
                INTO TABLE @lt_key_value.
            CATCH cx_sy_dynamic_osql_semantics.
              CONTINUE.
          ENDTRY.

          "*----------------------------
          " 키 중복 제거
          SORT lt_key_value.
          DELETE ADJACENT DUPLICATES FROM lt_key_value.

          "*----------------------------
          " 키 밸류별 작업
          LOOP AT lt_key_value INTO lv_key_value.
            "
            CLEAR: lv_flag_del.
            "
            lv_usmd_value = lv_key_value.

            " 키에 매핑된 Change Request 정보 검색
            SELECT usmd_crequest
              INTO TABLE lt_cr
              FROM usmd1213
             WHERE usmd_entity     = lv_entity
               AND usmd_entity_obj = lv_entity
               AND usmd_value      = lv_usmd_value.
            IF sy-subrc <> 0.
              " 삭제 대상 설정
              lv_flag_del = abap_true.
            ENDIF.

            " Change Request 별 상태 검색
            LOOP AT lt_cr INTO lv_cr.
              SELECT SINGLE mandt
                INTO sy-mandt
                FROM usmd120c
               WHERE usmd_crequest = lv_cr
                 AND usmd_creq_status NOT IN (usmd0_cs_crequest_status-finally_approved,  " 05: 승인완료
                                              usmd0_cs_crequest_status-finally_rejected). " 06: 반려완료.
              IF sy-subrc <> 0.
                " 삭제 대상 설정
                lv_flag_del = abap_true.
                " 루프 종료
                EXIT.
              ENDIF.
            ENDLOOP. " LOOP AT lt_cr INTO lv_cr.

            "*----------------------------
            " 삭제 대상인 경우
            IF lv_flag_del EQ abap_true.
              "
              lv_where = |{ lv_key_field } = lv_key_value|.

*              lv_table = lt_log_phys_name[ entity   = lv_entity
*                                           sub_kind = usmd1_cs_tabl_kind-map ]-phys_name.
              READ TABLE lt_log_phys_name INTO ls_log_phys_name
               WITH KEY entity = lv_entity sub_kind = usmd1_cs_tabl_kind-map.
              lv_table = ls_log_phys_name-phys_name.
              "
              CREATE DATA lr_data TYPE (lv_table).
              ASSIGN lr_data->* TO <ls_data>.
              "
              lv_key_json = lv_usmd_value.
              SELECT SINGLE * INTO <ls_data>
                FROM (lv_table)
               WHERE (lv_where).
              "
              lv_key_json = /ui2/cl_json=>serialize( <ls_data> ).
              "
              WRITE:/ lv_key_json.

              "*----------------------------
              " 테이블별 레코드 삭제
              " 엔티티별 매핑 테이블이 아닌 테이블에 대해 작업
              LOOP AT lt_log_phys_name INTO ls_log_phys_name
                                      WHERE kind     = 'TABL'
                                        AND entity   = lv_entity
                                        AND sub_kind <> usmd1_cs_tabl_kind-map.
                "*----------------------------
                " 테이블 아이디 설정
                lv_table = ls_log_phys_name-phys_name.
                "*----------------------------
                " 테이블 레코드 삭제 처리
                DELETE FROM (lv_table) WHERE (lv_where).
              ENDLOOP.
            ENDIF.
          ENDLOOP. " LOOP AT lt_key_value INTO lv_key_value.


        WHEN usmd1_cs_tabl_kind-map.
          "
          lv_entity    = ls_log_phys_name-entity.
          lv_table     = ls_log_phys_name-phys_name.
          lv_key_field = 'USMDK' && lv_model && lv_entity.

          "
          WRITE:/ '*', lv_entity, lv_table, 'USMD1213'.

          TRY .
              "*----------------------------
              " 테이블 키 밸류 검색
*              lv_select = |'I' AS SIGN, 'EQ' AS OPTION, { lv_key_field } AS LOW|.
              lv_select = lv_key_field.
              SELECT (lv_select)
                FROM (lv_table)
*                INTO TABLE @lt_range_key_value.
                INTO TABLE @lt_key_value.
              SORT lt_key_value.
            CATCH cx_sy_dynamic_osql_semantics.
              CONTINUE.
          ENDTRY.

          " 키에 매핑 안된 Change Request 정보 검색
          SELECT *
            INTO TABLE lt_usmd1213
            FROM usmd1213
           WHERE usmd_entity     = lv_entity
             AND usmd_entity_obj = lv_entity.
*             AND usmd_value      NOT IN lt_range_key_value.

          LOOP AT lt_usmd1213 INTO ls_usmd1213.
            READ TABLE lt_key_value TRANSPORTING NO FIELDS WITH KEY table_line = ls_usmd1213-usmd_value BINARY SEARCH.
            IF sy-subrc EQ 0.
              DELETE lt_usmd1213.
            ELSE.
              lv_key_json = /ui2/cl_json=>serialize( ls_usmd1213 ).
              WRITE:/ lv_key_json.
            ENDIF.
          ENDLOOP.

          DELETE usmd1213 FROM TABLE lt_usmd1213.

        WHEN OTHERS.
      ENDCASE.

    ENDLOOP. " LOOP AT lt_log_phys_name INTO ls_log_phys_name

  ENDLOOP. " LOOP AT lt_model INTO lv_model.

*--------------------------------------
  " 커밋
  COMMIT WORK.

'ABAP > MDG 기준정보' 카테고리의 다른 글

ZCL_MDP_READ_HELPER  (0) 2021.12.15
MDG BP 생성/수정 CR 만드는 로직  (0) 2021.12.15

+ Recent posts