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

se38 -Code

Former Member
0 Likes
2,562

Hello friends,

I have 2 internal tables of the same structure.

I need to insert data into ITAB1 from ITAB2, based on where condition on ITAB1.

IN ITAB1, i have vbeln = 'P01' and is found multiple time. But i want to insert before the last 'P01' in Itab1.

Let me know if i am not clear.

Below is an example of data in ITAB1 and ITAB2.

MAdhu.

DATA : BEGIN OF itab1 OCCURS 0,

vbeln LIKE vbak-vbeln,

matnr LIKE mara-matnr,

END OF itab1.

DATA : BEGIN OF itab2 OCCURS 0,

vbeln LIKE vbak-vbeln,

matnr LIKE mara-matnr,

END OF itab2.

itab1-vbeln = 'A01'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'B01'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'P01'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'P04'.

itab1-matnr = '231'.

APPEND itab1.

itab1-vbeln = 'P20'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'P04'.

itab1-matnr = '231'.

APPEND itab1.

itab1-vbeln = 'P20'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'PT1'.

itab1-matnr = '111'.

APPEND itab1.

itab1-vbeln = 'PT2'.

itab1-matnr = '121'.

APPEND itab1.

itab1-vbeln = 'PT2'.

itab1-matnr = '222'.

APPEND itab1.

itab1-vbeln = 'P01'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'PA1'.

itab1-matnr = '231'.

APPEND itab1.

itab1-vbeln = 'P04'.

itab1-matnr = '234'.

APPEND itab1.

itab1-vbeln = 'PT2'.

itab1-matnr = '1212'.

APPEND itab1.

itab1-vbeln = 'P01'.

itab1-matnr = '2222'.

APPEND itab1.

itab1-vbeln = 'PT1'.

itab1-matnr = '1112'.

APPEND itab1.

itab2-vbeln = 'PT1'.

itab2-matnr = 'MADHU'.

APPEND itab2.

itab2-vbeln = 'PT2'.

itab2-matnr = '1'.

APPEND itab2.

itab2-vbeln = 'PT2'.

itab2-matnr = '2'.

APPEND itab2.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,525

Hi,

Try this..

LOOP AT ITAB1 WHERE VBELN = 'P01'.

***dummy loop..

ENDLOOP.

V_TABIX = SY-TABIX.

LOOP AT ITAB2.

MOVE ITAB2 TO ITAB1.

INSERT ITAB1 INDEX V_TABIX.

V_TABIX = V_TABIX + 1.

ENDLOOP.

Hope this is what you want..

Thanks,

Naren

27 REPLIES 27
Read only

Former Member
0 Likes
2,525

Try this:

LOOP AT ITAB1.

AT END OF VBELN.

IF ITAB1-VBELN EQ 'P01'.

ITAB2-VBELN = ITAB1-VBELN.

ITAB2-MATNR = ITAB1-MATNR.

APPEND ITAB2.

CLEAR ITAB2.

ENDIF.

ENDAT.

ENDLOOP.

Thanks,

Santosh

Read only

0 Likes
2,525

Thanks SKJ,

I dont want to append i want to insert to ITAB1 and before the last hit where vbeln - 'P01' in ITAB1.

Thanks for the suggestion,

MAdhu.

Read only

0 Likes
2,525

Try this:

SORT ITAB2 BY VBELN.

LOOP AT ITAB2.

AT END OF VBELN.

INSERT LINES OF ITAB2 INDEX SY-TABIX INTO ITAB1.

CLEAR ITAB1.

ENDAT.

ENDLOOP.

Thanks,

Santosh

Read only

Former Member
0 Likes
2,525

Hi,

You can sort the internal table by VBELN..and do AT END OF vbeln..

SORT ITAB2 by vbeln.

LOOP AT ITAB2.

AT END OF VBELN.

****Do your change here.

ENDAT.

ENDLOOP.

Thanks,

Naren

Read only

0 Likes
2,525

Thanks Naren for the suggestion.

Cant sort the table. I need to maintain the order which they are in. Its an IDOC program for segments and the order is important.

Madhu.

Read only

0 Likes
2,525

hey check this.

data: v_tabix type sy-tabix.

loop at itab1 where vbeln = 'P01'.

v_tabix = sy-tabix.

