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 with key field - question

Former Member
0 Likes
1,024

Hi,

Can you use the same key field more than once?

For example:

READ TABLE I_TVKWZ INTO I_TVKWZ_2 WITH KEY WERKS = '1004'

Werks = '1002'.

I get an error when trying this.

Is there an alternative?

Thanks,

John

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
991

Hi John,

I guess you are trying to use OR condition here which will not work with READ statment, you have to use loop for that

loop at itab where werks = '1004' or
                           werks = '1002'.

endloop.

Hi,

Can you use the same key field more than once?

For example:

READ TABLE I_TVKWZ INTO I_TVKWZ_2 WITH KEY WERKS = '1004'

Werks = '1002'.

I get an error when trying this.

Is there an alternative?

Thanks,

John

9 REPLIES 9
Read only

Former Member
0 Likes
991

Hi,

You need to use READ TABLE Twice

Regards

Sudheer

Read only

0 Likes
991

What do you mean?

Read only

Former Member
0 Likes
991

Hi ,

In this requirement you can use loop at where condion ...Instead of read

Loop at Internal table where werks = '1002' and '1004'.

Thanks,

varma

Read only

former_member194669
Active Contributor
0 Likes
991

Hi,

Use Loop with where condition.


loop at itab where werks eq 'A001' and werks eq 'A002'.
  exit.
endloop.
if sy-subrc eq 0.
  <<<<<           >>>>>
endif.

aRs

Message was edited by:

Read only

Former Member
0 Likes
992

Hi John,

I guess you are trying to use OR condition here which will not work with READ statment, you have to use loop for that

loop at itab where werks = '1004' or
                           werks = '1002'.

endloop.

Read only

Former Member
0 Likes
991

Hi John,

You can not use KEY FIELD in READ statement more than once by giving different values. You need to use LOOP AT with WHERE condition and specify the condition in the WHERE clause only.

LOOP AT I_TVKWZ <b>WHERE WERKS = '1004' OR WERKS = '1002'</b>.

<<DO AS PER REQUIREMENT>>

ENDLOOP.

Try as follows if you want only with READ statement

DATA V_SUBRC1 TYPE SY-SUBRC.

DATA V_SUBRC2 TYPE SY-SUBRC.

<b>READ TABLE I_TVKWZ INTO I_TVKWZ_2 WITH KEY WERKS = '1004'.

V_SUBRC1 = SY-SUBRC.

READ TABLE I_TVKWZ INTO I_TVKWZ_2 WITH KEY WERKS = '1004'.

V_SUBRC2 = SY-SUBRC.

IF V_SUBRC1 = 0 OR V_SUBRC2 = 0.

<<DO ACCORDINLGY>>

ENDIF.</b>

Thanks,

Vinay

Read only

Former Member
0 Likes
991

Hi John,

try this:

DATA: begin of itab occurs 0,

werks like mseg-werks,

i type i,

end of itab.

itab-werks = '1000'. itab-i = itab-i + 1. append itab.

itab-werks = '1000'. itab-i = itab-i + 1. append itab.

itab-werks = '2000'. itab-i = itab-i + 1. append itab.

itab-werks = '3000'. itab-i = itab-i + 1. append itab.

itab-werks = '5000'. itab-i = itab-i + 1. append itab.

itab-werks = '5000'. itab-i = itab-i + 1. append itab.

itab-werks = '7000'. itab-i = itab-i + 1. append itab.

itab-werks = '7000'. itab-i = itab-i + 1. append itab.

itab-werks = '9000'. itab-i = itab-i + 1. append itab.

itab-werks = '9000'. itab-i = itab-i + 1. append itab.

itab-werks = '9000'. itab-i = itab-i + 1. append itab.

*

loop at itab where werks = '1000' or werks = '9000'.

write: / itab-werks, itab-i.

endloop.

Regards, Dieter

Read only

former_member194669
Active Contributor
0 Likes
991

Hi,

Try this way.


read table itab with key wekrs = 'A001'
                         binary search.
if sy-subrc eq 0.
  read table itab with key werks = 'A002'
                         binary search.
  if sy-subrc eq 0.
*   <<<<< do your coding  >>>>>
  endif.
endif.
  

aRs

Read only

Former Member
0 Likes
991

Solved?

Regards, Dieter