Application Development and Automation 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: 
Read only

Problem with MODIFY command

Former Member
0 Likes
4,207

Hi,

I want to modify a Z table from the internal table.

Do i need to use modify command to update the Z table?

i need to update the Z table from the internal table only when a new record or change in the existing record in the Z table exist in internal table.

If the same record exist in the Z table with no change, then modify should fail.

I am using modify statement for this, but every time it is updating the Z table even if there is no change in the records from internal table.

My code---

modify zempfinal from wa_empfinal.

if sy-subrc = 0.

--

--

---

else.

--

--

endif.

Can anyone suggest the solution for this?

thanks,

Rakesh

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,286

Hi,

To change a single line, use the following statement:

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f2> ...].

The work area <wa>, which must be compatible with the line type of the internal table, plays a

double role in this statement. Not only it is used to find the line that you want to change, but it

also contains the new contents. The system searches the internal table for the line whose table

key corresponds to the key fields in <wa>.

To change one or more lines using a condition, use the following statement:

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f2> ... WHERE <cond>.

This processes all of the lines that meet the logical condition [Page 225] <cond>. The logical

condition can consist of more than one comparison. In each comparison, the first operand must

be a component of the line structure. If the table lines are not structured, the first operand can

also be the expression TABLE LINE. The comparison then applies to the entire line.

Ex:1.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 2. LINE-COL2 = 100.

MODIFY TABLE ITAB FROM LINE.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

2 100

3 9

4 16

The program fills a hashed table with a list of square numbers. The MODIFY

statement changes the line of the table in which the key field COL1 has the value 2.

EX:2.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL2 = 100.

MODIFY ITAB FROM LINE TRANSPORTING COL2

WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

2 100

3 100

4 16

The program fills a hashed table with a list of square numbers. The MODIFY

statement changes the lines of the table where the content of field COL2 is greater

than 1 and the content of field COL1 is less than 4.

Regards,

Bhaskar

6 REPLIES 6
Read only

Former Member
0 Likes
1,286

Hi,

Probably you can do a select before the modify statement..

SELECT SINGLE * FROM ZTABLE WHERE ...." give the key fields..

IF ZTABLE = WA_EMPFINAL AND SY-SUBRC = 0.

****Record exists

ELSE.

*****Modify / Insert the record.

MODIFY ZEMPFINAL FROM WA_EMPFINAL.

ENDIF.

Thanks,

Naren

Read only

Former Member
0 Likes
1,287

Hi,

To change a single line, use the following statement:

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f2> ...].

The work area <wa>, which must be compatible with the line type of the internal table, plays a

double role in this statement. Not only it is used to find the line that you want to change, but it

also contains the new contents. The system searches the internal table for the line whose table

key corresponds to the key fields in <wa>.

To change one or more lines using a condition, use the following statement:

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f2> ... WHERE <cond>.

This processes all of the lines that meet the logical condition [Page 225] <cond>. The logical

condition can consist of more than one comparison. In each comparison, the first operand must

be a component of the line structure. If the table lines are not structured, the first operand can

also be the expression TABLE LINE. The comparison then applies to the entire line.

Ex:1.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 2. LINE-COL2 = 100.

MODIFY TABLE ITAB FROM LINE.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

2 100

3 9

4 16

The program fills a hashed table with a list of square numbers. The MODIFY

statement changes the line of the table in which the key field COL1 has the value 2.

EX:2.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL2 = 100.

MODIFY ITAB FROM LINE TRANSPORTING COL2

WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

2 100

3 100

4 16

The program fills a hashed table with a list of square numbers. The MODIFY

statement changes the lines of the table where the content of field COL2 is greater

than 1 and the content of field COL1 is less than 4.

Regards,

Bhaskar

Read only

Former Member
1,286

Hi Rakesh,

Before modifying the ztable, compare and delete the records in the source internal table, if it's not modified in the ztable.

Pls find the logic below. Hope this helps...

1. Read the data from zempfinal into an internal table itab_zempfinal.

2. sort the data in the internal table.

3. for all the entries in source internal table (assuing, wa_empfinal your internal table), compare the record with the correspnding entry in the ztable(zempfinal) using the internal table itab_zempfinal.

4. if it's same, delete the records from the source internal table(wa_empfinal).

5. now use this filtered internal table for modifying the records in the ztable(zempfinal).

Herewith, pls find the code for the same.

1. select * from zempfinal into table itab_zempfinal.

2. sort itab_zempfinal.

