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 Internal Table

Former Member
0 Likes
2,542

Hi all,

Is it possible to use an 'OR' condition with read table itab into wa with key addition.?

example: read table itab into wa with key field1 = A

or field1 = B

or field1 = C.

Thanks.

Mungala

8 REPLIES 8
Read only

VXLozano
Active Contributor
0 Likes
880

Not as I can know... you can but, do a three consecutive READ TABLES, one per each field you want to check 😕

(check the code by Rich Heilman a bit down... it's pretty better than mine :P)

READ TABLE itab INTO wa WITH KEY field1 = X.
IF sy-subrc <> 0.
  READ TABLE itab INTO wa WITH KEY field2 = Y.
  IF sy-subrc <> 0.
    READ TABLE itab INTO wa WITH KEY field3 = Z.
  ENDIF. 
ENDIF.

CHECK sy-subrc = 0. "Or an IF, of course

Message was edited by:

Vicenç Lozano

Message was edited (again) by:

Vicenç Lozano

Read only

Former Member
0 Likes
880

hi,

no you cant.

Regards,

Azad,

Read only

Former Member
0 Likes
880

Hi,

We can not use the OR condition in the Read table, but you can use the Comparing option to compare the fields

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm

Regards

Sudheer

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
880

No. You can not do that. Instead you can use a Loop with WHERE clause.

Loop at itab where field1 = 'A'
                       or field1 = 'B'
                       or field1 = 'C'.
* Do what you want here

*Now get out of the loop after first read
Exit.
Endloop.

Regards,

Rich Heilman

Read only

former_member196280
Active Contributor
0 Likes
880

It is not possible to use OR command on read but there are different approches to acheive this.

Rgds,

Sairam

Read only

Former Member
0 Likes
880

Praveena,

That is possible within Loop .. endloop.

You can loop in the internal table and can come out of it, when u find the required record.

Regards,

Sujatha

Message was edited by:

Sujatha

Read only

Former Member
0 Likes
880

Hi,

Specifying the Search Key

The search key may be either the table key or another key.

Using the Table Key

To use the table key of <itab> as a search key, enter <key> as follows:

READ TABLE <itab> FROM <wa> <result>.

or as follows

READ TABLE <itab> WITH TABLE KEY <k1> = <f1> ... <kn> = <fn> <result>.

In the first case, <wa> must be a work area compatible with the line type of <itab>. The values

of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. If you do not know

the name of one of the key fields until runtime, you can specify it as the content of a field <ni>

using the form (<ni>) = <fi>. If the data types of <fi> are not compatible with the key fields, the

system converts them.

The system searches for the relevant lines as follows:

Standard tables

Linear search, where the runtime is in linear relation to the number of table entries.

Sorted tables

Binary search, where the runtime is in logarithmic relation to the number of table entries.

Hashed tables

The entry is found using the hash algorithm of the internal table. The runtime is

independent of the number of table entries.

Using a Different Search Key

To use a key other than the table key as a search key, enter <key> as follows:

READ TABLE <itab> WITH KEY = <f> <result>.

or as follows

READ TABLE <itab> WITH KEY <k1> = <f1> ... <kn> = <fn> <result>.

In the first case, the whole line of the internal table is used as the search key. The contents of the entire table line are compared with the contents of field <f>. If <f> is not compatible with the line type of the table, the value is converted into the line type. The search key allows you to find entries in internal tables that do not have a structured line type, that is, where the line is a single field or an internal table type.

In the second case, the search key can consist of any of the table fields <k1>...<kn>. If you do not know the name of one of the components until runtime, you can specify it as the content of a field <ni> using the form (<ni>) = <fi>. If <ni> is empty when the statement is executed, the search field is ignored. If the data types of <fi> are not compatible with the components in the internal table, the system converts them. You can restrict the search to partial fields by

specifying offset and length.

The search is linear for all table types. The runtime is in linear relation to the number of table lines.

Regards,

Bhaskar

Read only

Former Member
0 Likes
880

Hi,

HAve a look at this link:

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/read.htm

Pls reward points.

Regards,

Ameet