Application Development and Automation 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: 
Read only

READ TABLE concept

Former Member
6,673

Hi...

Can someone explain me the concept of READ TABLE with examples.

I need a simple explaination not reference to any sites or link.

Thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,765

Hi SIM,

READ TABLE will read a single record from an internal table

READ TABLE ITAB INDEX 1 --> Reads the first record in ITAB

READ TABLE ITAB with key matnr = '10000'.

the above statement reads the record with matnr as 10000, if there are many records with same matnr , it reads the first record with that matnr

8 REPLIES 8
Read only

Former Member
0 Likes
4,766

Hi SIM,

READ TABLE will read a single record from an internal table

READ TABLE ITAB INDEX 1 --> Reads the first record in ITAB

READ TABLE ITAB with key matnr = '10000'.

the above statement reads the record with matnr as 10000, if there are many records with same matnr , it reads the first record with that matnr

Read only

GauthamV
Active Contributor
0 Likes
4,765

hi,

check these read statements.

READ - Reading an Internal Table

Variants:

1. READ TABLE itab FROM wa [ additions].

2. READ TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn [additions].

3. READ TABLE itab WITH KEY k1 = v1 ... kn = vn [BINARY SEARCH] [additions].

4. READ TABLE itab INDEX i [additions].

Obsolete Variants

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Short Forms of Line Operations not Allowed.

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.See Key Specifications and Unicode.

Effect

Reads an entry from an internal table, using either its key or its index. The return code SY-SUBRC specifies whether an entry could be read. If you specify a non-unique key (the table must have a NON-UNIQUE key for this to be valid), the system returns the entry with the lowest index from the set of entries that meet the condition.

SY-SUBRC = 0:

An entry was read.

SY-TABIX is set to the index of the entry.

SY-SUBRC = 2:

An entry was read.

SY-TABIX is set to the index of the entry. This return code can only occur when you use the COMPARING addition. For further detauls, refer to the COMPARING section of the additions

SY-SUBRC = 4:

No entry was read.

The value of SY-TABIX depends on the table type and whether the BINARY SEARCH addition was specified.

If the table is a SORTED TABLE or a table sorted in ascending order of the type STANDARD TABLE with the BINARY SEARCH addition, SY-TABIX refers to the next-highest index.

Otherwise, SY-TABIX is undefined.

SY-SUBRC = 8:

No entry was read.

This return code only occurs with a SORTED TABLE or a STANDARD TABLE with the BINARY SEARCH addition. SY-TABIX is set to the number of all entries plus 1.

The READ TABLE statement also fills the system fields SY-TFILL and SY-TLENG.

Variant 1

READ TABLE itab FROM wa [additions].

Variant 2

READ TABLE itab WITH TABLE KEY k1 = v1 ... kn =

vn [additions].

Effect

The system uses the specified table key values to locate the correct line. If there is more than one entry with the same key, the system returns the first. The way in which the system looks for table entries depends on the table type:

STANDARD TABLE:

The system searches from the start of the table. The response time is in linear relation to the number of table entries.

SORTED TABLE:

The response time is in logarithmic relation to the number of table entries.

HASHED TABLE:

The response time is constant.

Notes

When you specify the table key using k1 = v1 ... kn = vn you must specify values for all of the key fields. If the type of a value is not compatible with the type of the corresponding key field,the system uses MOVE logic to convert it to the type of the key field before reading from the table.

If you do not know the name of a component until runtime, you can use the expression WITH TABLE KEY ... (ni) = vi ... to specify it dynamically as the contents of the field ni. If ni is empty at runtime, the key specification is ignored.

If a table has a non-structured line type, you can use the pseudocomponent TABLE_LINE to address the entire line as the table key (see also Pseudocomponent TABLE_LINE in Internal Tables).

If you specify the key implicitly using FROM wa, the values for the table key are taken from the corresponding components of the (structured) field wa. wa must be compatible with the line type of itab. In this way, you can access a table using READ without having to know the table key statically. If the table key is empty, the system reads the first line.

Variant 3

READ TABLE itab WITH KEY k1 = v1 ... kn = vn

[BINARY SEARCH] [additions].

Effect

The system evaluates the specified key to identify the correct line. If the type of a value is not compatible with the type of the corresponding key field, the system uses MOVE logic to convert the value into the type of the component before reading the table. This is an asymmetric comparison logic, in which the component type takes precedence over the value type.

The way in which the system looks for an entry in the table depends on its table type. The system optimizeds the key access whenever possible (see Optimized Key Operations With Internal Tables).

STANDARD TABLE:

If you use the ... BINARY SEARCH addition, the system uses a binary search. Otherwise, the search is sequential. This assumes that the internal table is sorted in ascending order in the sequence of the specified key fields.

SORTED TABLE:

If the specified key fields form a left-justified extract of the table key or are identical with the entire table key, the search is binary, otherwise sequential.

HASHED TABLE:

If the key fields specified are identical with the entire table key, the hash algorithm is used, otherwise read access is sequential.

Notes

The system reads the first entry in which the specified components k1 ... kn correspond with the values of v1 ... vn. If the type of a value and the type of the corresponding key are incompatible, the system uses MOVE logic to convert the value into the type of the component before reading the table.

If your table has a non-structured line type, you can use the pseudocomponent TABLE_LINE to address the entire line as the key (see also Pseudocomponent TABLE_LINE with Internal Tables).

