ERP/SAP/R/3

Internal Table : READ, APPEND, MODIFY AND COLLECT

파란실버라이트 2011. 4. 6. 11:32
다음 프로그램을 디버깅하면서 이해할 수 있습니다.

  *&---------------------------------------------------------------------*
*& Report  ZTEST_it_01ERNAL_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTEST_it_01ERNAL_TABLE.

tables : kna1.

* Header가 있는 internal Table 선언
DATA BEGIN OF it_01 OCCURS 0,
  vbeln like vbak-vbeln,
  matnr type matnr,
  cnt type I,
  end of it_01.

* 특정 table과 같은 구조로 선언
data : it_02 like kna1 occurs with header line.

* it_01 Table에 data를 append한다.
it_01-vbeln = 'A'.
it_01-matnr = 'AA'.
it_01-cnt = 1.
append it_01.

it_01-matnr = 'AB'.
it_01-cnt = 2.
append it_01.

it_01-vbeln = 'B'.
it_01-matnr = 'BB'.
it_01-cnt = 3.
append it_01.

it_01-vbeln = 'C'.
it_01-matnr = 'CC'.
it_01-cnt = 4.
append it_01.
clear it_01.


write : /'it_01에 값을 확인'.
LOOP AT it_01.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.

write : /'int-vbeln = A 값을 확인'.
LOOP AT it_01 where vbeln = 'A'.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.

write : /'Read commend(with key) 사용 : IF sy-subrc = 0.을 사용하여 data를  확인'.
read table it_01 with key vbeln = 'A'.
  IF sy-subrc = 0.
      write : / it_01-vbeln,it_01-matnr,it_01-cnt.
  ENDIF.
write : /.

write : /'Read commend(index) 사용 : IF sy-subrc = 0.을 사용하여 data를  확인'.
read table it_01 index 3.
  IF sy-subrc = 0.
      write : / it_01-vbeln,it_01-matnr,it_01-cnt.
  ENDIF.
write : /.


* index가 3인 it_01에 insert
write : /'insert후  값을 확인'.
it_01-vbeln = 'D'.
it_01-matnr = 'DD'.
it_01-cnt = 5.
insert it_01 index 3.
  clear it_01.
  LOOP AT it_01.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.

* 기본 적으로 append와 같이 마지막에 추가되는 것은 같으나 이미 저장된 record중에 character type의 field를 비교하여
* 같은 값으로 이미 저장되어 있는 record가 있으면 수자타입의 필드는 기존의 필드에 현재 header에 있는 값은 더한다.

write : /'collect후   값을 확인'.
it_01-vbeln = 'A'.
it_01-matnr = 'AB'.
it_01-cnt = 2.
collect it_01.
  clear it_01.
  LOOP AT it_01.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.


* it_01의 index가 3인 record를 수정한다.
write : /'modify후   값을 확인'.
LOOP AT it_01.
  it_01-vbeln = 'Z'.
*  modify it_01.
  modify it_01 index 3.
  clear it_01.

ENDLOOP.

  LOOP AT it_01.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.

* it_01의 contents를 비운다.
  write : /'refresh 후   값을 확인'.
refresh it_01.
  LOOP AT it_01.
  write : / it_01-vbeln,it_01-matnr,it_01-cnt.
ENDLOOP.
write : /.

write /'end Program'.