0% found this document useful (0 votes)
249 views

09 - ABAP - Advanced Internal Tables

The document discusses various techniques for working with internal tables in ABAP including: 1) Testing and modifying internal table contents such as obtaining information, copying data between tables, inserting/modifying/deleting rows. 2) Filling internal tables using the COLLECT statement or by selecting data directly from a database table. 3) Examples are provided for each technique to demonstrate how to work with internal tables in ABAP programs.

Uploaded by

VaraPrasad.P
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views

09 - ABAP - Advanced Internal Tables

The document discusses various techniques for working with internal tables in ABAP including: 1) Testing and modifying internal table contents such as obtaining information, copying data between tables, inserting/modifying/deleting rows. 2) Filling internal tables using the COLLECT statement or by selecting data directly from a database table. 3) Examples are provided for each technique to demonstrate how to work with internal tables in ABAP programs.

Uploaded by

VaraPrasad.P
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

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

Filling an Internal Table from a Database Table Summary Exercise / Q&A

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

Testing and Modifying Internal Table Contents

The table body operator: it[] DESCRIBE TABLE APPEND LINES INSERT LINES INSERT MODIFY FREE DELETE CLEAR REFRESH COLLECT

The table body operator


itab it: the header & the body

it[]: means "the body of the internal table it.


to perform table operations that do not require the header line

if itab does not have a header line:


it ~ it[]: represent the body

Contents
Obtaining Information about an Internal Table

Obtaining Information
Information:
Whether the internal table contains data?
How many rows it contains?

Determining Whether an itab Is Empty


IF it[] IS INITIAL.

Determining the Number of Rows in an itab


use DESCRIBE TABLE statement

DESCRIBE TABLE Statement


Syntax: DESCRIBE TABLE it [LINES i] [OCCURS j].

where:
it is the name of an internal table.

i and j are numeric variables.

Effected system variables:


SY-TFILL: Number of rows (~ LINES i) SY-TLENG: Length of a row in bytes

Obtaining Information - example


REPORT y_vu_1201. DATA: BEGIN OF it OCCURS 3, f1 VALUE 'X', END OF it, n TYPE i, m TYPE i. IF it[] IS INITIAL. WRITE: / 'it is empty'. ENDIF. APPEND: it, it. IF NOT it[] IS INITIAL. WRITE: / 'it is not empty'. ENDIF. WRITE: / DESCRIBE WRITE: / / / 'number of rows TABLE it LINES n 'number of rows 'length of a row 'occurs value from sy-tabix:', OCCURS m. from sy-tfill:', from sy-tleng:', :', sy-tabix. sy-tfill, sy-tleng, m.

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

Copying a Portion of an itab


APPEND LINES Statement
append rows to the end

INSERT LINES Statement


insert rows at a place

APPEND LINES Statement


Syntax:
APPEND LINES OF it1 [FROM nf] [TO nt] TO it2.

where:
it1 and it2 are itabs with or without header lines (same structure)

nf and nt are numeric variables, literals, or constants


nf: index of the first row to be copied nt: index of the last row to be copied

After APPEND LINES, SY-TABIX number of rows in the table

INSERT LINES Statement


Syntax:
INSERT LINES OF it1 [FROM nf] [TO nt] INTO it2 [INDEX nb].

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

nt: index of the last row to be inserted


nb: insert before row number nb

Copying Data - examples


it1

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

Comparing the Contents of Two Internal Tables

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

INSERT statement - examples

it-f1 = -99. INSERT it INDEX 3.

LOOP AT it WHERE f1 >= 4. it-f1 = -88. INSERT it. ENDLOOP.

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.

exp is a logical expression involving components of it.


WHERE exp: cannot be used inside LOOP AT or with INDEX

MODIFY statement - examples

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

Deleting Internal Table Contents


FREE statement REFRESH statement
delete all rows free the associated memory
delete all rows leave the memory allocated CLEAR it[] CLEAR it
delete all rows leave the memory allocated

CLEAR statement

DELETE statement

