Changing value of itab

Multi tool use
Changing value of itab
I'm a newbie in ABAP. I have an issue that I couldn't sort out which should be not that hard to implement.
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
matnr
palet
matnr
palet
zlldet
matnr
matnr
itab
table:
itab
+-------+-------+-----------+
| 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
zlldet
matnr
itab
+-------+--------+-----------+
| 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.
MODIFY
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
.
sharkod
D
zzldet
matnr
3 Answers
3
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.
If you want / need to use MODIFY to change a single field, you should at least specify the TRANSPORTING clause...
– vwegert
Sep 7 '12 at 15:18
it is not copying any line to wa. ? Maybe my itab is a little bit differen :) please see the edit.
– Mtok
Sep 10 '12 at 6:24
ok I finally did it. Thanks!
– Mtok
Sep 10 '12 at 7:42
Although the answer might work it's really a bad approach to use a SELECT statement inside a loop. This might (and probably will) led to avoidable and undesirable performances issues in your program/exit/job/etc. Instead, filter all values from your itab which will require data from zlldet. Then, use a SELECT statement which will be called once (outside a loop) and fetching all data you need to update the internal table. Just after that, update your internal table with the help of a second internal table which contains the result of your SELECT.
– fabiopagoti
Sep 10 '12 at 22:25
Instead of using MODIFY
, use a field symbol:
MODIFY
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 MATNR
s in a single query.
PALET
MATNR
FOR ALL ENTRIES IN
MATNR
EDIT: Added type names. BTW, your way of declaring the internal table is somewhat outdated...
I tried field-symbol but it says : The row type of the table "ITAB" is not compatible with symbol field type "<ls_line>".
– Mtok
Sep 10 '12 at 5:46
Then please show us a complete code sample and we might be able to help you. With what you've provided so far, I've had to guess the data types.
– vwegert
Sep 10 '12 at 5:57
please see my edit.
– Mtok
Sep 10 '12 at 6:22
what I want to do is to check zzldet for the matnr if sharkod includes 'D'.
– Mtok
Sep 10 '12 at 6:23
thanks vwegert I solved the problem with modify which is Phillips answer. I'll try this one after this edit also! thanks.
– Mtok
Sep 10 '12 at 7:43
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You seem to have declared itab as a table WITH HEADER LINE. Header lines are deprecated and shouldn't be used anymore. Loop into a work area instead or use a field symbol like in the answer by vwegert.
– Philipp
Sep 7 '12 at 11:48