ABAP Internal Table

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 63

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.

 Like a database table, an internal consists of one or more


rows with an identical structure but unlike a database table it
cannot hold data after the program ends.

 It is used as temporary storage for manipulating data and


representing data. Internal table is like arrays.

 Internal tables provide a means of taking data from a fixed


structure and storing it in working memory in ABAP.

 Internal table consists of a body and an optional header


line.
Refer below slides for more examples.
Basic Internal table

Internal table Body

Work area or Header line


Data Structures

Structure Internal Table

Address List Address List

LN FN City ST. LN FN City ST.

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>.

MOVE: ‘X’ TO ADDRESS-FLAG, Address Structure


Flag ID Name1 City
‘00001’ TO ADDRESS-ID,
‘Smith’ TO ADDRESS-NAME1,
‘Philadelphia’ TO ADDRESS-CITY.

WRITE ADDRESS.
Internal Table Types
• Standard
• Sorted
• Hashed
Creating an Internal Table with Header Line

REPORT Y170DM38. The TYPES statement defines the


TABLES: EMPLOYEE. structure and data type for the
internal table.
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID, The DATA statement with an
NAME1 LIKE EMPLOYEE-NAME1,
INITIAL SIZE creates the actual
internal table capable of storing
COUNTRY LIKE EMPLOYEE-
data. Because of the WITH
COUNTRY,
HEADER LINE addition, this
END OF EMP. internal table is created with a
header line.
DATA: EMPTAB TYPE STANDARD TABLE
ID NAME1 COUNTRY
OF EMP INITIAL SIZE 10 WITH HEADER LINE.
Header Line

SELECT * FROM EMPLOYEE.


MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
Internal Table Keys
• Implicit Key
– All character fields
• Explicit Key
– User-defined
• e.g. … WITH [ UNIQUE/NON-UNIQUE ] KEY FIELD1
FIELD2 ...
Size of an Internal Table
Creating an Internal Table without a Header Line

REPORT Y170DM40. The TYPES statement defines the


TABLES: EMPLOYEE.
structure and data type for the
internal table and its work area
TYPES: BEGIN OF EMP,
ID LIKE EMPLOYEE-ID,
The DATA statement with an
NAME1 LIKE EMPLOYEE-NAME1,
INITIAL SIZE creates the actual
COUNTRY LIKE EMPLOYEE-COUNTRY, internal table without a header
END OF EMP. line. The DATA statement
without the INITIAL SIZE
creates the work area for the
DATA: EMPTAB TYPE STANDARD TABLE
internal table.
OF EMP INITIAL SIZE 10,
EMPTAB_WA TYPE EMP. Work Area
ID NAME1 COUNTRY
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB_WA.
APPEND EMPTAB_WA TO EMPTAB.
ENDSELECT.
APPEND <work area> to <EMPTAB>.
Internal Table without a Header Line
WHY???

Separate Internal Table Work Area

Performance Issues

Nested Internal Tables


Transferring ABAP Dictionary Table Structures

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.

Notice the MOVE statement instead


of a MOVE-CORRESPONDING.
Automatic Field Conversion
• MOVE-CORRESPONDING or MOVE field to
field
– Individual field type conversion
• MOVE
– Structure to structure
– Field to structure
– Structure to field
• Intermediate C type
• Followed by adoption of new types
Mass Reading from Database Tables into
Internal Tables

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.

SELECT * FROM EMPLOYEE INTO TABLE EMPTAB


Notice no ENDSELECT is
WHERE COUNTRY = ‘USA’. needed here because no loop
processing occurs.
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE. This LOOP AT <EMPTAB>
TYPES: BEGIN OF EMP, statement allows for a logical
COUNTRY LIKE EMPLOYEE-COUNTRY, expression in a WHERE clause
NAME1 LIKE EMPLOYEE-NAME1, to limit the processing of the
internal table.
SALES LIKE EMPLOYEE-SALES,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE
10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. If no internal table entries
ENDSELECT. qualify under the logical
LOOP AT EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’. expression, the statement
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1,
within the loop is not
EMPTAB-SALES. executed and SY-SUBRC is
ENDLOOP. set to 4.
IF SY-SUBRC NE 0. WRITE: / ‘NO ENTRIES’. ENDIF.
System Field SY-TABIX

REPORT Y170DM46.
TABLES: EMPLOYEE.

TYPES: BEGIN OF EMP,


COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
screen output
PARAMETERS: START LIKE SY-TABIX DEFAULT 10,
END LIKE SY-TABIX DEFAULT 20.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB. SY-TABIX
ENDSELECT.
LOOP AT EMPTAB FROM START TO END.
WRITE: / SY-TABIX, EMPTAB-COUNTRY, EMPTAB-NAME1.
ENDLOOP.
Accumulating Data within an
Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE.
COLLECT <EMPTAB>.
TYPES: BEGIN OF EMP, Country Sales
COUNTRY LIKE EMPLOYEE-COUNTRY, D 400,000 Header Line
SALES LIKE EMPLOYEE-SALES, USA 1,000,000
END OF EMP. GB 500,000
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE.
D 7,800,000

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.

DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE


10 WITH HEADER LINE.

SELECT * FROM EMPLOYEE.


MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.

READ TABLE ….
Reading a Single Table Entry -
Options

READ TABLE <EMPTAB> options:


1) READ TABLE <EMPTAB>.
2) READ TABLE <EMPTAB> WITH KEY <k1> = <v1>…
<kn> = <vn>.
3) READ TABLE <EMPTAB> WITH TABLE KEY <k1> = <v1> ...
<kn> = <vn>.
4) READ TABLE <EMPTAB> WITH KEY = <value>.
5) READ TABLE <EMPTAB> WITH KEY . . . BINARY SEARCH.
6) READ TABLE <EMPTAB> INDEX <i>.
7) READ TABLE <EMPTAB> COMPARING <f1> <f2> . . . .
8) READ TABLE <EMPTAB> COMPARING ALL FIELDS.
9) READ TABLE <EMPTAB> TRANSPORTING <f1> <f2> . . . .
10) READ TABLE <EMPTAB> TRANSPORTING NO FIELDS.
Maintaining Internal Tables
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB. INSERT <EMPTAB> INDEX <i>.
ENDSELECT. MODIFY <EMPTAB> INDEX <i>.
DELETE <EMPTAB> INDEX <i>.
READ TABLE EMPTAB INDEX 1.
MOVE ‘ABC’ TO EMPTAB-NAME1.
MODIFY EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
WRITE / ‘Attempt to modify failed.’.
ELSE. Check SY-SUBRC after
WRITE: / EMPTAB-COUNTRY, EMPTAB-
every attempt to change an
NAME1. internal table entry.
ENDIF.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.
king with an Internal Table without a Header Line

APPEND <work area> TO <internal table>.

COLLECT <work area> INTO <internal table>.

INSERT <work area> INTO <internal table>.

MODIFY <internal table> FROM <work area>.

READ TABLE <internal table> INTO <work area>.

LOOP AT <internal table> INTO <work area>.


Deleting an Internal Table

CLEAR <internal table>


• Initialises the header
line.
REFRESH <internal table>
• Internal table lines • Deletes all
remain unchanged. table lines. FREE <internal table>
• Storage space
• Deletes all
is not released.
table lines.
• Paging is released.
• Storage space
• Header line is released.
remains unchanged. • Header line
remains unchanged.
formation about an Internal Table
REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP,
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
LINE_COUNT TYPE I, DESCRIBE TABLE <internal table>
INITIAL_COUNT TYPE I.
LINES <var1>
SELECT * FROM EMPLOYEE.
OCCURS <var2>.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT. screen output
WRITE: / ‘ lines:’, LINE_COUNT,
/ ‘occurs:’, INITIAL SIZE_COUNT.
Declaration of internal table and assigning values

Declaration of header line Occurs define the


is done with begin of itab body of internal
table

Data : Begin of itab occurs 10, N1 C1 N1 C1


N1 type i,
C1(20) type c,
End of itab.
Header line
or work area
Itab-n1 = 100.
Itab-c1 = 'sap 1'.
Append itab.

Body

Continued in next slide


Itab-n1 = 200.
Itab-c1 = 'sap 2'.
Append itab.

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.

Continued in next slide


Output of program

100 sap 1
200 sap 2
300 sap 3
100 sap 4
Complex Internal table

Types of internal table

Standard internal table


Indexed table
Sorted internal table

Hashed internal table Non index table

Standard internal table uses default non-unique key

Sorted internal table can use both non-unique key


and unique key

Hashed table uses only unique key only


Difference b/w types of internal table
 Standard  Sorted internal  Hashed internal
internal table table table
 Key not given  Key given  Key given
importance importance importance
 No field sorting  Specified field  No field sorting
done
with key will be done
 Can accept only
sorted .  Can accept only
non-unique key
 Can accept both unique key
 Can accept value
through index non-unique and  Can’t accept
unique key value through
 Can accept value index
through index
Types of internal table
Example program for internal table types

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.

data:itab like standard table of stab with non-unique key


pernr initial size 10 with header line.

Continued in next slide


select pernr vorna nachn rufnm gbdat gbort natio from
pa0002 into table itab up to 10 rows.

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.

Continued in next slide


data:htab like sorted table of stab with non-
unique key gbort initial size 10 with header line.

select pernr vorna nachn rufnm gbdat gbort


natio from pa0002 into corresponding
fields of table htab up to 10 rows
where gbort <> ' ' .

loop at htab to 10.


write:/ htab-pernr,15 htab-vorna color 5,
30 htab-nachn, 40 htab-rufnm,
50 htab-gbdat, 70 htab-gbort,
90 htab-natio.
Endloop.

skip 2.

Continued in next slide


data:begin of stab1,
kokrs like csks-kokrs,
kostl like csks-kostl,
datbi like csks-datbi,
datab like csks-datab,
verak like csks-verak,
end of stab1.

data:mtab1 like hashed table of stab1


with unique key kostl initial size 10
with header line.

select kokrs kostl datbi datab verak from


csks into table mtab1 up to 10 rows.sadcsa

Continued in next slide


sort mtab1 by kostl descending.

loop at mtab1.
write:/ mtab1-kokrs color 5,15 mtab1-kostl,
30 mtab1-datbi, 50 mtab1-datab,
70 mtab1-verak.
endloop.

Continued in next slide


OUTPUT

Continued in next slide


Operations of internal table
Operations of internal table include :-

Sort , insert, modify ,read, delete , free , refresh, clear

Syntax :-

Sort <internal table name> by <field name>.

Sort <internal table name> by <field name>


descending.

insert <internal table name> index <index number> .

Continued in next slide


Modify <internal table name> index <index number>
transporting <field 1> <field 2> ---- .

Modify <internal table name> transporting <field 1> <field 2>


where <field 1> = <value>' .

Read table <internal table name> index <index number> .

Read table <internal table name> with key <field name> =


<value> .

Delete <internal table name> index <index number> .

Delete <internal table name> where <field name> = <value>.

Continued in next slide


Delete <internal table name> from <index number> to
<index number>.

Clear <internal table>[].

Clear <internal table>.

Free <internal table>.

Refresh <internal table>.


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

Continued in next page


Example program for Operations of internal table

OUTPUT

Continued in next page


Example program for Operations of internal table

OUTPUT

Continued in next page


Example program for Operations of internal table

OUTPUT

Continued in next page


Example program for Operations of internal table

OUTPUT

Continued in next page


Example program for Operations of internal table

OUTPUT

Continued in next page


Example program for Operations of internal table

OUTPUT
Control level processing

Control level processing is allowed within a


LOOP over an internal table.

It is triggered when new set of records


occurred while processing loop statement.

It starts with AT and end with ENDAT


Control level Processing statements are listed below : -

At first.
.... <statements> ...
endat.

At new <field name>.


.... <statements> ...
endat.

On change of <internal table name>-<field name>.


.... <statements> …
Else.
.... <statements> …
endon.

Continued in next slide


At end of <field name>.
.... <statements> ...
endat.

At last.
.... <statements> ...
endat.

sum

All the above control level processing should be written in loop


and endloop statement.

Only on change ...<statements>... endon can be written both in


loop statement and select … endselect statements.
2 prerequisite has to be taken care for executing
control level processing

1st the field which you want operate on control level


processing should be declared in first of structure from
Which you declare internal table.

2nd the field has to be sorted


Example program for Control level Processing

tables: t001p.

data: begin of ram,


werks like t001p-werks,
btrtl like t001p-btrtl,
btext like t001p-btext,
mofid like t001p-mofid,
end of ram.

select-options area for t001p-werks.


data: itab like standard table of ram initial
size 10 with header line.

select werks btrtl btext mofid from t001p into


corresponding fields of table itab where werks in
area.

sort itab by werks.

loop at itab.

at first.

write: / 'HELLO',20 sy-datum,35 sy-uzeit.

endat.
at new werks.

write:/20 'NEW AREA' color 5, itab-werks


color 1.

endat.

on change of itab-werks.

write:/20 'AREA CHANGED' color 7.

else.

* WRITE:/20 'AREA NOT CHANGED'


COLOR 7.
endon.
write: / itab-werks, itab-btrtl, itab-btext,
itab-mofid.

at end of werks.

write:/20 'END OF AREA' color 3.

endat.

at last.

uline.

Endat.

endloop.

You might also like