본문 바로가기

ABAP 물타기/Report

Tip : Dynamic Internal Table (동적 인터널테이블, Use 'INSERT REPORT')


  • Create Date   : 2010-06-19
  • Change Date : 2010-06-19

잡담 -ㅂ-;

어제 회식하기전에 모닝케어를 먹었더니 쌩쌩하구나~
커피 잘 마셔주는 south dragon 군에게 이 글을 바칩니다. ㅋ
사례로 과일천ca 1.5L를 증정받았습니다. 캬캬 


Overview

동적 internal table은 'INSERT REPORT' 구문을 사용해서 생성한다.
두 개의 Report가 필요하다.
첫 째는 데이터를 추출하고 dynamic table을 생성할 Report
둘 째는 데이터를 가공해서 dynamic table에 담는 Report

INSERT REPORT

Repository에 ABAP 프로그램을 생성하는 명령어이다.
생성하는 프로그램이 존재하면 덮어 써버린다.

EXPORT/IMPORT

Report 간 데이터를 주거니 받거니 할때 사용한다.


Report1 : ZEDU012

  REPORT  ZEDU012.

types : begin of type1,
          kunnr type bsid-kunnr,
          hkont type bsid-hkont,
          dmbtr type bsid-dmbtr,
        end of type1.

types : begin of type2,
          text(72type c,
        end of type2.

types : begin of type3,
          hkont type bsid-hkont,
        end of type3.

* base data
data : gt_01 type table of type1 with header line.

* create itab.
data : gt_02 type table of type2 with header line.

* account group
data : gt_03 type table of type3 with header line.

data : l_text(72type c,
       l_count(2type c.

* start report
START-OF-SELECTION.
  perform get_data.
  perform set_table.


FORM GET_DATA .

  select kunnr hkont sum( dmbtr ) as dmbtr
  into CORRESPONDING FIELDS OF TABLE gt_01
  from bsid
  where budat between '20100501' and '20100630'
  group by kunnr hkont
  .

  select hkont
  into CORRESPONDING FIELDS OF TABLE gt_03
  from bsid
  where budat between '20100501' and '20100630'
  group by hkont
  .


ENDFORM.                    " GET_DATA


FORM SET_TABLE .

  clear : l_count, l_text, gt_02, gt_02[].
  sort gt_01 by kunnr hkont.

  gt_02-text = '* Dynamic internal table from ZEDU012 *'.
  append gt_02.

  gt_02-text = `data : begin of gt_dyna occurs 0,`.
  append gt_02.

  gt_02-text = `  kunnr like bsid-kunnr,`.
  append gt_02.

* account 별 column 생성
  loop at gt_03.
    clear : l_text.
    l_count = l_count + 1.
    concatenate `  hkont` l_count ` like bsid-hkont,` into l_text.
    gt_02-text = l_text.
    append gt_02.
  endloop.

  gt_02-text = `    end of gt_dyna.`.
  append gt_02.

* dynamic itab create.
  insert report 'ZEDU013DYN' from gt_02.
  export gt_01 to memory id 'ZEDU012_1'.
  export gt_03 to memory id 'ZEDU012_3'.

  call transaction 'ZEDU013'.

ENDFORM.                    " SET_TABLE



Report2 : ZEDU013

다시 한 번 말하지만, ZEDU013DYN include는 생성해야한다.
임시적으로 gt_dyna internal table 을 생성하기 바란다. (Active 하기 위해서)
그리고 결과 값이 제대로 안 나온다.
KUNNR 과 컬럼이름을 변경하는 것은 숙제로 남겨둔다. (절대 귀찮아서가 아니다)

  REPORT  ZEDU013.

* 동적 itab이 ZEDU012 레포트에 의해 동적으로 생성됨.
* 최초 개발시에는 Active를 위해서 같은 이름의 dummy itab을 생성하기 바람.
include ZEDU013DYN.

type-pools : slis.  "function alv
data : gt_fieldcat type slis_t_fieldcat_alv,
       gs_layout   type slis_layout_alv.

* import data from 'ZEDU012'
types : begin of type1,
          kunnr type bsid-kunnr,
          hkont type bsid-hkont,
          dmbtr type bsid-dmbtr,
        end of type1.

types : begin of type3,
          hkont type bsid-hkont,
        end of type3.

* base data
data : gt_01 type table of type1 with header line.

* account group
data : gt_03 type table of type3 with header line.

* start report.
START-OF-SELECTION.
  perform set_data.
  perform create_alv.


FORM SET_DATA .

  field-symbols : <f>.
  data : l_field(72type c,
         l_count(2type c,
         l_index(2type c.

* import data
  import gt_01 from memory id 'ZEDU012_1'" 데이터
  import gt_03 from memory id 'ZEDU012_3'" 컬럼이름 모음

  sort gt_01 by kunnr hkont.

  loop at gt_01.

* 값이 없는 셀이 있을경우 숫자형오류가 발생함, 기본값 0을 셋팅해줌.
    at new kunnr.
      clear : l_count.
      loop at gt_03.
        l_count = l_count + 1.
        concatenate `gt_dyna-hkont` l_count into l_field.
        assign (l_field) to <f>.
        <f> = 0.
      endloop.
    endat.

* column 이름이 들어있는 테이블의 index를 구해서
* 같은 위치의 column에 값을 넣어줌.
* 이해가 가는지? ㅎㅎㅎ
    read table gt_03 with key hkont = gt_01-hkont.
    if sy-subrc = 0.
      l_index = sy-tabix.
      concatenate `gt_dyna-hkont` l_index into l_field.
      assign (l_field) to <f>.
      <f> = gt_01-dmbtr.
    endif.

    on change of gt_01-kunnr.
      gt_dyna-kunnr = gt_01-kunnr.
      append gt_dyna.
    endon.
  endloop.

ENDFORM.                    " SET_DATA


FORM CREATE_ALV .

* alv 발로 짰음.
* class 써서 컬럼 주고 해보기 바람.
* kunnr 과 account 컬럼 이름을 gt_03 을 Looping 돌면서 fieldcat. 생성바람.

  call function 'REUSE_ALV_FIELDCATALOG_MERGE'
    exporting
      i_program_name = sy-cprog
      i_internal_tabname = 'GT_DYNA'
      i_inclname = sy-cprog
    changing
      ct_fieldcat = gt_fieldcat.

   gs_layout-colwidth_optimize = 'X'.

   call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      is_layout = gs_layout
      it_fieldcat = gt_fieldcat
    tables
      t_outtab = gt_dyna.
ENDFORM.                    " CREATE_ALV



Result

결과 데이터에 2가지 account 밖에 없어서 column이 2개가 되었다.
거래처별 account 별 금액이 되시겠다.
실행은 ZEDU012 에서 한다. 거기서 ZEDU013 Report를 호출 한다.



'ABAP 물타기 > Report' 카테고리의 다른 글

Search Help  (0) 2010.07.11
Select CHECKBOX  (0) 2010.07.04
Custom Possible Entry  (0) 2010.07.04
BDC : QUAN Field 처리  (0) 2010.07.04
Tip : Dynamic Internal Table (동적 인터널테이블, Use fieldcatalog)  (1) 2010.06.21