‎2007 Jul 03 12:29 PM
Hi,
What is the use of modify statement.? When do I use it?
Regards,
Balaji.
‎2007 Jul 03 12:33 PM
HI,
Please see the example
MODIFY - Changing an Internal Table
Variants:
1. 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 o operations.
Variant 1
MODIFY itab [FROM wa] [INDEX idx] [TRANSPORTING f1 ... fn].
Effect
Changes a single entry in the internal table itab, specifying the key explicitly or implicitly. You can only use this variant with index table (standard or sorted tables).
If you specify "FROM wa", the new values are taken from the work area wa. If you do not specify FROM, the header line of itab is used as the work area.
You can use "INDEX idx" to specify the table index of the line you want to change. This may be omitted within a LOOP at an internal table. In this case, the current table line is changed.
The INDEX specification can come before the FROM addition.
If you specify " TRANSPORTING f1 ... fn", only components f1, f2, ... of the work area are copied into the table. You can also specify components dynamically in the form (name). The actual component name is then taken from the field name at runtime. If name contains an invalid component name, the system triggers 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, you can use this variant for any table.
You can use "FROM wa" and "TRANSPORTING f1 ... fn" in the same way as in variant 1. If you do not use the TRANSPORTING addition, only the non-key fields are transported.
The key of the entry to be changed is taken from the work area. The procedure depends on the table type:
STANDARD TABLE:
The table is searched sequentially by its table key. The first entry found is changed. The runtime required for the search is in linear relation to the number of table entries.
SORTED TABLE:
The table is searched by its table key using a binary search. In tables with a non-unique key ( NON-UNIQUE) only the first entry in the list of duplicates is changed. The runtime required for the search is in logarithmic relation to the number of table entries.
HASHED TABLE:
The entry is found and changed by table key using the internal hash administration. The runtime required is essentially constant, since, unlike in standard or hashed tables, it does not depend on the number of 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 ... fn WHERE cond.
Effect
Changes several entries in the internal table itab. You can use this variant for any table.
You can use " FROM wa" and "TRANSPORTING f1 ... fn" as in variant 1. If the table has the type SORTED TABLE or HASHED TABLE, the TRANSPORTING list may not contain key fields.
In comparisons within the logical expression cond, the first operand must always be a subfield of the line structure 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 or HASHED table, a full table scan is always required.
If you are working with a SORTED TABLE, the partial sequential processing can be optimized so that only the entries that actually satisfy the WHERE condition are processed. This optimization requires 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 the beginning of the table key.
All operands in the WHERE condition that are not components of the internal table itab can be replaced with a functional method call. Functional methods are methods in ABAP Objects that can have any number of IMPORTING parameters and a single RETURNING parameter.
If the line type of the internal table contains object reference variables as components, or the entire line type is a reference variable, you can use the attributes of the object to which the reference refers as values in the WHERE condition (see Attributes of objects as the 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 an explicitly-specified work area, the work area must be compatible with the 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 are constructing it using the "MODIFY ... TRANSPORTING ..." statement, as long as the components selected with TRANSPORTING are not contained in the key of the internal table. This method does not cause significant performance impairment.
Notes
Performance:
You can save unnecessary assignments if you use statements that use a specific work area with internal tables with header lines.
The runtime required to change an entry in a table 100 bytes wide using an index is around 5 msn (standardized microseconds).
If you only want to change individual fields within a complex line structure, it is quicker to use " TRANSPORTING f1 f2 ..." than the simple MODIFY statement. This applies particularly if you can use the TRANSPORTING addition to eliminate tabular components.
Notes
Runtime errors:
ITAB_ILLEGAL_COMPONENT: Illegal dynamic specification for a line component.
ITAB_ILLEGAL_TRANSP_COMP: Illegal key components in the TRANSPORTING list.
Related
APPEND itab, INSERT itab, COLLECT itab
Additional help
Changing Table Lines
Changing Table Lines Using the Index
Regards,
Nandha
Reward if it helps
‎2007 Jul 03 12:30 PM
Hi,
MODIFY:
To insert lines into a database table regardless of whether there is already a line in the table with the same primary key, use the following:
MODIFY <target> <lines>.
If the database table contains no line with the same primary key as the line to be inserted, MODIFY works like INSERT, that is, the line is added.
If the database already contains a line with the same primary key as the line to be inserted, MODIFY works like UPDATE, that is, the line is changed.
For performance reasons, you should use MODIFY only if you cannot distinguish between these two options in your ABAP program.
UPDATE:
The Open SQL statement for changing data in a database table is:
UPDATE <target> <lines>.
It allows you to change one or more lines in the database table <target>. You can only change lines in an ABAP Dictionary view if it only contains fields from one table, and its maintenance status is defined as Read and change. You may specify the database table <target> either statically or dynamically.
Regards,
Vinodh.
‎2007 Jul 03 12:31 PM
Hi Balaji,
Modify statment will work both insert and Update..
Modify ztable from table int_table.
if you are changing non primary key,then it works like update..
if you are creating new record then it works like insert command.
Check this link also
https://forums.sdn.sap.com/click.jspa?searchID=3613725&messageID=3479280
Reward Points if it is helpful
Thanks.
‎2007 Jul 03 12:31 PM
MODIFY for Database Tables
Inserts or changes lines in database tables.
Syntax
MODIFY <dbtab> FROM <wa>.
MODIFY <dbtab> FROM TABLE <itab>.
MODIFY for any Internal Table
Changes the content of lines in internal tables of any type.
Syntax
MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f 2>...]
[ASSIGNING <FS> | REFERENCE INTO <dref>].
MODIFY for Index Tables
Changes the content of lines in index tables.
Syntax
MODIFY <itab> FROM <wa> [INDEX <idx>] [TRANSPORTING <f1> <f 2>...]
[ASSIGNING <FS> | REFERENCE INTO <dref>].
MODIFY for Lists
Changes a list line.
Syntax
MODIFY LINE <n> [INDEX <idx>] [OF CURRENT PAGE|OF PAGE <p>]
|CURRENT LINE
LINE FORMAT <option1> <option2>...
FIELD VALUE <f1> [FROM <g1>] <f2> [FROM <g2>]...
FIELD FORMAT <f1> <options1> <f2> <options2>.
Regards,
Pavan P.
‎2007 Jul 03 12:33 PM
HI,
Please see the example
MODIFY - Changing an Internal Table
Variants:
1. 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 o operations.
Variant 1
MODIFY itab [FROM wa] [INDEX idx] [TRANSPORTING f1 ... fn].
Effect
Changes a single entry in the internal table itab, specifying the key explicitly or implicitly. You can only use this variant with index table (standard or sorted tables).
If you specify "FROM wa", the new values are taken from the work area wa. If you do not specify FROM, the header line of itab is used as the work area.
You can use "INDEX idx" to specify the table index of the line you want to change. This may be omitted within a LOOP at an internal table. In this case, the current table line is changed.
The INDEX specification can come before the FROM addition.
If you specify " TRANSPORTING f1 ... fn", only components f1, f2, ... of the work area are copied into the table. You can also specify components dynamically in the form (name). The actual component name is then taken from the field name at runtime. If name contains an invalid component name, the system triggers 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, you can use this variant for any table.
You can use "FROM wa" and "TRANSPORTING f1 ... fn" in the same way as in variant 1. If you do not use the TRANSPORTING addition, only the non-key fields are transported.
The key of the entry to be changed is taken from the work area. The procedure depends on the table type:
STANDARD TABLE:
The table is searched sequentially by its table key. The first entry found is changed. The runtime required for the search is in linear relation to the number of table entries.
SORTED TABLE:
The table is searched by its table key using a binary search. In tables with a non-unique key ( NON-UNIQUE) only the first entry in the list of duplicates is changed. The runtime required for the search is in logarithmic relation to the number of table entries.
HASHED TABLE:
The entry is found and changed by table key using the internal hash administration. The runtime required is essentially constant, since, unlike in standard or hashed tables, it does not depend on the number of 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 ... fn WHERE cond.
Effect
Changes several entries in the internal table itab. You can use this variant for any table.
You can use " FROM wa" and "TRANSPORTING f1 ... fn" as in variant 1. If the table has the type SORTED TABLE or HASHED TABLE, the TRANSPORTING list may not contain key fields.
In comparisons within the logical expression cond, the first operand must always be a subfield of the line structure 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 or HASHED table, a full table scan is always required.
If you are working with a SORTED TABLE, the partial sequential processing can be optimized so that only the entries that actually satisfy the WHERE condition are processed. This optimization requires 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 the beginning of the table key.
All operands in the WHERE condition that are not components of the internal table itab can be replaced with a functional method call. Functional methods are methods in ABAP Objects that can have any number of IMPORTING parameters and a single RETURNING parameter.
If the line type of the internal table contains object reference variables as components, or the entire line type is a reference variable, you can use the attributes of the object to which the reference refers as values in the WHERE condition (see Attributes of objects as the 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 an explicitly-specified work area, the work area must be compatible with the 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 are constructing it using the "MODIFY ... TRANSPORTING ..." statement, as long as the components selected with TRANSPORTING are not contained in the key of the internal table. This method does not cause significant performance impairment.
Notes
Performance:
You can save unnecessary assignments if you use statements that use a specific work area with internal tables with header lines.
The runtime required to change an entry in a table 100 bytes wide using an index is around 5 msn (standardized microseconds).
If you only want to change individual fields within a complex line structure, it is quicker to use " TRANSPORTING f1 f2 ..." than the simple MODIFY statement. This applies particularly if you can use the TRANSPORTING addition to eliminate tabular components.
Notes
Runtime errors:
ITAB_ILLEGAL_COMPONENT: Illegal dynamic specification for a line component.
ITAB_ILLEGAL_TRANSP_COMP: Illegal key components in the TRANSPORTING list.
Related
APPEND itab, INSERT itab, COLLECT itab
Additional help
Changing Table Lines
Changing Table Lines Using the Index
Regards,
Nandha
Reward if it helps
‎2007 Jul 03 12:34 PM
Hello,
<b>Database table:</b>Here the modify statement will modify a particular record if it is existing in the database, otherwise it will create a new entry in the database.
<b>Internal table :</b> Here the modify statement is used to modify an existing record in the internal table. This will not create new entries.
Reward if usefull.
Rakesh
‎2007 Jul 03 12:36 PM
hi,
it will work as both insert and update depending on condition. when a record is found then modify works like update and saves that changes to internal table with new record.
if it didnt found any record then it appends a record at the end of internal table with given data.
if useful reward some points.
with reagrds,
suresh.a