Question

I have an internal table itab which has some rows that one component of the rows matnr is empty. I want to check the palet column of the internal table and get matnr according to palet from the zlldet table, and then change the matnr column of internal table to new matnr value. Here is a summary;

itab table:

+-------+-------+-----------+
| palet | matnr | something |
+-------+-------+-----------+
|  1234 | null  | abc       |
|  1235 | null  | saa       |
|  1236 | null  | ssd       |
+-------+-------+-----------+

So, I will check the palet column from the zlldet table and find the matnr value of that row. The new itab values should be:

+-------+--------+-----------+
| palet | matnr  | something |
+-------+--------+-----------+
|  1234 | 543213 | abc       |
|  1235 | 988876 | saa       |
|  1236 | 344545 | ssd       |
+-------+--------+-----------+

What I've tried:

LOOP AT itab.
  SELECT SINGLE matnr INTO itab-matnr
    FROM zlldet WHERE palet = itab-palet.
  ENDIF.
ENDLOOP.

I am trying to change the value in the second line. I know it is wrong, I should use some MODIFY statement but I'm not sure how.

Edit:

Here is the complete code:

REPORT zwps0510 MESSAGE-ID zc LINE-SIZE 255 NO STANDARD PAGE HEADING.
TABLES: zzllog, zzldet, marc, makt.
DATA: BEGIN OF itab OCCURS 0.
    INCLUDE STRUCTURE zzllog.
DATA: END OF itab.

DATA: adet TYPE p.
*/ paket numaraları
SELECT-OPTIONS: spalet FOR zzllog-palet.      "/paket no
SELECT-OPTIONS: spaleta FOR zzllog-paleta.    "/paketnoa
SELECT-OPTIONS: spaletb FOR zzllog-paletb.    "/paketnob
SELECTION-SCREEN SKIP 1.
*/ paketle ilgili bilgiler
SELECT-OPTIONS: sdispo FOR marc-dispo.
SELECT-OPTIONS: smatnr FOR zzllog-matnr.
SELECT-OPTIONS: sharkod FOR zzllog-harkod.    "/logtaki işlem kodu
*SELECT-OPTIONS: stelf1 FOR zzllog-telf1.
SELECT-OPTIONS: starih FOR zzllog-tarih.
SELECT-OPTIONS: susr FOR zzllog-usr.
SELECT-OPTIONS: saufnr FOR zzllog-aufnr.
SELECTION-SCREEN SKIP 1.
*/ depo adres bilgileri
SELECT-OPTIONS: sflgnum FOR zzllog-flgnum.    "/kaynak depo no
SELECT-OPTIONS: sflgtyp FOR zzllog-flgtyp.    "/kaynak depo tipi
SELECT-OPTIONS: sflgpla FOR zzllog-flgpla.    "/kaynak depo numarası
SELECT-OPTIONS: stlgnum FOR zzllog-tlgnum.    "/hedef depo no
SELECT-OPTIONS: stlgtyp FOR zzllog-tlgtyp.    "/hedef depo tipi
SELECT-OPTIONS: stlgpla FOR zzllog-tlgpla.    "/hedef depo numarası
SELECTION-SCREEN SKIP 1.
*/ Çıktıda gelecek kolonların seçimi
SELECTION-SCREEN BEGIN OF BLOCK uc WITH FRAME TITLE text-001.
PARAMETERS : tarih AS CHECKBOX DEFAULT 'X',
         saat AS CHECKBOX DEFAULT 'X',
         usr AS CHECKBOX DEFAULT 'X',
         palet AS CHECKBOX DEFAULT 'X',
         paleta AS CHECKBOX DEFAULT 'X',
         paletb AS CHECKBOX DEFAULT 'X',
         harkod AS CHECKBOX DEFAULT 'X',  "/ hareket kodu
         matnr AS CHECKBOX DEFAULT 'X',
         menge AS CHECKBOX DEFAULT 'X',
         sebep AS CHECKBOX DEFAULT 'X',  "/ sipariş/red
         aufnr AS CHECKBOX DEFAULT 'X',  "/sm.siparişi
         ref2 AS CHECKBOX DEFAULT 'X',
         paufnr AS CHECKBOX DEFAULT 'X', "/enj.siparişi
         flgnum AS CHECKBOX DEFAULT 'X', "/from depo...
         flgtyp AS CHECKBOX DEFAULT 'X',
         flgpla AS CHECKBOX DEFAULT 'X',
         tlgnum AS CHECKBOX DEFAULT 'X', "/ to depo...
         tlgtyp AS CHECKBOX DEFAULT 'X',
         tlgpla AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF BLOCK uc.
*
TOP-OF-PAGE.
   PERFORM baslik.
*
START-OF-SELECTION.
  PERFORM prepare_itab.
  PERFORM write_itab.