clear the header line

delete one or more rows

FREE and REFRESH


FREE statement
Syntax: FREE it. NOTES:
All rows are deleted and all memory used by the body of the itab is freed. The header line, if it exists, remains unchanged.

REFRESH statement

Syntax: REFRESH it. NOTES:

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

FREE and REFRESH examples


REPORT y_vu_1207. DATA: BEGIN OF it OCCURS 3, f1 LIKE sy-index, END OF it, i LIKE sy-index. DO 3 TIMES. i = sy-index. DO 3 TIMES. it-f1 = i * sy-index. APPEND it. ENDDO. WRITE: / ''. LOOP AT it. WRITE it-f1. ENDLOOP. REFRESH it. ENDDO. FREE it.

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 Statement - examples


DELETE it WHERE f1 BETWEEN 'B' AND 'D'.

LOOP AT it WHERE f1 BETWEEN 'E' AND 'J'. DELETE it. ENDLOOP.

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)

COLLECT Statement - example


DATA: BEGIN OF it OCCURS 10, date like sy-datum, " part of default key tot_sales TYPE p DECIMALS 2, "not part of default key name(10), " part of default key num_sales TYPE i VALUE 1, "not part of default key END OF it. it-date = '20050901'. it-date = '20050901'. it-tot_sales = 400. it-tot_sales = 100. it-name = 'Jack'. it-name = 'Jack'. COLLECT it. COLLECT it. it-date = '20050901'. it-tot_sales = 200. it-name = 'Jim'. COLLECT it.

it-date = '20050902'. it-tot_sales = 700. it-name = 'Jack'. COLLECT it.

it-date = '20050901'. it-tot_sales = 500. it-name = 'Jim'. COLLECT it.

it-date = '20050901'. it-tot_sales = 300. it-name = 'Jack'. COLLECT it.

it-date = '20050901'. it-tot_sales = 600. it-name = 'Jane'. COLLECT it.

Contents
Filling an Internal Table from a Database Table

Filling an Internal Table from a Database Table

Selecting multiple rows directly into the body of an internal table


multiple rows at 1 time

Selecting single rows into a work area and then appending


Adding Rows one by one Using SELECT

Selecting Multiple Rows Directly


use the INTO TABLE addition of the SELECT statement selected rows are placed in a single operation (array operation)
array operation:
statement performs an operation on multiple rows of an itab more efficient than single row operations

no work areas are used or needed Syntax:


(a) SELECT * (b) SELECT f1 f2 . . . FROM dbtab INTO [CORRESPONDING FIELDS OF] TABLE it.

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

Selected Fields Must Fit into the Internal Table


A row is retrieved from dbtab.
A row is allocated in it. The fields from dbtab is moved byte-by-byte into the fields in it. The data types and lengths of each sending field in dbtab should match the receiving field in it. Any remaining fields in it are filled with initial values (blanks or zeros).

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.

Using the CORRESPONDING FIELDS Addition

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

Adding Rows one by one Using SELECT


Requires:
the use of a work area omitting the TABLE addition a second statement such as APPEND, INSERT, or COLLECT
TABLES lfa1. DATA it LIKE lfa1 OCCURS 2 WITH HEADER LINE. SELECT * FROM lfa1 INTO it WHERE land1 = 'DE'. APPEND it. ENDSELECT. SELECT * FROM lfa1 WHERE land1 = 'DE'. APPEND lfa1 TO it. ENDSELECT. "notice 'table' is omitted so the "row goes into the header line of it

"no 'into' so the row goes into the "default table work area "and then is appended to it

Summing Up: Various Forms of SELECT


Statement(s) Writes To

SELECT INTO TABLE it

Body

SELECT INTO CORRESPONDING FIELDS OF TABLE it SELECT INTO it

Body

Header line

SELECT INTO CORRESPONDING FIELDS OF it

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

To copy a portion of an itab from one to another

MODIFY

modifies the contents of one or more rows

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

deletes all rows and frees the memory


to accumulate counts and totals

You might also like