ABAP Internal Table
ABAP Internal Table
ABAP Internal Table
SAP AG
Internal table is a temporary table stored in RAM or ABAP
memory on the application server. It is created and filled by a
Program during execution and is discarded when the program
Ends.
LN FN City ST.
LN FN City ST.
Declaring a Structure - Method #1
Is this statement
1 REPORT YN1C0008.
2
necessary for the Basic Syntax:
3 TABLES: TABNA. code? DATA: BEGIN OF <name>
4 DATA: BEGIN OF ADDRESS,
5 FLAG TYPE C,
6 ID LIKE TABNA-ID, <field1> . . .
7 NAME1 LIKE TABNA-NAME1,
8 CITY LIKE TABNA-CITY, <field2> . . .
9 END OF ADDRESS.
10 MOVE ‘X’ TO ADDRESS-FLAG. ...
11 MOVE ‘0001’ TO ADDRESS-ID.
12 MOVE ‘Smith’ TO ADDRESS-NAME1. END OF <name>.
13 MOVE ‘Philadelphia’ TO ADDRESS-CITY.
14
15 WRITE ADDRESS.
16 Address Structure
17 Flag ID Name1 City
18
19
20
21
22
23
Declaring a Structure - Method #2
REPORT Yxxxxxxx.
Basic Syntax:
TYPES: BEGIN OF ADDR,
FLAG,
TYPES: BEGIN OF <name1>,
ID LIKE EMPLOYEE-ID, <field1> . . . ,
NAME1 LIKE EMPLOYEE- <field2> . . . ,
NAME1,
CITY LIKE EMPLOYEE-CITY, ... ,
END OF ADDR. END OF <name1>.
DATA: <name2> TYPE
DATA: ADDRESS TYPE ADDR.
<name1>.
WRITE ADDRESS.
Internal Table Types
• Standard
• Sorted
• Hashed
Creating an Internal Table with Header Line
Performance Issues
REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: EMPTAB LIKE STANDARD TABLE OF
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
The internal table EMPTAB will
have the exact same structure as
SELECT * FROM EMPLOYEE. the dictionary table
MOVE EMPLOYEE TO EMPTAB.
EMPLOYEE.
APPEND EMPTAB.
ENDSELECT.
REPORT Y170DM69.
SELECT * FROM <table> . . .
TABLES: EMPLOYEE. 1. INTO TABLE <EMPTAB>.
2. APPENDING TABLE
DATA: EMPTAB LIKE STANDARD TABLE <EMPTAB>.
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
REPORT Y170DM46.
TABLES: EMPLOYEE.
screen output
SELECT * FROM EMPLOYEE. A 371,065.00
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB. CH 45,305.00
COLLECT EMPTAB. D 8,200,000.00
ENDSELECT. F 0.00
LOOP AT EMPTAB. GB 500,000.00
NL 577,000.00
WRITE: / EMPTAB-COUNTRY, EMPTAB-SALES.
NO 234.00
ENDLOOP.
USA 1,000,000.00
HK 0.00
Sorting an Internal Table
REPORT Y170DM44.
TABLES: EMPLOYEE. Sorting options:
TYPES: BEGIN OF EMP,
1) SORT <EMPTAB> - sorts
COUNTRY LIKE EMPLOYEE-COUNTRY,
the entries of the internal table
NAME1 LIKE EMPLOYEE-NAME1, <EMPTAB> in ascending order.
SALES LIKE EMPLOYEE-SALES,
2) SORT <EMPTAB> BY
END OF EMP.
<field> - sorts the table on one or
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
more fields within the table.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
SORT EMPTAB BY SALES DESCENDING. screen output
LOOP AT EMPTAB.
WRITE: / ITAB-COUNTRY, ITAB-NAME1, ITAB-SALES.
ENDLOOP.
Reading a Single Table Entry
REPORT Y170DM47.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMPTAB.
READ TABLE ….
Reading a Single Table Entry -
Options
Body
Itab-n1 = 300.
Itab-c1 = 'sap 3'.
Append itab.
Itab-n1 = 100.
Itab-c1 = 'sap 4'.
Append itab.
Loop at itab.
Write:/ itab-n1,itab-c1.
Endloop.
100 sap 1
200 sap 2
300 sap 3
100 sap 4
Complex Internal table
tables:pa0002.
data:begin of stab,
pernr like pa0002-pernr,
vorna like pa0002-vorna,
nachn like pa0002-nachn,
rufnm like pa0002-rufnm,
gbdat like pa0002-gbdat,
gbort like pa0002-gbort,
natio like pa0002-natio,
end of stab.
loop at itab.
write:/ itab-pernr,15 itab-vorna,30 itab-nachn,
40 itab-rufnm, 50 itab-gbdat, 70 itab-gbort,
90 itab-natio.
Endloop.
skip 2.
skip 2.
loop at mtab1.
write:/ mtab1-kokrs color 5,15 mtab1-kostl,
30 mtab1-datbi, 50 mtab1-datab,
70 mtab1-verak.
endloop.
Syntax :-
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
Control level processing
At first.
.... <statements> ...
endat.
At last.
.... <statements> ...
endat.
sum
tables: t001p.
loop at itab.
at first.
endat.
at new werks.
endat.
on change of itab-werks.
else.
at end of werks.
endat.
at last.
uline.
Endat.
endloop.