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

problem with read stmt

Former Member
0 Likes
938

My requirement is that i have an itab, which has KOSTL along with other things.

i need to make sure that each and every KOSTL in this itab, has a master data in CSKS, if not found,

then all those KOSTLs should be listed,

so i thought of doing this way: with for all entries on itab ,grab all the data from CSKS pertaining to this itab

and keep them in new i_csks internal table, and then loop through over itab, looking for the match.

i coded the following way, here the condition is that, the KOKRS field in the CSKS should be one of 2000 or 3000.

SELECT KOSTL KOKRS

INTO TABLE I_CSKS

FROM CSKS

FOR ALL ENTRIES IN ITAB

WHERE KOSTL = ITAB-KOSTL AND ( KOKRS = 2000 OR KOKRS = 3000 ).

LOOP AT ITAB.

>>>>>> READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .

IF SY-SUBRC <> 0.

I_KOSTL-KOSTL = ITAB-KOSTL.

I_KOSTL-KOKRS = I_CSKS-KOKRS.

APPEND I_KOSTL.

ENDIF.

ENDLOOP.

ENDFORM.

i am struck at the READ stmt how do i put both KOKRS = 2000 or KOKRS = 3000., bcos its not taking OR inbetween.

any clues.

thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
914

Several options:

1. Internal table only contains KOSTL where KOKRS 2000 / 3000 based on SELECT statement, so you do not need to check this again when in the LOOP - just read with key KOSTL = ...

2. Does ITAB have KOKRS? if so read with KOSTL = ITAB-KOSTL KORKS = ITAB-KOKRS

3. Do 2 reads:


LOOP AT ITAB.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 2000 .
  IF SY-SUBRC <> 0.
    READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .
  ENDIF.
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = I_CSKS-KOKRS.  "<<<< This will not work - I_CSKS entry not found so KOKRS value indeterminate !!! 
    APPEND I_KOSTL.
  ENDIF.
ENDLOOP.


4. Alternative (not sure if this is your requirement


LOOP AT ITAB.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 2000 .
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = '2000'.    "<< Not found in 2000
    APPEND I_KOSTL.
  ENDIF.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = '3000'.    "<< Not found in 3000
    APPEND I_KOSTL.
  ENDIF.
ENDLOOP.


Andrew

7 REPLIES 7
Read only

Former Member
0 Likes
914

Hi Sanjana,

You do not require to put KOKRS = 2000 or KOKRS = 3000 in the READ statement as you already put this condition in the SELECT statement so your internal table I_CSKS has data only for KOKRS = 2000 or 3000.

Regards,

Atish

Read only

Former Member
0 Likes
914

in u r read statement u have to specify the work area.

change READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL into wa_csks.

u r i_csks contain only 2 fields kostl and kokrs so specify key as table_line..

bye

Venu.

Read only

Former Member
0 Likes
915

Several options:

1. Internal table only contains KOSTL where KOKRS 2000 / 3000 based on SELECT statement, so you do not need to check this again when in the LOOP - just read with key KOSTL = ...

2. Does ITAB have KOKRS? if so read with KOSTL = ITAB-KOSTL KORKS = ITAB-KOKRS

3. Do 2 reads:


LOOP AT ITAB.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 2000 .
  IF SY-SUBRC <> 0.
    READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .
  ENDIF.
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = I_CSKS-KOKRS.  "<<<< This will not work - I_CSKS entry not found so KOKRS value indeterminate !!! 
    APPEND I_KOSTL.
  ENDIF.
ENDLOOP.


4. Alternative (not sure if this is your requirement


LOOP AT ITAB.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 2000 .
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = '2000'.    "<< Not found in 2000
    APPEND I_KOSTL.
  ENDIF.
  READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .
  IF SY-SUBRC <> 0.
    I_KOSTL-KOSTL = ITAB-KOSTL.
    I_KOSTL-KOKRS = '3000'.    "<< Not found in 3000
    APPEND I_KOSTL.
  ENDIF.
ENDLOOP.


Andrew

Read only

former_member426550
Participant
0 Likes
914

use loop statement instead of Read statement....

..

or u can use two read statements....

Read only

Former Member
0 Likes
914

Hi

I believe no need to put KOKR in Read Stmt..

If you insist checking KOKR Try this..

LOOP AT ITAB.

>>>>>> READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL kokrs = 3000 .

IF SY-SUBRC <> 0.

CHECK I_CSKS-KOKR = 2000 or 3000 .

IF SY_SUBRC = 0 .

I_KOSTL-KOSTL = ITAB-KOSTL.

I_KOSTL-KOKRS = I_CSKS-KOKRS.

APPEND I_KOSTL.

ELSe.

CONTINUE .

ENDIF.

ENDLOOP.

Hope this helps.

Praveen

Read only

Former Member
0 Likes
914

Hi Sanjana,

You could try below code for your requirement.

SELECT KOSTL

INTO TABLE I_CSKS

FROM CSKS

WHERE KOKRS = 2000

OR KOKRS = 3000.

LOOP AT ITAB.

READ TABLE I_CSKS

TRANSPORTING NO FIELDS

WITH KEY KOSTL = ITAB-KOSTL.

IF SY-SUBRC <> 0.

I_KOSTL-KOSTL = ITAB-KOSTL.

I_KOSTL-KOKRS = ITAB-KOKRS.

APPEND I_KOSTL.

ENDIF.

ENDLOOP.

Regards,

Teddy

Read only

naimesh_patel
Active Contributor
0 Likes
914

Don't bother about the KOKRS = 2000 or 3000

LOOP AT ITAB.
READ TABLE I_CSKS WITH KEY KOSTL = ITAB-KOSTL.  " << if you have any entry it will set SY-SUBRC = 0
IF SY-SUBRC <> 0.
I_KOSTL-KOSTL = ITAB-KOSTL.
I_KOSTL-KOKRS = I_CSKS-KOKRS.
APPEND I_KOSTL.
ENDIF.
ENDLOOP.

Hope you got it

Regards,

Naimesh Patel