Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

when to use Internal table wirh and without header line

Former Member
0 Kudos

Hi experts,

Please tell me when to use internal table with header lines and in whicah cases need to go for with out header line and also please give me the syntaxes with examples if possible.

6 REPLIES 6

Former Member
0 Kudos

Hi Rao,

When you want loop into internal table,or want to process items from internal table ,you can do this using internal table with header line.Because whenever to read internal table it first read it into header(structure) and then from header it shows the values. You offen come across scenario when you initialize internal table from another internal table then you don't need header line.

Or when you want to compare two internal table you don't need internal table with header line.

give points if reply is useful.

regards,

Albert

Former Member
0 Kudos

Hi,

When you create an internal table object you can also declare a header line with the same name. You can use the header line as a work area when you process the internal table. The ABAP statements that you use with internal tables have short forms that you can use if your internal table has a header line. These statements automatically assume the header line as an implicit work area. The following table shows the statements that you must use for internal tables without a header line, and the equivalent statements that you can use for internal tables with a header line:

Operations without header line

Operations with header line

Operations for all Table Types

INSERT <wa> INTO TABLE <itab>.

INSERT TABLE ITAB.

COLLECT <wa> INTO <itab>.

COLLECT <itab>.

READ TABLE <itab> ... INTO <wa>.

READ TABLE <itab> ...

MODIFY TABLE <itab> FROM <wa> ...

MODIFY TABLE <itab> ...

MODIFY <itab> FROM <wa> ...WHERE ...

MODIFY <itab> ... WHERE ...

DELETE TABLE <itab> FROM <wa>.

DELETE TABLE <itab>.

LOOP AT ITAB INTO <wa> ...

LOOP AT ITAB ...

Operations for Index Tables

APPEND <wa> TO <itab>.

APPEND <itab>.

INSERT <wa> INTO <itab> ...

INSERT <itab> ...

MODIFY <itab> FROM <wa> ...

MODIFY <itab> ...

Using the header line as a work area means that you can use shorter statements; however, they are not necessarily easier to understand, since you cannot immediately recognize the origin and target of the assignment. Furthermore, the fact that the table and its header line have the same name can cause confusion in operations with entire internal tables. To avoid confusion, you should use internal tables with differently-named work areas.

The following example shows two programs with the same function. One uses a header line, the other does not.

With header line:

TYPES: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1

WITH HEADER LINE.

DO 4 TIMES.

ITAB-COL1 = SY-INDEX.

ITAB-COL2 = SY-INDEX ** 2.

INSERT TABLE ITAB.

ENDDO.

ITAB-COL1 = 2.

READ TABLE ITAB FROM ITAB.

ITAB-COL2 = 100.

MODIFY TABLE ITAB.

ITAB-COL1 = 4.

DELETE TABLE ITAB.

LOOP AT ITAB.

WRITE: / ITAB-COL1, ITAB-COL2.

ENDLOOP.

Without header line:

TYPES: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA: ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,

WA LIKE LINE OF ITAB.

DO 4 TIMES.

WA-COL1 = SY-INDEX.

WA-COL2 = SY-INDEX ** 2.

INSERT WA INTO TABLE ITAB.

ENDDO.

WA-COL1 = 2.

READ TABLE ITAB FROM WA INTO WA.

WA-COL2 = 100.

MODIFY TABLE ITAB FROM WA.

WA-COL1 = 4.

DELETE TABLE ITAB FROM WA.

LOOP AT ITAB INTO WA.

WRITE: / WA-COL1, WA-COL2.

ENDLOOP.

The list, in both cases, appears as follows:

1 1

2 100

3 9

The statements in the program that does not use a header line are easier to understand. As a further measure, you could have a further work area just to specify the key of the internal table, but to which no other values from the table are assigned.

Regards ,

Albert

Former Member
0 Kudos

Hi,

When you need to access a field directly from internal

table before moving it to work area you need to use

with header line.

Header line is nothing but like a work Area.

Ex:

<b>DATA :itab type table of MARA.</b>

The above declaration doesn't have a header line.So if you need to access the internal table contente you should use.

DATA wa_itab like line of itab.

LOOP AT itab into <b>wa_itab</b>.

....

ENDLOOOP.

But where as when you use header line

DATA :itab type table of MARA <b>with header line</b>.

You <b>need not declare</b> a work area.

LOOP AT itab.

....

ENDLOOOP.

Please note that in the above statement INTO WA_itab is missing.

Please reward points if this explanation is useful.

Regards,

Siva

Former Member

Former Member
0 Kudos

Hi Narshimha,

I think following can help in your cause:

While adding or retrieving records to / from internal table we have to keep the record temporarily.

An internal table consists of a body and an optional header line.

Header line is a implicit work area for the internal table. It depends on how the internal table is declared that the itab will have the header line or not.

e.g.

data: begin of itab occurs 10,

ab type c,

cd type i,

end of itab. " this table will have the header line.

data: wa_itab like itab. " explicit work area for itab

data: itab1 like itab occurs 10. " table is without header line.

The header line is a field string with the same structure as a row of the body, but it can only hold a single row.

It is a buffer used to hold each record before it is added or each record as it is retrieved from the internal table. It is the default work area for the internal table.

I hope this helps.

Cheers

Amit

0 Kudos

I haven't used header lines for many years (except in R2 and early R3 releases) and I do not miss them. Header lines are obsolete and should not be used, the same applies for occurs btw. I think there is a weblog from Horst Keller - explaining why.

My point is that accessing different data objects (the workarea and the table body) via the same name is kind of ambigous and not good programming style.

Example:

Consider you read somewhere in the coding

clear itab.

What do you think happens here.

If itab is declared with a header line then the answer is:

the header line (the workarea) is cleard - the contents of the table body remains.

If itab is declared without header line then

the contents of the table body is cleared.

Just my 2ct

Christian