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: 

OR condition in key while reading internal table..

Former Member
0 Kudos

Hello,

I want to reading internal table as follows:

READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME1.

I want to read the same table with or condition in key like

domvalue = it_kna1-name1 or domvalue = it_kna1-name2.

How can I do or condition for Keys ?

Regards,

Rajesh

1 ACCEPTED SOLUTION

former_member212653
Active Contributor
0 Kudos

You can only use READ TABLE statement with equality condition.

You can achieve the desired result in the following way:


READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME1.
if sy-subrc ne 0.
READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME2.
 if sy-subrc = 0.
    PERFORM subroutine1.
 endif.
else.
  PERFORM subroutine1.
endif.

5 REPLIES 5

former_member212653
Active Contributor
0 Kudos

You can only use READ TABLE statement with equality condition.

You can achieve the desired result in the following way:


READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME1.
if sy-subrc ne 0.
READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME2.
 if sy-subrc = 0.
    PERFORM subroutine1.
 endif.
else.
  PERFORM subroutine1.
endif.

0 Kudos

In my case I will have value in either of fields, so I want to read the table with that key which has the value..

0 Kudos

You can achieve the desired result in the following way:



READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME1.
if sy-subrc ne 0.
READ TABLE i_cust WITH KEY domvalue = it_kna1-NAME2.
 if sy-subrc = 0.
    PERFORM subroutine1.
 endif.
else.
  PERFORM subroutine1.
endif.
 

0 Kudos

Ok, then you could use a field symbol.

field-symbols: <fs_name> type any.

if it_kna1-NAME1 is not initial.
 assign it_kna1-NAME1 to <fs_name>.
elseif it_kna1-NAME2 is not initial.
 assign it_kna1-NAME2 to <fs_name>.
endif.

READ TABLE i_cust WITH KEY domvalue = <fs_name>.
if sy-subrc = 0.

endif.

Regards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You must use a LOOP in this case.

loop at i_cust 
        where domvalue = it_kna1-NAME1
             or  domvalue = it_kna1-name2.
* Do something
exit.  " now exit if at least one line has been read
endif.

Regards,

Rich Heilman