다른 언어나 DB를 배우고 SAP에 접근하는 사람에게,
아밥에서의 internal Table의 이해는 가장 어려운 부분 인것 같다.
아래에서 사용한 예제를 다시 사용하였다.
REPORT Z0818_INTERNALTABLE.
DATA: BEGIN OF line,
col1 TYPE i,
col2 TYPE i,
END OF line.
DATA: itab LIKE STANDARD TABLE OF line ,
"itab like line occurs 0 with header line,
jtab LIKE sorted table of line WITH NON-UNIQUE KEY col1.
"occurs n을 Begin of..의 구조체와
"Data .. Like..에 써주는 순간
"Internal Table이 된다.
"Internal Tabele로 선언되어야
"Append나 Insert가 가능해진다.
"헤더를 주기 위해 with header line
"을 줄때는 그 앞에 occurs n이 필요.
DO 3 TIMES.
line-col1 = sy-index. line-col2 = sy-index ** 2.
APPEND line TO itab.
line-col1 = sy-index. line-col2 = sy-index ** 3.
APPEND line TO jtab.
ENDDO.
INSERT LINES OF itab INTO TABLE jtab.
LOOP AT jtab into line.
WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.
--------------------------------------------------------------------------------------
DATA: BEGIN OF line,
col1 TYPE i,
col2 TYPE i,
END OF line.
위는 그냥 구조체다.
위 구조체가 테이블이 되기 위해서는 occurs n을 붙여야 하는 것이다.
단지 occurs n을 붙여 테이블이 되지만, wa가 된다.(Wora Area)
즉 헤더가 없는 것이다.
헤더가 없으면 Loop a으t로 바로 값을 헤더에 올려 편집이 불가능하므로
(Loop at itab into wa를 사용해야한다.)
프로그래밍에선 주로 wa(헤더가 없는 Internal Table)가 아닌 헤더가 있는 internal Table을 쓰게 된다.
DATA: itab LIKE STANDARD TABLE OF line ,
"itab like line occurs 0 with header line,
jtab LIKE sorted table of line WITH NON-UNIQUE KEY col1.
헤더를 주기 위해 with header line 을 줄때는 그 앞에 occurs n이 필요하다.
Occurs n이란 테이블의 row를 설정하는 것인데, 별로 의미는 없지만 사용해 주어야 internal Table이 될 수 있다. 의미가 없는 이유는 필요한 때마다 row가 자동으로 설정이 되기 때문이다.
그래서 웬만해서는 0로 선언해서 메모리를 처음에 줄이는 쪽으로 사용한다.
occurs n 선언말고 테이블이 되는 또 다른 방법은 위처럼 data itab like ...table 형식으로 선언하는 것이다.
여기는 5가지의 테이블 양식이 있다.
standard, sorted, hashed, index, any..
TABKIND - Internal Table Types
Alternatives:
1. STANDARD TABLE
2. SORTED TABLE
3. HASHED TABLE
4. INDEX TABLE
5. ANY TABLE
Effect
The table type - set in the DATA, TYPES, orCREATE DATA statement, specifies how the system accesses entries in the internal table in generic key operations.
(READ TABLE itab, DELETE TABLE itab, INSERT TABLE itab, MODIFY TABLE itab, COLLECT itab). As a general rule, the runtime required for key operations depends on the total length of the key.
The various table types have the following hierarchy:
ANY TABLE
|
---------------------
| |
INDEX TABLE HASHED TABLE
|
---------------------
| |
STANDARD TABLE SORTED TABLE
Alternative 1
STANDARD TABLE
Effect
Defines the table as a standard table. Key access to a standard table uses a linear search. This means that the timne required for a search is in linear relation to the number of table entries.
You should use index operations to access standard tables.
For the sake of compatibility, you can use TABLE as a synonym of STANDARD TABLE.
Alternative 2
SORTED TABLE
Effect
Defines the table as one that is always saved correctly sorted. Key access to a sorted table uses a binary key. If the key is not unique, the system takes the entry with the lowest index. The runtime required for key access is logarithmically related to the number of table entries.
You can also access sorted tables by index operations. When you insert using an index, the system checks to ensure that the sort sequence has been correctly maintained. For this reason, it takes longer than inserting entries in a standard table. As a rule, you should only access sorted tables using their key.
Alternative 3
HASHED TABLE
Effect
Defines the table as one that is managed with an internal hash procedure. You can imagine a hashed table as a set, whose elements you can address using their unique key. Unlike standard and sorted tables, you cannot access hash tables using an index. All entries in the table must have a unique key. Access time using the key is constant, regardless of the number of table entries.
You can only access a hashed table using the generic key operations or other generic operations ( SORT, LOOP, and so on). Explicit or implicit index operations (such as LOOP ... FROM oe INSERT itab within a LOOP) are not allowed.
Alternative 4
INDEX TABLE
Effect
Standard and sorted tables belong to the generic class index tables. An index table is one that you can access using an index. You can currently only use the table type INDEX TABLE to specify the type of generic parameters in a FORM or a FUNCTION. Hashed tables are not index tables, and cannot therefore be passed to parameters defined as INDEX TABLE.
Alternative 5
ANY TABLE
Effect
Like INDEX TABLE, you use ANY TABLE to specify the type of any generic table parameter. The set of permitted operations for a table with type ANY TABLE consists of the intersection of all permitted operations for STANDARD, SORTED and HASHED TABLEs, and so is identical to the set of operations permitted for hashed tables.
Note in particular that you cannot use index access for tables with this type.