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: 

which is the better way to declare internal table?

Former Member
0 Kudos

hi dons in abap pllese answer for the following.

how many internal tables are available in abap?how to declare each one?

6 REPLIES 6

Former Member
0 Kudos

<b>Internal Table type </b>

The table type determines how ABAP accesses individual table rows. Based on the table type, internal tables can be subdivided into the following three categories:

In standard tables, a logical index is set up internally. The data can be accessed using the table index or the key. If the data is accessed using the key, the response time depends linearly on the number of table entries. The key of a standard table is always non-unique. When you define the table, you must not specify uniqueness for the key.

Sorted tables are always stored sorted by the key. Also for sorted tables, a logical index is set up internally. The data can be accessed using the table index or the key. If the data is accessed using the key, the response time depends logarithmically on the number of table entries since the data is accessed through a binary search. The key of sorted tables can be unique or non-unique. When you define the table, you must specify whether the key is UNIQUE or NON-UNIQUE.

Standard tables and sorted tables are also commonly referred to as index tables.

In hash tables, no logical index is set up internally. Hash tables can only be accessed by specifying the key. The response time in this case is constant irrespective of the number of table entries since the data is accessed through a hash algorithm. The key of hash tables must be unique. When you define the table, you must specify the key as UNIQUE.

Unlike other user-defined ABAP data types, the data type of an internal table does not need to be fully specified but can be set up generically. This means that if you create a data type for an internal table, you can either leave the key or the row type and the key unspecified.

-


<b>The best way to define an internal table is first define a field string and then declare a table of that type.</b>

Example.

DATA: BEGIN OF fs_spfli,

carrid TYPE spfli-carrid,

connid TYPE spfli-connid,

countryfr TYPE spfli-countryfr,

END OF fs_spfli.

DATA: t_spfli LIKE STANDARD TABLE OF fs_spfli.

Regards,

Pavan

Former Member
0 Kudos

Hi,

The best way to define an internal table

Types: BEGIN OF ty_spfli,
     carrid TYPE spfli-carrid,
     connid TYPE spfli-connid,
     countryfr TYPE spfli-countryfr,
END OF ty_spfli.

DATA: it_spfli type table of ty_spfli,
          wa_spfli type ty_spfli.

Cheers,

Satish

0 Kudos

Hi,

Internal Tables

Internal tables are a way to store variable datasets of a fixed structure in the working memory of ABAP. The data is stored on a row-by-row basis, where each row has the same structure.

Internal tables provide the functionality of dynamic arrays and relieve the programmer of the expenditure of a program-controlled dynamic memory management (see Memory management of deep data objects. Internal tables are preferably used to store and format the content of database tables from within a program. Furthermore, internal tables in connection with structures are the most important means of defining very complex data structures in an ABAP program.

Data Type of an Internal Table

A table type defined in the ABAP Dictionary or using TYPES or DATA is fully specified by:

Row Type

The row type of an internal table can be any data type. If the row type is structured, the individual components of a row are called columns of the internal table.

Table Category

The table category defines how an internal table is managed and how the individual rows can be accessed. There are three table categories:

Standard tables are managed internally by a table index which may be realized by means of a logical index. Access is possible via table index or table key. With a key access, the response time is directly dependent on the number of table entries. The key of a standard table is always non-unique.

Sorted tables are managed internally by means of a table index, too. They are always sorted by the table key and can be accessed via the table index or the table key. With a key access, the response time is logarithmically dependent on the number of table entries, because the table is accessed via a binary search. The key of sorted tables can be unique or non-unique.

Hashed tables are managed internally by a hash algorithm. Hashed tables can be accessed only via the table key. The repsponse time is constant, independent of the number of table entries. The key of hashed tables is always unique.

Table Key

The table key is required for the identification of table rows. There are two possible keys for internal tables, the standard key and a user-defined key. The key can be declared as unique or non-unique, depending on the table category. With unique keys, a row with a particular content in the key fields can occur only once in the internal table. A user-defined key can consist of components of the row type or of the entire row ( pseudo component table_line), provided they do not contain or are no internal tables. When defining the key, the sequence of the key fields is significant. A table type defined in the ABAP Dictionary or with TYPES needs not be fully specified, in contrast to all other data types. In the definition, either only the key or the row type and the key can remain unspecified. Such a type is generic and can be used exclusively for the typing of field symbols and formal parameters. For this, also the predefined generic ABAP types ANY TABLE and INDEX TABLE exist. The first includes all table types, the second inlcudes the standard and sorted tables, the so-called index tables.

Like strings, internal tables are dynamic data objects. As far as row type, table category and table key are concerned, they are always fully specified, but with any number of rows, which is limited only by the capacity limits of the actual system installations (see Maximum size of dynamic data objects).

Accessing Internal Tables

When accessing internal tables, you can address either the entire table or table body or individual rows.

To access the table body, you use special statements such as SORT, but also general statements such as MOVE. To access individual rows, you use special statements such as READ_TABLE, LOOP AT or MODIFY. When accessing individual rows, you either use a work area into which the row content can be read or where it can be modified, or you link a row to a field symbol or a data reference variable and use it to access the row directly.

Notes

If the row type of internal tables contains object reference variables as components comp, then the attributes attr of the object to which the reference of a row points can be used as key values for reading, sorting and changing table rows. This is basically possible with statements in which individual components of the table are addressed.

With all modifying accesses to individual rows of sorted tables and hashed tables, the content of the table key must not be changed.

Selecting the Table Category

Which table category to use depends on which kind of access to individual rows will be applied most frequently for the table.

Standard Tables

This table category is suited best whenever the individual entries can be accessed using the index. The index access is the fastest access to table entries. You should fill a standard table by appending rows with APPEND and execute the other accesses by specifying an index (addition INDEX of the resprective statement). Since the expenditure for key accesses to standard tables increases directly with the number of table entries, you should use key accesses to standard tables only if you can decouple the filling of the table from the other processing operations. If a standard table is sorted after the filling, a key access with binary search (BINARY SEARCH) is connected to the number of table entries only logarithmically.

Sorted Tables

This table category is suited whenever the table must already be sorted when it is filled. You then use INSERT to fill the table according to the sort sequence defined by the table key. With key accesses, the expenditure is connected logarithmically to the number of table entries, because a binary search is carried out automatically. Sorted tables are suited for partly sequential processing in a LOOP if the beginning of the table key is specified in the WHERE condition.

Hashed Tables

This table category is suited whenever the key accessess represent the central operation on the table entries. With hashed tables, no index access is possible. With key accesses, the expenditure is always constant and independent of the number of table entries. Like with database tables, the key of a hashed table is always unique. Therefore, hashed tables are also suited as database-type internal tables.

Internal Tables with Header Lines

Outside of classes and as long as an internal table is not a component of a structure or row of another internal table, you can create an internal table with a so-called header line (HEADER LINE). A header line is a work area whose data type is the row type of the internal table and whose name is the name of the internal table. You should not use this possibility, because then two data objects with the same name exist in the ABAP program. Instead of a header line, you can simply create an explicit work area using the addition LINE OF of the statements TYPES, DATA etc.

The statements for the access to individual rows use the header line as implicit work area if no explicit work area is specified. In all other cases it depends on the statement and operand position whether the table body or the header line is used for processing when you specify a table name. Usually the header line is addressed. To force access to the table body, you can specify square brackets after the name (for example, itab[]). When passing it to table parameters, table body and header line are passed.

REPORT demo_int_tables_compare .

DATA: BEGIN OF line,

col1 TYPE i,

col2 TYPE i,

END OF line.

DATA: itab LIKE TABLE OF line,

jtab LIKE TABLE OF line.

DO 3 TIMES.

line-col1 = sy-index.

line-col2 = sy-index ** 2.

APPEND line TO itab.

ENDDO.

MOVE itab TO jtab.

line-col1 = 10. line-col2 = 20.

APPEND line TO itab.

IF itab GT jtab.

WRITE / 'ITAB GT JTAB'.

ENDIF.

APPEND line TO jtab.

IF itab EQ jtab.

WRITE / 'ITAB EQ JTAB'.

ENDIF.

line-col1 = 30. line-col2 = 80.

APPEND line TO itab.

IF jtab LE itab.

WRITE / 'JTAB LE ITAB'.

ENDIF.

line-col1 = 50. line-col2 = 60.

APPEND line TO jtab.

IF itab NE jtab.

WRITE / 'ITAB NE JTAB'.

ENDIF.

IF itab LT jtab.

WRITE / 'ITAB LT JTAB'.

ENDIF.

Regards,

Sesh

gopi_narendra
Active Contributor
0 Kudos

This is the best way of declaring a internal table and work area

types : begin of ty_tab,
          zuonr type bsid-zuonr,
        end of ty_tab.

data : it_tab type table of ty_tab initial size 0,
       is_tab type ty_tab.

Regards

Gopi

Former Member
0 Kudos

hi venkaiah,

What Are Different Types Of Internal Tables and Their Usage

Standard Internal Tables

Standard tables have a linear index. You can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition.

This table type is particularly appropriate if you want to address individual table entries using the index. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index (INDEX option with the relevant ABAP command). The response time for accessing a standard table is in linear relation to the number of table entries. If you need to use key access, standard tables are appropriate if you can fill and process the table in separate steps. For example, you can fill a standard table by appending records and then sort it. If you then use key access with the binary search option (BINARY), the response time is in logarithmic relation to

the number of table entries.

Sorted Internal Tables

Sorted tables are always saved correctly sorted by key. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition. Standard tables and sorted tables both belong to the generic group index tables.

This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it. You fill the table using the (INSERT) statement, according to the sort sequence defined in the table key. Table entries that do not fit are recognised before they are inserted. The response time for access using the key is in logarithmic relation to the number of

table entries, since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP, as long as the WHERE condition contains the beginning of the table key.

Hashed Internal Tables

Hashes tables have no internal linear index. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition.

This table type is particularly suitable if you want mainly to use key access for table entries. You cannot access hashed tables using the index. When you use key access, the response time remains constant, regardless of the number of table entries. As with database tables, the key of a hashed table is always unique. Hashed tables are therefore a useful way of constructing and

using internal tables that are similar to database tables.

Index Tables

Index table is only used to specify the type of generic parameters in a FORM or FUNCTION. That means that you can't create a table of type INDEX.

Internal tables are not DB tables. Standard and Sorted tables in combined are basically called as Index tables and there nothing else. Here is the hierarchy

ANY TABLE

|

-


| |

Index Tables Hashed Table

|

-


| |

Standard Table Sorted Table

***********************************************************************************************

Here is the link that has the details regardign the internal table and its types.internal table types

Kindly reward points if answer is useful...

Regards,

Mandeep.

Former Member
0 Kudos

Hi Venkaiah,

There are 3 types of internal tables are available.

1. standard

2. sorted

3. hashed

It is always better to declare internal tables as types.

and then u can iuse them as whichever u want to use.

I have given you the declaration of internal tables in all the three ways.

types: begin of ty_data,

matnr like mara-matnr,

end of ty_data.

data: tbl_data type sorted table of ty_data with unique key matnr.

data: tbl_data1 type hashed table of ty_data with unique key matnr.

data: tbl_data2 type standard table of ty_data.

Reward points if u find this suggestion helpful.

Thanks and Regards,

Yuvaraj