If you do not know the name of a component until runtime, you can use the expression WITH KEY ... (ni) = vi ... to specify it dynamically as the contents of the field ni. If ni is empty at runtime, the system ignores the component. If ni contains an invalid component name, a runtime error occurs. If ni contains an empty name, the system ignores the key specification.

If the line type of the internal table contains object reference variables as componetns or the entire line type is a reference variable, you can use the attributes of the object to which the reference is pointing in a particular line as key values (see Attributes of Objects as the Key of an Internal Tables).

If you specify a completely empty key, the system reads the first entry from the table.

You restrict your specification using offset and length. This is valid for both the static and dynamic variant.

Variant 4

READ TABLE itab INDEX i [additions].

Effect

Accessing the table entry with the index i.

Notes

Performance:

The quickest way to access a single line of an internal table is direct access using an index, because the response time is then not linked to the size of the table, and is restricted more or less to the transport costs for a single line.

For hashed tables, the response time is constant. Accessing a table using the hash administration makes the response time around 3 times slower than using index access.

If you use the key to access a table, the response time increase as the number of table entries and the size of the search key increase. Searching using a binary search is considerably quicker than using a linear search. Therefore, in many cases it can be quicker to sort the table and then use the BINARY SEARCH addition.

The runtime required to read a line from a table with 100 entries using the index is around 7 msn (standard microseconds), to read a line using a key of 30 bytes using a binary search, around 25 msn, and without binary search, around 100 msn.

Using statements that use an explicit work area for internal tables with a header line can avoid unnecessary value assignments.

Exceptions

Non-Catchable Exceptions

Cause: Overlapping or duplicate key specifications used when reading a table using READ ... WITH TABLE KEY.

Runtime Error: DYN_KEY_DUPLICATE

Cause: When you read a table of the type SORTED, using the BINARY SEARCH addition, the specified key fields must make up a left-justified extract of the key.

Runtime Error: ITAB_ILLEGAL_BINARY_SEARCH

Cause: Key specification missing.

Runtime Error: ITAB_KEY_COMPONENT_MISSING

Cause: Invalid key specification when accessing a key table.

Runtime Error: ITAB_KEY_ILLEGAL_COMPONENT

Cause: Invalid implicit key specification in the Unicode context.

Runtime Error: READ_ITAB_UC_KEY_ERROR

Additional help

Reading Table Lines

Reading Table Lines Using the Index

reward points if hlpful.

Read only

Former Member
0 Likes
4,765

HI

If you want to read a single record then you use read table

ex.

read table itab into wa_itab index 1. " read record with a particular index.

read table itab into wa_itab with key field1 = '...' " read record which satisfy the condition field1 = '.....'.

Regards

Aditya

Read only

vinod_vemuru2
Active Contributor
0 Likes
4,765

Hi Sim,

READ table is used to read single record from an internal table.

It is analogous to SELECT SINGLE where we will get single record from data base. But in read table we will get single record from internal table.

Check below example.

READ TABLE i_edid4 INTO wa_edid4 WITH KEY

docnum = wa_edids-docnum

BINARY SEARCH

TRANSPORTING sdata.

Here i_edid4(internal table) has many records. To get the data of record having field docnum eq wa_edids-docnum we use above statement. It will place the whole record into work area wa_edid4 for that record.

BINARY SEARCH is used for improving performance. SORT by with key fields is mandatory for BINARY SEARCH to work.

TRANSPORTING is used to transport only the field contents of the fields specified in this clause. It is also used for performance improvement.

Check various additos of READ table in F1 help like INDEX etc.

Thanks,

Vinod.

Read only

Former Member
0 Likes
4,765

Hi,

This statement reads a row from internal table.You have to specify the row by either naming values table_key for the table key, a free condition free_key or an index .The output result determines when and where the row contents are read. If the row to be read is not uniquely specified, the first suitable row is read. In the case of index tables, this row has the lowest table index of all matching rows.

You ca't read any table conditionally

i.e.

Read table itab where f1 le 20. is not possible.

Read only

Former Member
0 Likes
4,765

U can use read table to read single record from an internal table.

loop at orders.

.

-


READ TABLE I_MVGR1 WITH KEY MVGR1 = ORDERS-MVGR1.

IF SY-SUBRC EQ 0.

ORDERS-BEZEI1 = I_MVGR1-DES1.

ENDIF.

-


.

.

modify orders.

endloop.

here in this code you are filling orders internal table with data. And i_mvgr1 is a table which keeps the data with respecto to mvgr1 field of orders.

Hope this helps

Edited by: aycan sirel on Jun 13, 2008 8:59 AM

Read only

Former Member
0 Likes
4,765

hi,

READ statement is used to get a single record from the internal table.

This plays a major role in performance issues.

We can avoid loop incase if we are searching for a particular record.

There are various syntaxes :

we have options for specifying the indexes and keys.

read table itab into workarea with key "primary key" = XXX

this reads a single record from itab into workarea.

read table itab into workarea index 10.

this reads a single record from itab with index 10. i.e 10 th record.

Thanks and regards.

Read only

Former Member
0 Likes
4,765

Hi,

Ex:-

read table tbl_kna1 with key kunnr = tbl_vbak-kunnr

binary search .

if sy-subrc = 0.

tbl_final-sortl = tbl_kna1-sortl .

tbl_final-ort01 = tbl_kna1-ort01 .

endif.

Regards,

Koti.