3,4. loop at ws_empfinal.

clear : zempfinal.

read table zemfinal with key <primary keys> = ws_empfinal-<primary keys> binary search.

if zempfinal = ws_empfinal.

delete ws_empfinal.

endif.

endloop.

5. modify zempfinal from ws_empfinal.

Read only

Clemenss
Active Contributor
0 Likes
1,286

Rakesh,

how do you know and why do you care the Z-table is updated if there is no change in the records?

Curious,

Clemens

Read only

Former Member
0 Likes
1,286

hi,

use <b>UPDATE</b> instead of <b>MODIFY</b> .

<b> MODIFY</b> will work in 2 ways<b> if the record is not there it wil insert n if there is any change in the record it wil update.</b>

<b>UPDATE</b> will update only when there is some change in record otherwise it wil leave unchanged syntax is same for both the case.

reward if helpful

ravi

Read only

Former Member
0 Likes
1,286

Hi,

Overwriting Individual Lines From a Work Area

To overwrite a single line in a database table with the contents of a work area, use the following:

UPDATE <target> FROM <wa> .

The contents of the work area <wa> overwrite the line in the database table <dbtab> that has the same primary key. The work area <wa> must be a data object with at least the same length and alignment as the line structure of the database table. The data is placed in the database table according to the line structure of the table, and regardless of the structure of the work area. It is a good idea to define the work area with reference to the structure of the database table.

If the database table contains a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is set to 4.

A shortened form of the above statement is:

UPDATE <dbtab>.

In this case, the contents of the table work area <dbtab> are used to update the database table with the same name. You must declare this table work area using the TABLES statement. In this case, it is not possible to specify the name of the database table dynamically. Table work areas with the same name as the database table (necessary before Release 4.0) should no longer be used for the sake of clarity.

Overwriting Several Lines Using an Internal Table

To overwrite several lines in a database table with the contents of an internal table, use the following:

UPDATE <target> FROM TABLE <itab> .

The contents of the internal table <itab> overwrite the lines in the database table <dbtab> that have the same primary keys. The same rules apply to the line type of <itab> as to the work area <wa> described above.

If the system cannot change a line because no line with the specified key exists, it does not terminate the entire operation, but continues processing the next line of the internal table.

If all lines from the internal table have been processed, SY-SUBRC is set to 0. Otherwise, it is set to 4. If not all lines are used, you can calculate the number of unused lines by subtracting the number of processed lines in SY-DBCNT from the total number of lines in the internal table. If the internal table is empty, SY-SUBRC and SY-DBCNT are set to 0.

Whenever you want to overwrite more than one line in a database table, it is more efficient to work with an internal table than to change the lines one by one.

Examples

UPDATE SFLIGHT SET PLANETYPE = 'A310'

PRICE = PRICE - '100.00'

WHERE CARRID = 'LH' AND CONNID = '0402'.

This example overwrites the contents of the PLANETYPE column with A310 and decreases the value of the PRICE column by 100 for each entry in SFLIGHT where CARRID contains ‘LH’ and CONNID contains ‘402’.

TABLES SPFLI.

DATA WA TYPE SPFLI.

MOVE 'AA' TO WA-CARRID.

MOVE '0064' TO WA-CONNID.

MOVE 'WASHINGTON' TO WA-CITYFROM.

...

UPDATE SPFLI FROM WA.

MOVE 'LH' TO SPFLI-CARRID.

MOVE '0017' TO SPFLI-CONNID.

MOVE 'BERLIN' TO SPFLI-CITYFROM.

...

UPDATE SPFLI.

CARRID and CONNID are the primary key fields of table SPFLI. All fields of those lines where the primary key fields are "AA" and "0064", or "LH" and "0017", are replaced by the values in the corresponding fields of the work area WA or the table work area SPFLI.

DATA: ITAB TYPE HASHED TABLE OF SPFLI

WITH UNIQUE KEY CARRID CONNID,

WA LIKE LINE OF ITAB.

WA-CARRID = 'UA'. WA-CONNID = '0011'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

WA-CARRID = 'LH'. WA-CONNID = '1245'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

WA-CARRID = 'AA'. WA-CONNID = '4574'. WA-CITYFROM = ...

INSERT WA INTO TABLE ITAB.

...

UPDATE SPFLI FROM TABLE ITAB.

This example fills a hashed table ITAB and then overwrites the lines in SPFLI that have the same primary key (CARRID and CONNID) as a line in the internal table.

Don't forget to reward if useful..