Iterating On a Table Using Field Symbols

Say you have a Table called “table_name”, of line type “table_row”, and you wish to iterate on it and perhaps change the rows while you’re iterating. You can do it in ABAP using field symbols.
First, you need to define the field symbol. In our example it’s called “symbol_name”


FIELD-SYMBOLS <symbol_name> TYPE table_row.


Now, you can iterate over the table, assigning each row to the field symbol


LOOP AT table_name ASSIGNING <symbol_name>.
...
ENDLOOP



On each iteration, you can manipulate the table using the field symbol. Any change done to the field symbol is also done on the table. It’s like using a pointer in C or a reference variable in C++.

Important note: When using field symbols on sorted or hashed table, then you may not change the key fields of these tables. Doing so results in a runtime error. The logic is simple: changing the key will change the sorting order or hashing order of the table, and since you’re in mid-iteration, it may cause you to re-iterate on these rows. You Java developers out there may be familiar with this concept, as it also exists in Java.

You can read more about field symbols at the following SAP help link.

Read more...

The difference between INSERT, UPDATE, and MODIFY

Here's a very common syntactic question: What's the difference between INSERT, UPDATE, and MODIFY?
The answer is plain and simple:

INSERT - inserts a new record. INSERT expects that a record with the required key does NOT exist in the table. Therefore, if a record with the same key already exists, a runtime error occurs. For more details on INSERT, visit here.

UPDATE - update an existing record. UPDATE expects that a record with required key exists in the table. If a record with the required key does not exist, it issue an error (sy-subrc is 4). For more details on UPDATE, visit here.

MODIFY - acts like a combination of INSERT and UPDATE. If a record with the specified key does not exist, it adds it to the table. If a record with the specified key does exist, it modifies it. So MODIFY actually acts like "Insert or change record". To read more about MODIFY, visit here.

To summarize:
INSERT - adds a new record to the table. If the row (key) exists, issues an error.
UPDATE - updates an existing record to the table. If the row (key) does not exist, issues an error.
MODIFY - If the key exists, modifies the record. If the key does not exist, adds the record to the table.

Read more...

About This Blog

This blog contains tips, tricks, tutorial, and some of my personal experiences with the SAP ABAP language.

FEEDJIT Live Traffic Feed

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP