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

Looping problem

Former Member
0 Likes
808

Hi all,

My internal table ITAB has a field called FIELD1 and has following values


6789
6795
3456

and my JTAB has a field called FIELD2 and has following values


AA
GG
JI

I need to pick values from a databse table ZTABLE and put in another internal table KTAB.

my select query is as follows

select FIELD3 FROM ZTABLE into KTAB-FIELD3 WHERE ZTABLE-FIELD1 = ITAB-FIELD1 AND ZTABLE-FIELD2 = JTAB-FIELD2.

ENDSELECT.

Now the problem is ..i have to fill KTAB for all entries in ITAB and JTAB...that means after looping and executing the select query..my KTAB should have 3 values for KTAB-FIELD3 that have been picked from ZTABLE-FIELD3. based on values from ITAB and JTAB.

how should i loop?

Regards,

Jessica Sam

7 REPLIES 7
Read only

former_member194669
Active Contributor
0 Likes
793

Try this way


select * from ztable into i_output for all entries in itab
       where field1 = itab-field1.

select * from ztable appending corresponding fields of table i_output 
       for all entries in jtab
       where field2 = jtab-field2.

loop at i_output.
v_tabix = sy-tabix.
  read table itab with key field1 = i_output-field1.
  if sy-subrc eq 0.
     read table jtab with key field2 = i_output-field2.
     if sy-subrc ne 0.
        delete i_output index v_tabix.
     endif.
  else.
     delete i_output index v_tabix.
  endif.
endloop.

after this loop i_output contains your values

a®

Read only

0 Likes
793

a@s

I tried the code but in second query


select * from ztable appending corresponding fields of table i_output 
       for all entries in jtab
       where field2 = jtab-field2

it is showing me a syntax erroe saying "Wrong expression "APPENDING" in FROM clause WHERE condition."

can u help me..what is correct syntax for the second select stmt that you posted?

Regards,

JEssica Sam

Read only

0 Likes
793

Hi jessica,

Add Into After appending & Try this code sample,, It will work,,,


DATA : BEGIN OF ITAB OCCURS 0,
       FIELD1(10),
      END OF ITAB.
DATA : BEGIN OF JTAB OCCURS 0,
       FIELD2(10),
      END OF JTAB.
DATA : BEGIN OF KTAB OCCURS 0,
       FIELD1(10),
       FIELD2(10),
      END OF KTAB.

ITAB-FIELD1 = 'ABCD'.
APPEND ITAB.
ITAB-FIELD1 = 'BCDE'.
APPEND ITAB.
ITAB-FIELD1 = 'CDEF'.
APPEND ITAB.

JTAB-FIELD2 = '1234'.
APPEND JTAB.
JTAB-FIELD2 = '2345'.
APPEND JTAB.
JTAB-FIELD2 = '3456'.
APPEND JTAB.

CLEAR : ITAB, JTAB.

LOOP AT ITAB .
  READ TABLE JTAB INTO JTAB INDEX SY-TABIX.
  KTAB-FIELD1 = ITAB-FIELD1.
  KTAB-FIELD2 = JTAB-FIELD2.
  APPEND KTAB.
  CLEAR : ITAB, JTAB, KTAB.
ENDLOOP.

SELECT * FROM ZTABLE INTO IT_FINAL FOR ALL ENTRIES IN KTAB
       WHERE FIELD1 = KTAB-FIELD1 AND FIELD2 = KTAB-FIELD2.

Thanks & regards,

Dileep .C

Read only

0 Likes
793

select * from ztable INTO appending corresponding fields of table i_output

for all entries in jtab

where field2 = jtab-field2

Add INTO before appending.

Thanks

Kiran

Read only

0 Likes
793

The problem is in 1st query coz of which u r getting error in 2nd query.

your query:-

select * from ztable into i_output for all entries in itab

where field1 = itab-field1.

corrected query:-

select * from ztable into table i_output for all entries in itab

where field1 = itab-field1.

NOTE :here itab should be an internal table with header line of type ztable.

data itab type table of ztable with header line.

Read only

venkat_o
Active Contributor
0 Likes
793

Hi Jessica sam, Just try this way. It works.


IF NOT itab[] IS INITIAL.
  SELECT *
  FROM ztable
  INTO TABLE ktab
  FOR ALL ENTRIES IN itab
  WHERE field1 = itab-field1.
ENDIF.

IF NOT jtab[] IS INITIAL.
  SELECT *
  FROM ztable
  INTO TABLE ktab
  FOR ALL ENTRIES IN jtab
  WHERE field2 = jtab-field2.
ENDIF.
Thanks Venkat

Read only

Former Member
0 Likes
793

thanke evry body solved