09 - ABAP - Advanced Internal Tables
09 - ABAP - Advanced Internal Tables
Contents
Objectives Testing and Modifying Internal Table Contents
Obtaining Information about an Internal Table Copying Data from one Internal Table to Another Inserting Rows into an Internal Table Modifying Rows in an Internal Table Deleting Internal Table Contents Filling an Internal Table Using COLLECT
Contents
Objectives
Objectives
Recognize the table body operator
to test for the existence of data in an itab to compare the contents of two itabs for equality
Determine the number of rows in an itab: DESCRIBE and SY-TFILL Copy the contents from one itab to another:
using the table body operator the APPEND LINES and INSERT LINES statements
Use the INSERT and MODIFY statements to change the itab contents Delete rows from an itab using DELETE, DELETE ... WHERE, CLEAR, CLEAR it[], REFRESH, and FREE Fill an itab using COLLECT Fill an internal table from a database table using the most efficient constructs
Contents
Testing and Modifying Internal Table Contents
The table body operator: it[] DESCRIBE TABLE APPEND LINES INSERT LINES INSERT MODIFY FREE DELETE CLEAR REFRESH COLLECT
Contents
Obtaining Information about an Internal Table
Obtaining Information
Information:
Whether the internal table contains data?
How many rows it contains?
where:
it is the name of an internal table.
Contents
Copying Data from one Internal Table to Another
Copying Data
it2[] = it1[].
to duplicate the contents of one itab in another it1 & it2: same structure any existing contents in it2 are overwritten the header lines remain unchanged
where:
it1 and it2 are itabs with or without header lines (same structure)
where:
it1 and it2 are itabs with or without header lines (same structure) nf, nt and nb are numeric variables, literals, or constants
nf: index of the first row to be inserted
APPEND LINES OF it1 FROM 2 TO 5 TO it2. INSERT LINES OF it1 FROM 8 INTO it2 INDEX 2.
it2
it2
LOOP AT it2. IF it2-f1 >= 'E'. INSERT LINES OF it1 TO 1 INTO it2. ENDIF. ENDLOOP.
it2
it2[] = it1[].
it2
IF it1[] = it2[].
it1, it2: same structure
TRUE:
the same number of rows and the contents of each row are the same
Contents
Inserting Rows into an Internal Table
INSERT statement
Syntax:
INSERT [wa INTO] it [INDEX n]
where:
wa: a work area (same structure as it) n: a numeric literal, variable, or constant
NOTES:
if wa is not specified insert the header line it insert before row n INSERT statement can be used inside or outside of LOOP AT it
outside: INDEX addition must be specified inside: INDEX is optional; if not specified insert current row
Contents
Modifying Rows in an Internal Table
MODIFY statement
Syntax:
MODIFY it [FROM wa] [INDEX n] [WHERE exp]]
where:
it is the name of an itab with or without a header line. wa is a work area with the same structure as a row in the body of it. n is a numeric literal, variable, or constant.
it-f1 = 5. it-f2 = 'Z'. MODIFY it INDEX 4. LOOP AT it. it-f1 = it-f1 * 2. MODIFY it. ENDLOOP.
Contents
Deleting Internal Table Contents
CLEAR statement
DELETE statement
REFRESH statement
FREE REFRESH:
All rows are deleted, all memory used by the body of the itab remains allocated. The header line, if it exists, remains unchanged.
Use FREE when finished using an itab before program ends Use REFRESH when delete all rows but intend to fill again
CLEAR Statement
Syntax: CLEAR it | CLEAR it[] CLEAR it[] same as REFRESH it
DELETE Statement
Syntax
DELETE it (a) [INDEX n] (b) [FROM i] [TO j] (c) [WHERE exp]
where:
n, i, and j are numeric literals, variables, or constants. exp is a logical expression involving components of it.
NOTES:
DELETE it without any additions can only be used inside LOOP AT it. deletes the current row.
DELETE it INDEX 5.
READ TABLE it WITH KEY f1 = 'K' BINARY SEARCH. IF sy-subrc = 0. DELETE it INDEX sy-tabix. ENDIF.
DELETE it FROM 6 TO 8.
Contents
Filling an Internal Table Using COLLECT
COLLECT Statement
collect totals within an itab while filling it Syntax:
COLLECT [wa INTO] it.
where:
it is an internal table. wa is a work area that has the same structure as it.
NOTES:
add up the numeric fields (types I, P, and F) with the same default key fields (type C, N, D, T, and X)
Contents
Filling an Internal Table from a Database Table
where:
dbtab is the name of a database table. f1 and f2 are fields within dbtab. it is the name of an itab.
NOTES:
ENDSELECT is not used with INTO TABLE
Example
REPORT y_vu_1301. TABLES lfa1. DATA it LIKE lfa1 OCCURS 23 WITH HEADER LINE.
SELECT * FROM lfa1 INTO TABLE it. LOOP AT it. WRITE: / it-lifnr, it-name1. ENDLOOP.
Some issues
SELECT INTO TABLE and Sorting
SELECT INTO TABLE, then SORT itab faster
SELECT ORDER BY
Examples
REPORT y_vu_1302. TABLES lfa1. DATA BEGIN OF it OCCURS 2. INCLUDE STRUCTURE lfa1. DATA END OF it. SELECT * FROM lfa1 INTO TABLE it WHERE lifnr < '0000000010'. LOOP AT it. WRITE: / it-mandt, it-lifnr, it-land1, it-name1. ENDLOOP. SKIP. SELECT lifnr land1 name1 FROM lfa1 INTO TABLE it WHERE lifnr < '0000000010'. LOOP AT it. WRITE: / it-mandt, it-lifnr, it-land1, it-name1. ENDLOOP.
moves fields with the same name the same effect as the MOVECORRESPONDING NOTES:
The order of the sending and receiving fields does not matter Sending fields that do not have a corresponding receiving field are discarded Receiving fields that do not have a corresponding sending field are unchanged
Example
REPORT y_vu_1304. TABLES lfa1. DATA: BEGIN OF it1 OCCURS 23, lifnr LIKE lfa1-lifnr, lifnr_ext LIKE lfa1-lifnr, land1 LIKE lfa1-land1, END OF it1. SELECT lifnr land1 FROM lfa1 INTO CORRESPONDING FIELDS OF TABLE it1 WHERE lifnr BETWEEN 'V10' AND 'V12'.
"no 'into' so the row goes into the "default table work area "and then is appended to it
Body
Body
Header line
Header line
Contents
Summary
Summary
The table body operator
IF it[] IS INITIAL: test for existence of data IF it1[] = it2[]: to compare two itabs for equality it2[] = it1[]: to duplicate an itab
get attributes of an itab
DESCRIBE TABLE it [LINES i] [OCCURS j]. APPEND LINES OF or INSERT LINES OF. INSERT
inserts a row at any position
MODIFY
Summary
DELETE CLEAR
removes one or more rows
CLEAR it: clears the header line of it CLEAR it[]: deletes all rows from it and leaves the memory allocated deletes all rows and leaves the memory allocated (~ CLEAR it[])
REFRESH
FREE
COLLECT