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: 

Read table index sy-tabix

Former Member
0 Kudos

Hi Experts,

As per my requirment I need fetch two different categories of matnr based on movment type

from mseg..

For eg: If I have two itab's : itab1 and itab2.

In itab1 the available records are:

matnr    werks     lifnr
mat1     unit1      ABC
mat2     unit1      ABC
mat3     unit1      ABC
mat4     unit1      ABC

In itab2 the available records are:

matnr_1  werks_1  lifnr_1
mat5       unit1       ABC
mat6       unit1       ABC
mat7       unit1       ABC
mat8       unit1       ABC

and I want to move this itab1 and itab2 to another itab ie. itab3.

and my o/p shud look like:

matnr    werks     lifnr     matnr_1
 mat1     unit1     ABC     mat5
 mat2     unit1     ABC     mat6
 mat3     unit1     ABC     mat7
 mat4     unit1     ABC     mat8.

Please advice

Karthik

Edited by: Karthik R on Jun 18, 2009 6:33 PM

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Karthik,

It is quite easy to do so using LOOP & READ statment combination.

NB: I am assuming you are combining the 2 tables based on Plant(WERKS) & Vendor(LIFNR)

The code will be:

DATA:

BEGIN OF ITAB3 OCCURS 0,
MATNR TYPE MATNR,
WERKS TYPE WERKS_D,
LIFNR TYPE LIFNR,
MATNR_1 TYPE MATNR,
END OF ITAB3.

SORT ITAB1 BY WERKS LIFNR.

SORT ITAB2 BY WERKS LIFNR. "-->This is important for READ TABLE

LOOP AT ITAB1.

ITAB3-MATNR = ITAB1-MATNR.
ITAB3-WERKS = ITAB1-MATNR.
ITAB3-LIFNR = ITAB1-LIFNR.

READ TABLE ITAB2 INDEX SY-TABIX.

IF SY-SUBRC = 0.
ITAB3-MATNR_1 = ITAB2-MATNR_1.
ENDIF.

APPEND ITAB3.

ENDLOOP.

Hope this helps.

BR,

Suhas

14 REPLIES 14

Former Member
0 Kudos

Not sure but try to do with the join by werks and lifnr

Edited by: sgrshah on Jun 18, 2009 3:07 PM

Former Member
0 Kudos

Hi,

Loop at 1st internal table and read the 2nd internal table with the common fields.. now move the respective fields to the new internal table 'Itab3...

here's a sample code..


loop at itab1 into ws_area.

read itab2 into ws_itab2 with key werks_1 = ws_area-werks lifnr_1 = ws_area-lifnr
.

itab3-matnr = ws_area-matnr.
itab3-werks = ws_area-werks.
itab3-lifnr = ws_area-lifnr.
itab3-matnr1 = ws_itab2-matnr.
endloop.

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Karthik,

It is quite easy to do so using LOOP & READ statment combination.

NB: I am assuming you are combining the 2 tables based on Plant(WERKS) & Vendor(LIFNR)

The code will be:

DATA:

BEGIN OF ITAB3 OCCURS 0,
MATNR TYPE MATNR,
WERKS TYPE WERKS_D,
LIFNR TYPE LIFNR,
MATNR_1 TYPE MATNR,
END OF ITAB3.

SORT ITAB1 BY WERKS LIFNR.

SORT ITAB2 BY WERKS LIFNR. "-->This is important for READ TABLE

LOOP AT ITAB1.

ITAB3-MATNR = ITAB1-MATNR.
ITAB3-WERKS = ITAB1-MATNR.
ITAB3-LIFNR = ITAB1-LIFNR.

READ TABLE ITAB2 INDEX SY-TABIX.

IF SY-SUBRC = 0.
ITAB3-MATNR_1 = ITAB2-MATNR_1.
ENDIF.

APPEND ITAB3.

ENDLOOP.

Hope this helps.

BR,

Suhas

Former Member
0 Kudos

Hi Suhas,

Thanks a lot !! Ur logic is working.. but I have some other problem now:

ie. If in itab 1 I have

matnr    werks     lifnr
mat1     unit1      ABC
mat2     unit1      ABC

In itab2 the available records are:

matnr_1  werks_1  lifnr_1
mat5       unit1       ABC
mat6       unit1       ABC
mat7       unit1       ABC
mat8       unit1       ABC

I want my o/p as :

matnr    werks     lifnr    matnr_1 
mat1     unit1      ABC    mat5
mat2     unit1      ABC    mat6
                           mat7
                           mat8.

But as per ur logic I got the o/p as:

matnr    werks     lifnr    matnr_1 
mat1     unit1      ABC    mat5
mat2     unit1      ABC    mat6

Please advice

Karthik

Edited by: Karthik R on Jun 18, 2009 7:07 PM