endloop.

loop at itab1 from v_tabix.

insert lines of itab2 into itab1.

exit.

endloop.

Read only

Former Member
0 Likes
2,525

Hi Madhu,

Check this code.

Try this:

DATA: WA_ITAB1 TYPE LINE OF ITAB1.

SORT ITAB1 BY VBELN.

LOOP AT ITAB1.

AT END OF VBELN.

READ TABLE ITAB1 INDEX SY-TABIX INTO WA_ITAB1.

IF ITAB1-VBELN EQ 'P01'.

ITAB2-VBELN = WA_ITAB1-VBELN.

ITAB2-MATNR = WA_ITAB1-MATNR.

APPEND WA_ITAB1 TO ITAB2.

CLEAR WA_ITAB1.

ENDIF.

ENDAT.

ENDLOOP.

Thanks,

Vinay

Read only

0 Likes
2,525

Thanks Vinay,

I need to modify ITAB1, not ITAB2 and I cant sort.

Any more suggestions,

Madhu. Let me know if I am not clear.

Madhu.

Read only

Former Member
0 Likes
2,525

Hi,

Okay...Let me understand the requirement clearly..

You want to add the from internal table ITAB2 to ITAB1 ...

Sorry..Please explain clearly..

Thanks,

Naren

Read only

0 Likes
2,525

yes Naren I want to add from itab2 to itab1.

where itab1-vbeln = 'p01' and should be the last hit itab1-vbeln = 'P01'.

In this example if you loop itab1 where vbeln = 'P01' the first time u will hit sy-tabix = 3 and the next time you will hit sy-tabix = 11.

so i need to insert data from itab2 in itab1 starting from sy-tabix 11.

Hope i am clear..let me know.

Madhu.

Read only

0 Likes
2,525

check my code & award points if it helps you.

Read only

0 Likes
2,525

Try this:

DATA: BEGIN OF IT_INDEX OCCURS 0,

TABIX LIKE SY-TABIX,

END OF IT_INDEX.

DATA: LV_LINES TYPE I,

LV_TABIX LIKE SY-TABIX.

  • Store tabix number of occurence of 'P01'

LOOP AT ITAB2.

IF ITAB2-VBELN = 'P01'.

IT_INDEX-TABIX = SY-TABIX.

APPEND IT_INDEX.

CLEAR IT_INDEX.

ENDLOOP.

  • Take the last tabix

DESCRIBE TABLE IT_INDEX LINES LV_LINES.

READ TABLE IT_INDEX INDEX LV_LINES.

IF SY-SUBRC EQ 0.

LV_TABIX = IT_INDEX-TABIX.

ENDIF.

  • Store rows into ITAB1 from last tabix of P01

INSERT LINES OF ITAB2 FROM LV_TABIX INTO ITAB1.

Thanks,

Santosh

Read only

0 Likes
2,525

Hi SKJ, where is the Cursor for ITAB1 ? You have to write this in loop

check my code madhu, with out sorting i had given the solution first. But i have copied your code too so may be you got confused.

Read only

0 Likes
2,525

Hi Madhu,

Check this code.

DATA V_INDEX TYPE SY-TABIX.

LOOP AT ITAB2 WHERE VBELN = 'P01'.

V_INDEX = SY-TABIX.

ENDLOOP.

INSERT LINES OF ITAB2 FROM V_INDEX INTO ITAB1.

Thanks,

Vinay

Read only

0 Likes
2,525

madhu you have to come out of the loop of itab1 after capturing the sy-tabix of the last record and then process the table again in another loop.

This will work..

data: v_tabix type sy-tabix.

loop at itab1 where vbeln = 'P01'.

v_tabix = sy-tabix.

endloop.

loop at itab1 from v_tabix.

insert lines of itab2 into itab1.

exit.

endloop.

Read only

0 Likes
2,525

I have looked in your code, This code inserts data for the first hit in ITAB1. I need to insert for the last Hit.

Madhu

Read only

0 Likes
2,525

No, I am sure it inserts in the last hit only because i have tested that as well.

v_tabix value is 15 after you come out of the loop.

Let me know what exactly is your problem.

loop at itab1 where vbeln = 'P01'.

v_tabix = sy-tabix.

endloop.

  • V_tabix is 15 here as per your example

