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: 

Best way to declare and use internal table

Former Member
0 Kudos

Hi all,

As per my knoledge there are various techeniques (Methods) to declare and use the internal tables.

Please Suggest me the Best way to declaring and using internal table ( WITH EXAMPLE ).

Please Give the reason as well how the particular method is good ?

What are benefits of particular method ?

Thanks in advance.

Regards

Raj

6 REPLIES 6

Former Member
0 Kudos

Hi,

The best way is to create an internal table with out a header line...Which will be supported in the OO programming..

Ex..



TYPES: BEGIN OF TYPE_MATERIAL,
                MATNR TYPE MATNR,
                WERKS TYPE WERKS_D,
             END OF TYPE_MATERIAL.

DATA: T_MARA TYPE STANDARD TABLE OF TYPE_MATERIAL.
DATA: S_MARA TYPE TYPE_MATERIAL.

* Process the internal table.
LOOP AT T_MARA INTO S_MARA.

* Display.
  WRITE: / S_MARA-MATNR, S_MARA-WERKS.

ENDLOOP.


Thanks

Naren

Former Member
0 Kudos

Hi Raj,

Below is the best way.

  • Type defination

TYPES : begin of t_itab,

field1 type char4,

...

end of t_itab.

  • Work Area defination

data gs_itab type t_itab.

  • internal table defination

data gt_itab type table of t_itab.

*Use as

LOOP AT gt_itab into gs_itab.

ENDLOOP.

It is as per SAP recommendations and also suitable for ABAP OO.

Reward points if useful.

Regards,

Atish

Former Member
0 Kudos

Internal table with header line

here i am using get time field command ,so use both program and see comparision time

report zxyz.

  • Internal table with header line.

DATA:BEGIN OF itab OCCURS 0,

lifnr LIKE lfa1-lifnr,

name1 LIKE lfa1-name1,

END OF itab.

data : f1 type i,

f2 type i,

f3 type i.

start-of-selection.

get run time field f1.

SELECT lifnr name1 FROM lfa1 INTO TABLE itab up to 200 rows.

loop at itab.

endloop.

get run time field f2.

f3 = f2 - f1 .

write 😕 'Time taken by query in Micro seconds', f3.

Internal table without header line

report zxyz1.

  • Internal table without header line.

types : begin of ty_itab ,

lifnr LIKE lfa1-lifnr,

name1 LIKE lfa1-name1,

end of ty_itab.

data itab type standard table of ty_itab.

data wa_itab like line of itab.

data : f1 type i,

f2 type i,

f3 type i.

start-of-selection.

get run time field f1.

SELECT lifnr name1 FROM lfa1 INTO TABLE itab up to 200 rows.

loop at itab into wa_itab.

endloop.

get run time field f2.

f3 = f2 - f1 .

write 😕 'Time taken by query in Micro seconds', f3.

former_member196299
Active Contributor
0 Kudos

hi Raj ,

The best method to declare an internal table is to first declare a type and keep all the fields there . you can us this type while declaring the itab .

example :

types : begin of Itab,

field1 type c,

field1 type c,

field1 type c,

end of itab .

data: itab1 type table of itab occurs 0 .

According to my knowledge this is the best way to declare an itab and is prooved to be the best as performance issues are concerned.

Regards,

Ranjita

Former Member
0 Kudos

Hey Raj,

Logically speaking, you can not have the best way to define an internal table.

It all depends on the situation :- whether you want the internal table with header line or you need table structure same as database table/structure or you need a sorted table or you need a table Object oriented ABAP.

You give specific requirement and only then the best way can be defined.

Former Member
0 Kudos

Hello Raj Ahir,

There are so many methods to declare an internal table.

Mainly I would like to explain 2 of them mostly used.

1. Using Work Area and

2. With header line.

This with header line concept is not suggestable, because, when we shift the code to oops concept.. it doesn't work... Because OOPS doesn't support the Headerline concept...

But it all depends on the situation.

If you are sure that your program doen't make use of the OOPs concept, you can use HEADER LINE concept. If it invols OOPs use WORK AREA concept.

Now I'l explain these two methods with an example each...

1. Using Work area.

TABLES: sflight.

DATA: it_sflight TYPE TABLE OF sflight.

DATA: wa_sflight LIKE LINE OF it_sflight.

SELECT *

FROM sflight

INTO it_sflight

WHERE <condition>.

LOOP AT it_sflight INTO wa_sflight.

WRITE / wa_sflight.

ENDLOOP.

In this case we have to transfer data into work area wa_sflight.

We can't access the data from the internal table direclty without work

area.

*<===============================================

2. Using Header line.

DATA: BEGIN OF it_sflight OCCURS 0,

carrid LIKE sflight-carrid,

connid LIKE sflight-connid,

fldate LIKE sflight-fldate,

END OF it_sflight.

SELECT *

FROM sflight

INTO it_sflight

WHERE <condition>.

LOOP AT it_sflight INTO wa_sflight.

WRITE / wa_sflight.

ENDLOOP.

In this case we can directly access the data from the internal table.

Here the internal table name represents the header. for each and every

interation the header line will get filled with new data. If you want to

represnent the internal table body you can use it_sflight[].

*<======================================================

TYPES: BEGIN OF st_sflight,

carrid LIKE sflight-carrid,

connid LIKE sflight-connid,

fldate LIKE sflight-fldate,

END OF st_sflight.

DATA: it_sflight TYPE TABLE OF st_sflight,

wa_sflight LIKE LINE OF it_sflight.

This is using with work area.

*----


DATA: it_sflight LIKE sflight OCCURS 0 WITH HEADER LINE.

This is using header line.

<b>REWARD THE POINTS IF IT IS HELPFUL.</b>

Regards

--

Sasidhar Reddy Matli.

Message was edited by: Sasidhar Reddy Matli

Sasidhar Reddy Matli