‎2007 Jan 25 3:59 PM
hi,
how to modify a internal table records which is within a loop and endloop.
‎2007 Jan 25 4:00 PM
HI,
check this....
<b>Loop at itab.
itab-field1 = ' ABCD'.
itab-field2 = '1234'.
Modify Itab transporting field1 field2.
Endloop.</b>
u can also add where condition like ..
Modify Itab transporting field1 field2 where field3 = 'XYZ'.
Regards
CNU
‎2007 Jan 25 4:02 PM
loop at itab.
itab-field1 = <newvalue>.
modify itab index sy-tabix.
endloop.
loop at itab.
v_tabix = sy-tabix.
read table itab1 with key <field> = <itab-field>.
if sy-subrc = 0.
itab-field1 = <newvalue>.
endif.
modify itab index v_tabix.
endloop.
loop at itab.
v_tabix = sy-tabix.
read table itab1 with key <field> = <itab-field>.
if sy-subrc = 0.
itab1-field1 = <newvalue>.
modify itab1 index sy-tabix.
endif.
endloop.
loop at itab.
v_tabix = sy-tabix.
read table itab1 with key <field> = <itab-field>.
if sy-subrc = 0.
itab-field1 = <newvalue>.
itab1-field1 = <newvalue>.
modify itab1 index sy-tabix.
endif.
modify itab index v_tabix.
endloop.
Message was edited by:
Ramesh Babu Chirumamilla
‎2007 Jan 25 4:02 PM
hi,
loop at itab
......
.......
modify itab
endloop.
if you are using work areas
loop at itab into wa
modify itab from wa transporting <fldname which is modified>
endloop.
reward points if this helps
‎2007 Jan 25 4:04 PM
HI,
MODIFY itab [FROM wa] [INDEX idx] [TRANSPORTING f1 ... fn].
2. MODIFY TABLE itab [FROM wa] [TRANSPORTING f1 ... fn].
3. MODIFY itab [FROM wa] TRANSPORTING f1 ... fn WHERE cond.
See Short forms not allowed with line ooperations.
Variant 1
MODIFY itab [FROM wa] [INDEX idx] [TRANSPORTING f1 ... fn].
Effect
Changes a single entry in the internaltable itab, specifying the key explicitly or implicitly.You can only use this variant with index table (standard or sortedtables).
If you specify "FROM wa", the new values are taken from the workarea wa. If you do not specify FROM, the header line ofitab is used as the work area.
You can use "INDEX idx" to specify the table index of the lineyou want to change. This may be omitted within a LOOP at an internal table. In this case, the currenttable line is changed.
The INDEX specification can come before the FROMaddition.
If you specify "TRANSPORTING f1 ... fn ", only components f1, f2, ... of the work area are copied into the table. You canalso specify components dynamically in the form (name). Theactual component name is then taken from the field name atruntime. If name contains an invalid component name, the systemtriggers a runtime error. You may not use a key field as a TRANSPORTING field with HASHED or SORTED tables.
The return code is set as follows:
When you specify the insertion point with INDEX idx:
SY-SUBRC = 0:
The entry was changed.
SY-SUBRC = 4:
Index position too large. No entry was changed,since the table contains less than idx entries.
If you do not specify an insertion point, return code is set to 0.
Variant 2
MODIFY TABLE itab [FROM wa] [TRANSPORTING f1 ...fn].
Effect
Generic change to a single entry in the internal table itab with key. Unlike variant 1, youcan use this variant for any table.
You can use "FROM wa" and "TRANSPORTING f1 ... fn" in thesame way as in variant 1. If you do not use the TRANSPORTINGaddition, only the non-key fields are transported.
The key of the entry to be changed is taken from the work area. Theprocedure depends on the table type:
STANDARD TABLE:
The table is searched sequentially by its table key. The first entryfound is changed. The runtime required for the search is in linearrelation to the number of table entries.
SORTED TABLE:
The table is searched by its table key using a binary search. In tableswith a non-unique key ( NON-UNIQUE) only the first entry in thelist of duplicates is changed. The runtime required for the search isin logarithmic relation to the number of table entries.
HASHED TABLE:
The entry is found and changed by table key using the internal hashadministration. The runtime required is essentially constant, since,unlike in standard or hashed tables, it does not depend on the numberof table entries.
The return code is set as follows:
SY-SUBRC = 0:
The first entry with the specified key was changed.
SY-SUBRC = 4:
There was no entry with the specified key.
Variant 3
MODIFY itab [FROM wa] TRANSPORTING f1 ... fnWHERE cond.
Effect
Changes several entries in the internal table itab. You can use this variant for anytable.
You can use " FROM wa" and "TRANSPORTING f1 ... fn" as invariant 1. If the table has the type SORTED TABLE orHASHED TABLE, the TRANSPORTING list may notcontain key fields.
In comparisons within the logical expressioncond, the first operand must always be a subfield of the linestructure of itab.
The return code is set as follows:
SY-SUBRC = 0:
At least one entry was changed.
SY-SUBRC = 4:
None of the entries was changed.
Notes
When you use the WHERE condition with a STANDARD orHASHED table, a full table scan is always required.
If you are working with a SORTED TABLE, the partial sequentialprocessing can be optimized so that only the entries that actuallysatisfy the WHERE condition are processed. This optimizationrequires that the WHERE condition be specified in the form
WHERE k1 = v1 AND k2 = v2 AND ... AND kn = vn
where the components k1, ..., kn are in the same sequence as thebeginning of the table key.
All operands in the WHERE condition that are not components ofthe internal table itab can be replaced with a functional methodcall. Functional methods are methods in ABAPObjects that can have any number of IMPORTINGparameters and a single RETURNING parameter.
If the line type of the internal table contains object referencevariables as components, or the entire line type is a referencevariable, you can use the attributes of the object to which thereference refers as values in the WHERE condition (seeAttributes of objects asthe key of internal tables).
Example
Reset a status flag universally
DATA: BEGIN OF TAB OCCURS 500,
FLAG TYPE C,
COMP(20) TYPE C,
END OF TAB.
CLEAR TAB-FLAG.
MODIFY TAB TRANSPORTING FLAG WHERE FLAG = 'X'.
Notes
General:
If you use the TRANSPORTING addition together with anexplicitly-specified work area, the work area must be compatible withthe line type of the internal table.
The counter for table entries begins at 1.
A unique or summarized dataset built up using an internal table and COLLECT can be modified while you areconstructing it using the "MODIFY ... TRANSPORTING ..."statement, as long as the components selected with TRANSPORTINGare not contained in the key of the internal table. This method doesnot cause significant performance impairment.
Regards,
Vara
‎2007 Jan 25 4:10 PM
Hello,
If it is inside the loop u could use just <b>modify itab</b>
Vasanth
‎2007 Jan 25 5:14 PM
If you want to modify an internal table on which you are looping then performance-wise best way is to declare a field-symbol typing it to the line of the internal table.
Now use
LOOP AT ITAB ASSIGINING <FIELD-SYMBOL1>.
<FIELD-SYMBOL1>-FIELD1 = 'FGHI'. "modify the fields here
<FIELD-SYMBOL1>-FIELD2 = '5678. "modify the fields here
......
ENDLOOP.
In this way the overhead of copying the fields first into work area and then back to the itab is eliminated. There is no need of even using the index for modifying a particular line of ITAB. A WHERE clause can also be use if needed.
Thanks
Sanjeev
‎2007 Jan 25 5:45 PM
best way is to use a workarea which is the easiest way
loop at itab into wa_itab.
modify itab from wa_itab.
endloop.
‎2007 Jan 25 6:03 PM
‎2007 Jan 25 11:24 PM
<b>This is the efficient way to do.</b>
FIELD-SYMBOLS: <fs_wa> type any.
FIELD-SYMBOLS: <fs_field> type any.
LOOP AT X_ITAB ASSIGNING <fs_wa>.
<fs_wa>-text1 = 'text'.
ENDLOOP.