loop at itab1 from v_tabix.

  • inserts the 3 records from itab2 to itab1 after the 15th record

insert lines of itab2 into itab1.

exit.

endloop.

Read only

0 Likes
2,525

Thanks Vinni, Your code actually solved the problem. I didnt see it. Anyways thanks again.

Naren the final code that u have sent also works.

Thanks all for their time.

Madhu.

Read only

Former Member
0 Likes
2,526

Hi,

Try this..

LOOP AT ITAB1 WHERE VBELN = 'P01'.

***dummy loop..

ENDLOOP.

V_TABIX = SY-TABIX.

LOOP AT ITAB2.

MOVE ITAB2 TO ITAB1.

INSERT ITAB1 INDEX V_TABIX.

V_TABIX = V_TABIX + 1.

ENDLOOP.

Hope this is what you want..

Thanks,

Naren

Read only

0 Likes
2,525

Thanks Naren.

When I am looping the dummy loop for sy-tabix. First the value is 3, 11 and 15. But when it comes to the next statement V_TABIX = SY-TABIX. the value od sy-tabix go back to 3.

I dont understant why?

I added anothere line to ITAB 4 and made it 4 rows and now when i execute the program and when i caome to V_TABIX = SY-TABIX. the value od sy-tabix go back to 4.

What could be the reason.

Madhu

Read only

0 Likes
2,525

Madhu - Did you get a chance to look at my code?

Thanks,

Santosh

Read only

0 Likes
2,525

Santosh,

why are u looping itab2 where vbeln = 'P01'

LOOP AT itab2.

IF itab2-vbeln = 'P01'.

it_index-tabix = sy-tabix.

APPEND it_index.

CLEAR it_index.

ENDIF.

ENDLOOP.

Read only

0 Likes
2,525

madhu, Is your problem solved ?

Read only

0 Likes
2,525

I have modified the code now.

DATA: BEGIN OF IT_INDEX OCCURS 0,

TABIX LIKE SY-TABIX,

END OF IT_INDEX.

DATA: LV_LINES TYPE I,

LV_TABIX LIKE SY-TABIX.

  • Store tabix number of occurence of 'P01'

LOOP AT ITAB1.

IF ITAB1-VBELN = 'P01'.

IT_INDEX-TABIX = SY-TABIX.

APPEND IT_INDEX.

CLEAR IT_INDEX.

ENDLOOP.

  • Take the last tabix

DESCRIBE TABLE IT_INDEX LINES LV_LINES.

READ TABLE IT_INDEX INDEX LV_LINES.

IF SY-SUBRC EQ 0.

LV_TABIX = IT_INDEX-TABIX.

LV_TABIX = LV_TABIX - 1.

ENDIF.

  • Store rows into ITAB1 from last tabix of P01

INSERT LINES OF ITAB2 INTO ITAB1 INDEX LV_TABIX.

Thanks,

Santosh

Read only

0 Likes
2,525

Check the result of my code

A01 234

B01 234

P01 234

P04 231

P20 234

P04 231

P20 234

PT1 111

PT2 121

PT2 222

P01 234

PA1 231

P04 234

PT2 1212

PT1 MADHU " itab2 data

PT2 1

PT2 2

<u><b>P01 2222 </b> </u> " last occurance of P01 from ur example

PT1 1112

Read only

Former Member
0 Likes
2,525

try out this

Try this:

LOOP AT ITAB1 where vbelm EQ 'P01'.

AT LAST.

READ ITAB2 into WA_ITAB2 with key (specify the key).

wa_ITAB1-VBELN = wa_ITAB2-VBELN.

wa_ITAB1-MATNR = wa_ITAB2-MATNR.

APPEND wa_ITAB1 to ITAB1.

CLEAR wa_ITAB1.

ENDAT.

ENDLOOP.

hope this will help

Ashwani

Read only

Former Member
0 Likes
2,525

Hi,

Try this..

LOOP AT ITAB1 WHERE VBELN = 'P01'.

V_TABIX = SY-TABIX.

ENDLOOP.

LOOP AT ITAB2.

MOVE ITAB2 TO ITAB1.

INSERT ITAB1 INDEX V_TABIX.

V_TABIX = V_TABIX + 1.

ENDLOOP.

Thanks,

Naren