Application Development 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: 

Is this MODIFY statement ABAP coding okay?

Former Member
0 Kudos

Hi,

I had this code:

if del_entries ne false. 
      delete from zsrcmtrx where lockkey eq lock_key. 
    else. 
      select * from zsrcmtrx into table lit_zsrcmtrx 
            where lockkey eq lock_key. 
      loop at lit_zsrcmtrx. 
        lit_zsrcmtrx-locked = false. 
        update zsrcmtrx from table lit_zsrcmtrx. 
      endloop. 
    endif.

I've changed it to:

IF del_entries NE false. 
      DELETE FROM zsrcmtrx WHERE lockkey EQ lock_key. 
    ELSE. 
      SELECT * FROM zsrcmtrx INTO TABLE lit_zsrcmtrx 
            WHERE lockkey EQ lock_key. 

      IF sy-subrc = 0. 
        LOOP AT lit_zsrcmtrx. 
          lit_zsrcmtrx-locked = false. 
          MODIFY lit_zsrcmtrx. 
        ENDLOOP. 
        UPDATE zsrcmtrx FROM TABLE lit_zsrcmtrx. 
      ENDIF. 
    ENDIF.

Is this okay? in your opinion? I'm just wondering if the Modify statement needs to have any additions to it...

Many Thanks for any help.

1 ACCEPTED SOLUTION

athavanraja
Active Contributor
0 Kudos

<i>LOOP AT lit_zsrcmtrx.

lit_zsrcmtrx-locked = false.

MODIFY lit_zsrcmtrx.

ENDLOOP.</i>

since you are modifying within the loop you dont need any addition to the modify statement.

if it were from outside the loop (for some reason) then you need to pass either the index of the record to be updated or the keys to the record.

Regards

Raja

6 REPLIES 6

athavanraja
Active Contributor
0 Kudos

<i>LOOP AT lit_zsrcmtrx.

lit_zsrcmtrx-locked = false.

MODIFY lit_zsrcmtrx.

ENDLOOP.</i>

since you are modifying within the loop you dont need any addition to the modify statement.

if it were from outside the loop (for some reason) then you need to pass either the index of the record to be updated or the keys to the record.

Regards

Raja

andreas_mann3
Active Contributor
0 Kudos

Hi

i think this statement seems to be false:

update zsrcmtrx from table lit_zsrcmtrx.

look F1 to update :

you can say:

loop...
zsrcmtrx = lit_zsrcmtrx. 
update /modify zsrcmtrx.
endloop.

Andreas

0 Kudos

ThanksGuys!

Andreas youi are almost always correct but I think on this occasion you might be wrong I think the statement:

<i>update zsrcmtrx from table lit_zsrcmtrx.</i>

Can update multiple lines...

From F1:

"Variant 3 UPDATE dbtab FROM TABLE itab. or

UPDATE (dbtabname) FROM TABLE itab.

Effect:

Mass update of several lines in a database table. Here, the primary key for identifying the lines to be updated and the values to be changed are taken from the lines of the internal table itab. The lines of the internal table must satisfy the same conditions as the work area wa in addition 1 to variant."

If you agree then I'll give you some points for helping out

0 Kudos

update zsrcmtrx from table lit_zsrcmtrx.

i generally use modify here as well.

modify zsrcmtrx from table lit_zsrcmtrx.

Regards

Raja

Former Member
0 Kudos

Hi Steve,

Your coding is correct. Because the modify statement is within the loop and your internal table is declared with header line (which you shouldn't do anymore as it is not allowed in the OO context) this is ok.

Kind regards,

John.

Former Member
0 Kudos

Steve, hi

I know the question is already answered, nevertheless, thought I'd add a few extra comments on some of the points made in the thread. Here goes:-

Your second bit of coding is better from a performance point of view at the database level because the array update can maximise the use of the data packet size when transferring the SQL to the database from the server. The first bit of coding will very likely result in multiple transfers over the network from server to database, one for each record in the internal table lit_zsrcmtrx.

Another alternative may be to use the following;

if del_entries ne false.

delete from zsrcmtrx where lockkey eq lock_key.

else.

update zsrcmtrx

set locked = false

where lockkey eq lock_key.

endif.

Only one database SQL issued instead of at least one select and one update. No data transfer from the database to the server in the select statement. No internal table manipulation (you won't even need the internal table at all). All the relevant database records will be updated correctly according to the where condition in the update statement.

If you aren't already, then I would suggest that you might want to consider using the SAP standard update technique, i.e. lock the records first using lock object enqueue function modules then create an update function module with the actual database update syntax in it and call that from within the main program with the addition IN UPDATE TASK added to the CALL FUNCTION statement (after the function module name). Provides better recovery facilities as updates are first logged in the update tables (VB*) before the actual database updates are triggered with a COMMIT WORK. (this is a short description, more detail is available in the SAP online help)

I would recommend avoiding using the MODIFY statement on a database table, it will perform an INSERT if it doesn't find the record(s), an UPDATE if it does, i.e. it actually constitutes two separate effects dependent on the circumstances. I believe SAP are trying to get away from syntax elements with more than one possible result as it can be unclear when skimming the code what the result might be.

MODIFY used with an internal table is OK but again you may be better served using a field symbol, e.g.

FIELD-SYMBOLS: <fs_name> LIKE LINE OF lit_zsrcmtrx.

LOOP AT lit_zsrcmtrx ASSIGNING <fs_name>.

<fs_name>-locked = false.

ENDLOOP.

Less data copying in the server work process memory because you access the internal table lines directly, through the field symbol. Usually a lot quicker especially if you are modifying lines, you just change them directly using the field symbol.

Hope at least some of this is helpful.

Kind regards

Mark