Edited by: Karthik R on Jun 18, 2009 7:08 PM

0 Kudos

Hello,

I am not sure about the req. but still try this :

DATA:
 
BEGIN OF ITAB3 OCCURS 0,
MATNR TYPE MATNR,
WERKS TYPE WERKS_D,
LIFNR TYPE LIFNR,
MATNR_1 TYPE MATNR,
END OF ITAB3,

V_INDEX TYPE SY-TABIX.
 
SORT ITAB1 BY WERKS LIFNR.
 
SORT ITAB2 BY WERKS LIFNR. "-->This is important for READ TABLE
 
LOOP AT ITAB1.
 
ITAB3-MATNR = ITAB1-MATNR.
ITAB3-WERKS = ITAB1-MATNR.
ITAB3-LIFNR = ITAB1-LIFNR.
 
READ TABLE ITAB2 INDEX SY-TABIX.
 
IF SY-SUBRC = 0.
ITAB3-MATNR_1 = ITAB2-MATNR_1.
ENDIF.
 
APPEND ITAB3.

"--> Add this code to achieve your req.
AT LAST.
V_INDEX = SY-TABIX + 1.
LOOP AT ITAB2 FROM V_INDEX.
ITAB3-MATNR_1 = ITAB2-MATNR_1.
APPEND ITAB3.
ENDLOOP.
ENDAT.
 
ENDLOOP.

BR,

Suhas

Former Member
0 Kudos

U Rock !! ........Thanks a lot my issue is sold....

0 Kudos

>

> U Rock !! ........Thanks a lot my issue is sold....

Hello,

I am Suhas ... Plz donot make me a rock

Former Member
0 Kudos

Hello


data: counter type i.
loop at itab1.
  counter = counter + 1.
  clear itab3.
  itab3-matnr = itab1-matnr.
  itab3-werks = itab1-werks.
  itab3-lifnr = itab1-lifnr.
  read table itab2 index counter.
  if sy-subrc = 0.
    itab3-matnr_1 = itab2-matnr_1.
  endif.
  append itab3.
endloop.

Former Member
0 Kudos

Hi,

try this.(sample code)

SELECT ekpo~ebeln
          ekpo~matnr
         ekpo~werks
         ekpo~lifnr
         ekko~metnr_1
         ekko~lifnr_1
         ekko~werks_1
         INTO TABLE it_data
         FROM it_tab1 JOIN it_tab2 ON it_tab1~werks = it_tab2~werks.
       and it_tab1~matnr ne it_tab2~matnr_1
        and it_tab1~lifnr = it_tab2~lifnr.

hope it'll help.

Regards,

Sneha.

Former Member
0 Kudos

Hello,

You can simply use the command

"APPEND LINES OF itab1 [FROM idx1] [TO idx2] TO itab2. "

This command appends lines of itab1 to itab2 .Now, your table itab2 has all the entries.

In case if you want to append only certain lines from itab1 and itab2 which are common, you can follow the below procedure:

1. loop at itab1.

2. Read itab2 for the values of itab2.

3. append to table itab3.

Hope this helps,

Thanks,

Sowmya Arni

Former Member
0 Kudos

Hi Karthik,

data: begin of itab3 occurs 0,

matnr like itab1-matnr,

werks like itab1-werks,

lifnr like itab1-lifnr,

matnr_1 like itab2-matnr_1,

end of itab3.

Data: itab2_temp like itab2.

Itab2_temp[] = Itab2[]. " Total body move from itab2 to Itab2_temp

Loop at itab1.

clear itab2_temp.

Read table itab2_temp with key werks_1 = itab1-werks

lifnr_1 = itab1-lifnr.

If sy-subrc = 0.

itab3-matnr = itab1-matnr.

itab3-werks = itab1-werks.

itab3-lifnr = itab1-lifnr.

itab3-matnr_1= itab2_temp-matnr_1.

Delete itab2_temp.

Append itab3.

Clear itab3.

Endif.

Endloop.

Now you will have required output in Internal table Itab3.

Regards,

K

Edited by: K

Edited by: K

Edited by: Kishore Kumar Karnati on Jun 18, 2009 3:31 PM

Former Member
0 Kudos

Hi Karthik,

I could not get exact what logic you want?

Please explain functional requirement also so that I can answer your query.

Do you want to connect first line from itab1 to first line from itab2, 2nd line from itab1 to 2nd line from itab2 ????????Requirement should be clear.

Regards,

Anil

Former Member
0 Kudos

Loop at bigger itab, copy required fields to workarea and APPEND to itab3.

Loop at smaller itab, copy required fields to workarea and MODIFY itab3 using index.

Use DESCRIBE TABLE itab LENGTH lines. to find length of itab.

0 Kudos

Hi Manish ,

U too Rockkk....Ur logic is also working perfectly....

Thanks a lot !

Karthik