IT WORK/SAP FI and ABAP

[ABAP] 인터널 테이블 여러줄을 한줄로 표시 Converting multiple rows to one row of an internal table in ABAP

Bathildis 2023. 4. 7. 11:18
반응형
REPORT YEDIT.

DATA: LT_DATA LIKE TABLE OF T000 WITH HEADER LINE.

DATA : D_TAB   TYPE REF   TO DATA,
       D_LINE  TYPE REF   TO DATA,
       TDD02L LIKE DD02L,
       NAMETAB LIKE DNTAB OCCURS 0 WITH HEADER LINE,
       LT_FCAT TYPE TABLE OF LVC_S_FCAT,
       LS_FCAT LIKE LINE  OF LT_FCAT.

DATA: LV_INDEX TYPE C LENGTH 10,
      LV_FIELD TYPE C LENGTH 30.

FIELD-SYMBOLS : <STRUC> TYPE ANY,
                <FROMF> TYPE ANY,
                <TOF>   TYPE ANY.

FIELD-SYMBOLS : <NEW_TAB>  TYPE TABLE,
                <NEW_LINE> TYPE ANY.

CLEAR: LT_DATA, LT_DATA[].
SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_DATA
  FROM T000.

DESCRIBE TABLE LT_DATA LINES SY-TFILL.

" 대상 테이블의 구조 가져오기 Get the structure of the target table
CLEAR: NAMETAB, NAMETAB[].
CALL FUNCTION 'NAMETAB_GET'
  EXPORTING
    LANGU          = SY-LANGU
    TABNAME        = 'T000'
  TABLES
    NAMETAB        = NAMETAB
  EXCEPTIONS
    NO_TEXTS_FOUND = 1.
SORT NAMETAB BY POSITION.

CLEAR : LT_FCAT[].

" 몇줄인지 확인해서 오른쪽으로 계속 붙여주기 위함
" Repeat number of rows times and keep making columns
DO SY-TFILL TIMES.
  CLEAR LV_INDEX.
  LV_INDEX = SY-INDEX.
  CONDENSE LV_INDEX NO-GAPS.
  LOOP AT NAMETAB.
    CONCATENATE NAMETAB-FIELDNAME LV_INDEX INTO LS_FCAT-FIELDNAME.
    LS_FCAT-REF_TABLE =  'T000'.
    LS_FCAT-REF_FIELD =  NAMETAB-FIELDNAME.
    APPEND  LS_FCAT  TO LT_FCAT.
  ENDLOOP.
ENDDO.

" 위에서 새로 만든 필드 카탈로그로 다이나믹 테이블 생성
" Create a dynamic table with the new field catalog that just has made avobe
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
  EXPORTING
    IT_FIELDCATALOG = LT_FCAT
  IMPORTING
    EP_TABLE        = D_TAB.

ASSIGN D_TAB->*  TO  <NEW_TAB>.
CREATE DATA D_LINE LIKE LINE OF <NEW_TAB>.
ASSIGN D_LINE->* TO <NEW_LINE>.

LOOP AT LT_DATA.

  CLEAR LV_INDEX.
  LV_INDEX = SY-TABIX.
  CONDENSE LV_INDEX NO-GAPS.

  LOOP AT NAMETAB.

    CLEAR LV_FIELD.
    CONCATENATE 'LT_DATA-' NAMETAB-FIELDNAME INTO LV_FIELD.
    ASSIGN (LV_FIELD) TO <FROMF>.
    IF SY-SUBRC EQ 0.
      CLEAR LV_FIELD.
      CONCATENATE NAMETAB-FIELDNAME LV_INDEX INTO LV_FIELD.
      ASSIGN COMPONENT LV_FIELD OF STRUCTURE <NEW_LINE> TO <TOF>.
      IF SY-SUBRC EQ 0.
        <TOF> = <FROMF>.
      ENDIF.
    ENDIF.
  ENDLOOP.

ENDLOOP.

APPEND <NEW_LINE> TO <NEW_TAB>.

 

 

LT_DATA의 구조는 다음과 같음

You can see the structure of LT_DATA below

000 SAP AG Walldorf DEM
200 Customizing Seoul KRW
210 Development Seoul KRW

 

 

다이나믹 테이블의 구조는 다음과 같음

You can see the structure of dynamic table below

000 SAP AG Walldorf DEM 200 Customizing Seoul KRW 210 Development Seoul KRW

 

반응형