**
FORM prepare_itab.
*/ Paketle ilgili tüm geçmiş hareketler log tablosundan okunur
  SELECT * INTO CORRESPONDING FIELDS OF TABLE itab
      FROM zzllog WHERE palet IN spalet
                    AND paleta IN spaleta
                    AND paletb IN spaletb
                    AND matnr IN smatnr
                    AND harkod IN sharkod
                    AND tarih IN starih
                    AND flgnum IN sflgnum
                    AND flgtyp IN sflgtyp
                    AND flgpla IN sflgpla
                    AND tlgnum IN stlgnum
                    AND tlgtyp IN stlgtyp
                    AND tlgpla IN stlgpla
                    AND aufnr IN saufnr
                    AND usr IN susr.
  IF sdispo[] IS NOT INITIAL.
    LOOP AT itab.
      SELECT SINGLE dispo INTO marc-dispo
                FROM marc WHERE matnr = itab-matnr
                            AND werks = '3001'
                            AND dispo IN sdispo.
      IF sy-subrc <> 0.
        DELETE itab.
      ENDIF.
    ENDLOOP.
  ENDIF.




  DESCRIBE TABLE itab LINES adet.
*/ tüm hareketler tarih ve saate göre sıralanır
 SORT itab BY tarih saat.
 WRITE:/ 'ADET: ', adet.
 ULINE.
   ENDFORM.   

So, what I want to do is exactly: if sharkod includes D, zzldet should be checked for the matnr.

Was it helpful?

Solution

When you want to do it with modify and not with a field symbol, that would be the correct solution:

LOOP AT itab.
  SELECT SINGLE matnr INTO itab-matnr
    FROM zlldet WHERE palet = itab-palet.
  MODIFY itab.
ENDLOOP.

You declared itab as a table with header line (which is deprecated, by the way). That means you have a table itab and a structure itab. Which one is used in which situation depends on context, and some commands, like LOOP AT and MODIFY, use both at the same time. This would be the code with a work area instead of a header line. It's more readable.

DATA itab TYPE TABLE OF [something].
DATA wa TYPE [something].

LOOP AT itab INTO wa.  " copies each line into wa
  SELECT SINGLE matnr INTO wa-matnr
    FROM zlldet WHERE palet = itab-palet.
  MODIFY itab FROM wa.  " writes the changed line back to the table
ENDLOOP.

You can also use the solution with a field symbol vwegert described. A field symbol is a pointer to the line of the table. Any changes to the field symbol will be automatically written back to the table, so you don't need MODIFY in this case. It is also faster, especially with very wide tables, because no data needs to be copied.

OTHER TIPS

Instead of using MODIFY, use a field symbol:

DATA: lt_materials TYPE TABLE OF zzllog.
FIELD-SYMBOLS: <ls_line> TYPE zzllog.

* other code with strange comments :-)

LOOP AT lt_materials ASSIGNING <ls_line> WHERE matnr IS INITIAL.
  SELECT SINGLE MATNR
    FROM zlldet
    INTO <ls_line>-matnr
    WHERE palet = <ls_line>-palet.
ENDLOOP.

This is just an example for the correct syntax. It's not a good idea to do this in a real program because there's a fair chance thtat you'll be hitting the database with thousands of requests, and potentially draw quite a bit of performance. It'd be better to preselect the PALET numbers that have no MATNR into a separate table and then use FOR ALL ENTRIES IN to read all the MATNRs in a single query.

EDIT: Added type names. BTW, your way of declaring the internal table is somewhat outdated...

You can also avoid the select statement every time inside the loop. Instead, you can try making use of Range tables and make use of select statement only once before the loop since it increases performance.

Define user defined structure and declare variables

types: begin of ty_p_m,
         palet type zlldet-palet,
         matnr type zlldet-matnr,
       end of ty_p_m.

data: r_palet type range table of zlldet-palet,
      r_line_palet like line of r_palet,
      i_p_matnr type standard table of ty_p_m,
      wa_p_m type ty_p_m.


field-symbols: <fs> type wa_p_m.

Fill the range table which is to be used in the select query in the next step.

r_line_palet-sign = 'I'.
r_line_palet-option = 'EQ'.

loop at itab into wa.
r_line_palet-low = wa-palet.
append  r_line_palet to r_palet.
endloop.

now use the select statement by referring the range table and get the data inside the declared internal table.

select palet matnr from zlldet into i_p_matnr
where palet in r_palet.

Now loop on declared internal table. Inside that read your internal table with incoming values and modify the table there itself using field symbols instead of modify statement.

loop at i_p_matnr into wa_p_matnr.
read table itab with key palet = wa_p_matnr-palet assigning <fs>.
if sy-subrc = 0.
<fs>-matnr = wa_p_matnr-matnr.
endif.
clear wa_p_matnr.
endloop.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top