08_abap - internal tables
TRANSCRIPT
-
7/30/2019 08_ABAP - Internal Tables
1/32
ABAP - Internal Tables
-
7/30/2019 08_ABAP - Internal Tables
2/32
Contents
Objectives
Internal Table Basics
Definition of an Internal Table
APPEND Statement
Reading Data from an Internal Table
Sorting the Contents of an Internal Table
Summary
Q&A
-
7/30/2019 08_ABAP - Internal Tables
3/32
Contents
Objectives
-
7/30/2019 08_ABAP - Internal Tables
4/32
Objectives
Define an internal table:
with or without a header line
using the DATA and INCLUDE STRUCTURE statements.
Fill an internal table:
using APPEND via a header line or an explicit work area.
Use LOOP AT and LOOP AT...WHERE
Use SY-TABIX to determine the current row index.
Locate a single row in an internal table: using READ TABLE.
Sort internal tables.
-
7/30/2019 08_ABAP - Internal Tables
5/32
Contents
Internal Table Basics
-
7/30/2019 08_ABAP - Internal Tables
6/32
Internal Table
internal table: a temporary table stored in RAM on theapplication server
created and filled by a program during execution
discarded when the program ends
consists of one or more rows with an identical structure
consists of a bodyand an optional header line
body: holds the rows of the internal table (itab)
all rows have the same structure header line: a field string with the same structure as a
row
the single row buffer of the itab
-
7/30/2019 08_ABAP - Internal Tables
7/32
Internal Table
OCCURS: to create the body of itab
no OCCURS field string
Itab with header line: use BEGIN OF orWITH HEADER LINE
A nested internal table cannot have aheader line
DATA: BEGIN OF it1 OCCURS 10, "has a header line
f1,
f2,
f3,
END OF it1.
DATA it2 LIKE lfa1 OCCURS 100. "doesn't have a header line
DATA it3 LIKE lfa1 OCCURS 100 WITH HEADER LINE. "it does now
-
7/30/2019 08_ABAP - Internal Tables
8/32
Internal Table
the body and the header line have the same name
Which is referred?
Where in code?
In assignment statements header line
it-f1 = A
field f1 of header line it
-
7/30/2019 08_ABAP - Internal Tables
9/32
Internal Table - Examples*header line
DATA: BEGIN OF it1 OCCURS 10,
f1,
f2,
f3,
END OF it1.
*no header line
DATA it2 LIKE lfa1 OCCURS 100.
*header line
DATA it3 LIKE lfa1 OCCURS 100 WITH HEADER
LINE.
*no header line
DATA it4 LIKE it3 OCCURS 100.
*header line
DATA BEGIN OF it5 OCCURS 100.
INCLUDE STRUCTURE lfa1.
DATA END OF it5.
* might use
DATA BEGIN OF it6 OCCURS 100.
INCLUDE STRUCTURE lfa1.
DATA: delflag,
rowtotal,
END OF it6.
*don't do it this way
DATA: BEGIN OF it7 OCCURS 100,s LIKE lfa1, "component names will be
END OF it7. "prefixed with it7-s-
DATA it8 LIKE sy-index OCCURS 10
WITH HEADER LINE.
DATA: BEGIN OF it9 OCCURS 10,
f1 LIKE sy-index,
END OF it9.
-
7/30/2019 08_ABAP - Internal Tables
10/32
APPEND Statement
to add a single row to a internal table
copy 1 row: from work area body (append to the end)
work area can be:
the header line implicit
other field string having the same structure explicit
Syntax: append [wa to] [initial line to] it.
wa is the name of a work area
itis the name of a previously defined internal table
SY-TABIX the relative row number of the rowappended
APPEND INITIAL LINE TO it appends an initial row
-
7/30/2019 08_ABAP - Internal Tables
11/32
APPEND Statement
DATA: BEGIN OF it OCCURS 3,
f1(1),
f2(2),
END OF it.
it-f1 = 'A'.
it-f2 = 'XX'.
APPEND it TO it.
WRITE: / 'sy-tabix =', sy-tabix.
it-f1 = 'B'.
it-f2 = 'YY'.
APPEND it.
WRITE: / 'sy-tabix =', sy-tabix.
it-f1 = 'C'.
APPEND it.
WRITE: / 'sy-tabix =', sy-tabix.
-
7/30/2019 08_ABAP - Internal Tables
12/32
Using the OCCURS Addition
How much memory to allocate in 1 time?
OCCURS 0 size of memory = 8KB (fixed)
OCCURS n
size of memory = min(sizeof(n lines),8KB)
Dont use OCCURS 0 if the expected itabsize
-
7/30/2019 08_ABAP - Internal Tables
13/32
Reading Data from an Internal Table
2 statements:
LOOP AT: to read multiple rows
READ TABLE: to read a single row
-
7/30/2019 08_ABAP - Internal Tables
14/32
LOOP AT Statement
Syntax: loop at it[into wa] [from m] [to n] [where exp].
---
endloop.
where: itis the name of an internal table.
wa is the name of a work area.
m and n are integer literals, constants, or variables representinga relative row number. For example, 1 means the first row in the
table, 2 means the second, and so on. exp is a logical expression restricting the rows that are read.
--- represents any number of lines of code. These lines areexecuted once for each row retrieved from the internal table.
-
7/30/2019 08_ABAP - Internal Tables
15/32
LOOP AT Statement
SY-TABIX:
contains the row number of the current record
1st record SY-TABIX = 1; 2nd record SY-TABIX = 2;
after ENDLOOP: SY-TABIX = the value before the
loop
SY-SUBRC:
= 0 if any rows were read
0 if no rows were read
-
7/30/2019 08_ABAP - Internal Tables
16/32
LOOP AT Statement - exampleREPORT y_vu_1104.
DATA: BEGIN OF it OCCURS 3,f1(1),
f2(2),
END OF it.
it-f1 = 'A'.
it-f2 = 'XX'.
APPEND it TO it. "appends header line IT to body IT
it-f1 = 'B'.
it-f2 = 'YY'.
APPEND it. "same as line 8
it-f1 = 'C'.
APPEND it. "the internal table now contains
three rows.
sy-tabix = sy-subrc = 99.LOOP AT it. "same as: loop at it into it
WRITE: / sy-tabix, it-f1, it-f2.
ENDLOOP.
WRITE: / 'done. sy-tabix =', sy-tabix,
/ ' sy-subrc =', sy-subrc.
-
7/30/2019 08_ABAP - Internal Tables
17/32
Using the FROM, TO, and WHERE additions
REPORT y_vu_1105.
DATA: BEGIN OF it OCCURS 3,f1(1),
f2(2),
END OF it.
it-f1 = 'A'.
it-f2 = 'XX'.
APPEND it.
it-f1 = 'B'.
it-f2 = 'YY'.APPEND it.
it-f1 = 'C'.
APPEND it.
LOOP AT it WHERE f2 = 'YY'. "f2 is right, it-f2 would be wrong here
WRITE: / sy-tabix, it-f1, it-f2.
ENDLOOP.
LOOP AT it TO 2. "same as: loop at it from 1 to 2.
WRITE: / sy-tabix, it-f1, it-f2.
ENDLOOP.
LOOP AT it FROM 2. "same as: loop at it from 2 to 3.
WRITE: / sy-tabix, it-f1, it-f2.
ENDLOOP.
-
7/30/2019 08_ABAP - Internal Tables
18/32
Using EXIT, CONTINUE, and CHECK
EXIT:
terminates the loop immediately
go to the statement after ENDLOOP
CONTINUE:
go to the beginning of the next pass
CHECK exp:
If exp = TRUE: do nothing
If exp = FALSE: same as CONTINUE
-
7/30/2019 08_ABAP - Internal Tables
19/32
READ TABLE Statement
to read a single row from an itab
Syntax:
read table it[into wa] [index i| with key keyexp.
where: itis the name of an internal table.
wa is the name of a work area.
iis an integer literal, constant, or variable representing arelative row number. For example, 1 means the first row inthe table, 2 means the second, and so on.
keyexp is an expression representing a value to be found.
-
7/30/2019 08_ABAP - Internal Tables
20/32
Using the INDEX Addition
index: 1st row in the table index 1; 2nd row
index 2;
READ TABLE IT INDEX i: reads the ith
row fromthe itab and places it in the header line.
If successful (the ith row exists)
SY-SUBRC = 0
SY-TABIX = i
-
7/30/2019 08_ABAP - Internal Tables
21/32
INDEX - example
REPORT y_vu_1106.
DATA: BEGIN OF it OCCURS 3,
f1(2) TYPE n,f2 TYPE i,
f3(2) TYPE c,
f4 TYPE p,
END OF it,
wa LIKE it.
it-f1 = '10'. it-f3 = 'AA'. it-f2 = it-f4 = 1. APPEND it.
it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. APPEND it.
it-f1 = '30'. it-f3 = 'CC'. it-f2 = it-f4 = 3. APPEND it.
READ TABLE it INDEX 2.
WRITE: / 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4.READ TABLE it INTO wa INDEX 1.
WRITE: /,
/ 'sy-subrc =', sy-subrc,/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4,
/ wa-f1, wa-f2, wa-f3, wa-f4.READ TABLE it INDEX 4.
WRITE: /,
/ 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4,/ wa-f1, wa-f2, wa-f3, wa-f4.
-
7/30/2019 08_ABAP - Internal Tables
22/32
Using the WITH KEY Addition
WITH KEY keyexp :
to find a row that matches the key expression
if > 1 row matches, the 1st one found (lowest index) is returned
Key Expression:
c1 = v1 c2 = v2 ...
component c1 has the value v1 AND component c2 has the value v2
(f1) = v1 (f2) = v2 ...
the value of f1: the name of the component (must be in uppercase)
if f1 is blank, the comparison is ignored
= wa
wa: a work area itab row structure
the 1st row in the itab exactly equal to the contents of wa
wa
wa: a work area or itab row structure (wa has n fields, the fields of wa match the first n
fields of itab row)
the 1st row in the itab whose first n fields match the contents of wa
-
7/30/2019 08_ABAP - Internal Tables
23/32
WITH KEY - example
READ TABLE it WITH KEY f1 = '30' f2 = 3.
WRITE: / 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4.
f = 'F2'. "must be in uppercase
READ TABLE it INTO w2 WITH KEY (f) = 2.WRITE: /,
/ 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4,
/ w2-f1, w2-f2, w2-f3, w2-f4.
-
7/30/2019 08_ABAP - Internal Tables
24/32
WITH KEY - example
CLEAR w2.
w2-f1 = '10'. w2-f3 = 'AA'. w2-f2 = w2-f4 = 1.READ TABLE it WITH KEY= w2.
WRITE: /,
/ 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4.
w1-f1 = '20'. w1-f2 = 2.READ TABLE it INTO w2WITH KEY w1.
WRITE: /,
/ 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,
/ it-f1, it-f2, it-f3, it-f4,
/ w2-f1, w2-f2, w2-f3, w2-f4.
-
7/30/2019 08_ABAP - Internal Tables
25/32
No Additions
default key: an imaginary key composed of allcharacter fields (types C, D, T, N, and X). Ex: DATA: BEGIN OF it OCCURS 3,
f1(2) TYPE n, part of default key
f2 TYPE i,f3(2) TYPE c, part of default key
f4 TYPE p,
END OF it.
READ TABLE with no additions finds a row that matches the default keyof the header
line.
Blanks in a default key field that column is ignored.
-
7/30/2019 08_ABAP - Internal Tables
26/32
Example
CLEAR it.
it(2) = ' '. it-f3 = 'BB'.
READ TABLE it.
WRITE: / 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,/ 'f1=', it-f1, 'f2=', it-f2, 'f3=', it-f3, 'f4=', it-f4.
CLEAR it.
it-f1 = '30'. it-f3 = 'AA'.
READ TABLE it.
WRITE: / 'sy-subrc =', sy-subrc,
/ 'sy-tabix =', sy-tabix,/ 'f1=', it-f1, 'f2=', it-f2, 'f3=', it-f3, 'f4=', it-f4.
-
7/30/2019 08_ABAP - Internal Tables
27/32
Sorting the Contents of an Internal
Table SORT statement:
sort by 1-n column(s)
ascending / descending
Syntax:
sort it[descending] [as text] [byf1 [ascending|descending] [as text]f2 ...].
where:
itis the name of an internal table.
f1 andf2 are components ofit.
... represents any number of field names optionally followed by ascending, descending,
and/or as text
NOTES: ASCENDING: default sort order.
If DESCENDING appears right after SORT default to all components.
If no additions sort by the default key fields (all fields of type C, N, D, and T)
-
7/30/2019 08_ABAP - Internal Tables
28/32
Example
SORT it BY f2.
f2
f1 f2
SORT it BY f2 f1 DESCENDING.
f2 f1
SORT it BY f2 DESCENDING f1.
SORT it DESCENDING BY f2 f1 ASCENDING.
f2 f1
SORT it.
f2
-
7/30/2019 08_ABAP - Internal Tables
29/32
Contents
Summary
-
7/30/2019 08_ABAP - Internal Tables
30/32
Summary
Itab: a collection of rows stored in RAM on the applicationserver: header line (optional): is the implicit work area for the itab
body
OCCURS clause: to allocate memory
does not limit the number of rows of an itab
LOOP AT statement: to retrieve multiple rows from an itab Each row is placed into the header line / explicit work area
FROM, TO, and WHERE: to specify the rows to be returned SY-TABIX: contains the 1-based relative row number
After ENDLOOP, SY-SUBRC will be zero if any rows wereprocessed
-
7/30/2019 08_ABAP - Internal Tables
31/32
Summary
Inside of a loop
EXIT: terminates the loop and continues with the statement following the ENDLOOP
CONTINUE: bypasses the remaining statements of the loop and immediately begins the
next iteration
CHECK: like CONTINUE when its logical expression is FALSE
READ TABLE statement: to locate a single row in the itab INDEX addition: locate a single row by relative row number
WITH KEY addition: locate a row by specifying a value to be found
SORT statement: sorts an itab by named components
If no components are named: sorted by default key fields (types C, N, D, T, X).
-
7/30/2019 08_ABAP - Internal Tables
32/32
Summary