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: 

SELECT while MODIFYING existing rows

0 Kudos

Hello,

I have an internal table with material, plant, PSS status and some other fields that is already full of data. I want to select the material description from MAKT using the entries in this table and modifying this table at the same time:

SELECT maktx FROM makt INTO CORRESPONDING FIELDS OF TABLE itab

FOR ALL ENTRIES IN itab

WHERE matnr = itab-matnr.

However, this deletes the current entries.

I also tried to use SELECT .... APPENDING INTO CORRESPONDING FIELDS OF TABLE

but am unsure if I am doing it correctly or if there is a different way to do it because the way I have written it, it adds additional rows to my table.

I am aware I can create a separate internal table and then loop through it to modify itab but was trying to see if there is a more efficient way to do this.

Thank you!

9 REPLIES 9

former_member226239
Contributor
0 Kudos

I think with the newer versions (7.40) there is an option to get a column data with out deleting current internal table data but with older versions you really don't have an option (as you mentioned, need to get material text data into a new internal table and then update the original internal table). Sorry...

-Chandra

Former Member
0 Kudos

not sure if this is possible. But, if your concern with the performance, you can use Loop assigning field symbol instead of Loop-Modify.

Chintu6august
Contributor
0 Kudos

Hi,

INTO CORRESPONDING FIELDS OF <workarea>, ... INTO CORRESPONDING FIELDS OF TABLE <itab> and ... APPENDING CORRESPONDING FIELDS OF TABLE <IT_TAB>

require a slightly increased runtime and degrade the performance.

so it will be a better to fetch all the data new internal table and make use of field symbols to modify your main internal table.

thanks!!

matt
Active Contributor
0 Kudos

The best solution is to populate it all in one go by using a JOIN on MAKT.

If that's not possible, select into a hashed internal table of MATNR and MAKTX (using FOR ALL ENTRIES to limit the scope). Then loop through "itab" assigning to a field symbol, read your hashed table, and populate the description field.

Former Member
0 Kudos

Hi,

You can use make use of field symbols for this.

E.g.

* Make sure there are no duplicate materials in the ITAB, else make a read table statement before select query.

LOOP AT itab ASSIGNING <fs_wa>.

SELECT SINGLE maktx

          FROM makt

          INTO <fs_wa>-maktx " Your field for material description

          WHERE matnr = <fs_wa>-matnr.              

ENDLOOP.

Regards,

Gaurav

matt
Active Contributor
0 Kudos

Not the most efficient way - having a select inside a loop.

0 Kudos

Hi Matthew,

Correct, this is not the most efficient way,

So, either it can be done as.

" Check for iTAB non-initial.

SELECT matnr maktx

     FROM makt

     INTO TABLE itab_temp  " Temp table with Matnr and Maktx.

FOR ALL ENTRIES IN itab

WHERE matnr = itab-matnr.

SORT itab_temp BY matnr.

LOOP AT itab ASSIGNING <fs_wa>.

READ TABLE itab_temp INTO ls_itab_temp WITH KEY matnr = <fs_wa>-matnr BINARY SEARCH.

IF    sy-subrc IS INITIAL.

     <fs_wa>-maktx = ls_itab_temp-maktx.

ENDIF.

             

ENDLOOP.

P.S. : I din't considered the performance factor before.

Regards,

Gaurav

Former Member
0 Kudos

Hi,

As you said

I am aware I can create a separate internal table and then loop through it to modify itab but was trying to see if there is a more efficient way to do this.


to increase performance pls move the records to final IT table (avoid modify).


please go through below.

********data declaration

create itabs gt_mara

                  gt_makt

                  gt_final

********start-of-selection

select <f1> <f2> ... from mara into table gt_mara

                           where mara in so_matnr.

if sy-subrc = 0.

select <f1> <f2>.. from  makt into table gt_makt

                         for all entries in gt_mara

                       where matnr = gt_mara-matnr

                      and spras = 'EN'.

endif.

***********process

sort : gt_mara by matnr,

        gt_makt by matnr.

loop at gt_mara into gs_mara.

gs_final-<f1> = gs_mara-<f1>.

read table gt_makt into gs_makt

               binarysearch.

if sy-subrc = 0.

gs_final-<fn> = gs_makt-<fn>.

endif.

append gs_final to gt_final

clear: gs_final,gs_mara, gs_makt.

endloop.

matt
Active Contributor
0 Kudos

anusha pasula wrote:

select <f1> <f2> ... from mara into table gt_mara

                           where mara in so_matnr.

if sy-subrc = 0.

select <f1> <f2>.. from  makt into table gt_makt

                         for all entries in gt_mara

                       where matnr = gt_mara-matnr

                      and spras = 'EN'.

endif.

Definitely not an efficient way. Don't use FOR ALL ENTRIES. Use a LEFT OUTER JOIN with MARA and MAKT.