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

UPDATE outside the Loop ......

Former Member
0 Likes
2,568

Hi ,

Currently in my code i have a update statement inside the loop. i want to move it out side. My criteria is to modify any number of records. I don't this MODIFY statement will be correct.

 
 LOOP AT t_zvei.

 UPDATE zxxvei SET
                                 ardoc       =  t_zvei-ardoc
                      where    
                               mjahr         =  t_zvei-mjahr
                               monat        =  t_zvei-monat.


 if sy-subrc eq 0.
   
       commit work.

endloop. 

endloop.

the problem with MODIFY is that it gives sy-subrc eq 0 only if all records are modified . But i want to commit even if any record is changed.

I want to change only few columns comparing the key ffileds.

Please help

1 ACCEPTED SOLUTION
Read only

venkat_o
Active Contributor
0 Likes
1,587

Hi, <li> Try like below which suggests that bring database table data into internal table and make changes to that internal table and update the same to the database.


REPORT ztest.
DATA: BEGIN OF it_data OCCURS 0,
        bukrs TYPE t001-bukrs,
        rcomp TYPE t001-rcomp,
      END OF it_data.
DATA:it_t001 TYPE STANDARD TABLE OF t001 WITH HEADER LINE.

START-OF-SELECTION.
  "Select required records from database table
  SELECT * FROM t001 INTO TABLE it_t001.
  SORT it_t001."It is sorted based on key fields.

  "Get your own data
  "Loop your internal table which has data to update database table.
  LOOP AT it_data.
    READ TABLE it_t001 WITH KEY bukrs = it_data-bukrs.
    IF sy-subrc EQ 0.
      it_t001-rcomp = 'XYZ Pte ltd'.
      MODIFY it_t001 INDEX sy-tabix TRANSPORTING rcomp.
      CLEAR it_t001.
    ENDIF.
  ENDLOOP.
  "Modify database table at a time using internal table
  MODIFY t001 FROM TABLE it_t001.
  IF sy-subrc EQ 0.
    WRITE 'All lines were inserted or changed.'.
  ELSE.
    WRITE 'At least one line could not be processed'.
  ENDIF.
Thanks Venkat.O

6 REPLIES 6
Read only

venkat_o
Active Contributor
0 Likes
1,588

Hi, <li> Try like below which suggests that bring database table data into internal table and make changes to that internal table and update the same to the database.


REPORT ztest.
DATA: BEGIN OF it_data OCCURS 0,
        bukrs TYPE t001-bukrs,
        rcomp TYPE t001-rcomp,
      END OF it_data.
DATA:it_t001 TYPE STANDARD TABLE OF t001 WITH HEADER LINE.

START-OF-SELECTION.
  "Select required records from database table
  SELECT * FROM t001 INTO TABLE it_t001.
  SORT it_t001."It is sorted based on key fields.

  "Get your own data
  "Loop your internal table which has data to update database table.
  LOOP AT it_data.
    READ TABLE it_t001 WITH KEY bukrs = it_data-bukrs.
    IF sy-subrc EQ 0.
      it_t001-rcomp = 'XYZ Pte ltd'.
      MODIFY it_t001 INDEX sy-tabix TRANSPORTING rcomp.
      CLEAR it_t001.
    ENDIF.
  ENDLOOP.
  "Modify database table at a time using internal table
  MODIFY t001 FROM TABLE it_t001.
  IF sy-subrc EQ 0.
    WRITE 'All lines were inserted or changed.'.
  ELSE.
    WRITE 'At least one line could not be processed'.
  ENDIF.
Thanks Venkat.O

Read only

kiran_k8
Active Contributor
0 Likes
1,587

Venkat,

No where it_data is getting filled before the loop.So,do you think that it will loop the it_data internal table even when there is no data in it ?

K.Kiran.

Read only

venkat_o
Active Contributor
0 Likes
1,587

My dear friend Kiran, You can see the comment written Before LOOP statment "Get your own data Thanks Venkat.O

Read only

kiran_k8
Active Contributor
0 Likes
1,587

Venkat,

Ok,didn't checked that.No Offences meant.

Happy Diwali

K.Kiran.

Read only

Former Member
0 Likes
1,587

No friends ... I have not all data for the DB table to be modified. I have only few fields that needs to be modified depending upon some criteria.

Read only

Former Member
0 Likes
1,587

no ans