From 6818f152ffde5253ab795411440f26986100409c Mon Sep 17 00:00:00 2001 From: danrega <16720986+danrega@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:10:12 +0100 Subject: [PATCH] Update --- 01_Internal_Tables.md | 394 ++++++++++++++++++++++++++++++++++++++ 06_Dynamic_Programming.md | 11 ++ README.md | 4 +- 3 files changed, 408 insertions(+), 1 deletion(-) diff --git a/01_Internal_Tables.md b/01_Internal_Tables.md index 1135931..d7f4286 100644 --- a/01_Internal_Tables.md +++ b/01_Internal_Tables.md @@ -29,6 +29,7 @@ - [Examples of Addressing Individual Components of Read Lines](#examples-of-addressing-individual-components-of-read-lines) - [Excursions with READ TABLE Statements](#excursions-with-read-table-statements) - [System Field Setting in READ TABLE Statements](#system-field-setting-in-read-table-statements) + - [Specifying a WHERE Condition in READ TABLE Statements](#specifying-a-where-condition-in-read-table-statements) - [COMPARING and TRANSPORTING Additions: Comparing Fields and Specifying Fields for Transport](#comparing-and-transporting-additions-comparing-fields-and-specifying-fields-for-transport) - [CASTING and ELSE UNASSIGN Additions when Specifying Field Symbols as Target Areas](#casting-and-else-unassign-additions-when-specifying-field-symbols-as-target-areas) - [BINARY SEARCH Addition: Optimized Read Access When Specifying Free Keys](#binary-search-addition-optimized-read-access-when-specifying-free-keys) @@ -44,6 +45,7 @@ - [Defining the Step Size and the Direction of Loop Passes](#defining-the-step-size-and-the-direction-of-loop-passes) - [Iteration Expressions](#iteration-expressions) - [Interrupting and Exiting Loops](#interrupting-and-exiting-loops) + - [Inserting and Deleting Lines in Internal Tables in Loops](#inserting-and-deleting-lines-in-internal-tables-in-loops) - [Operations with Internal Tables Using ABAP SQL SELECT Statements](#operations-with-internal-tables-using-abap-sql-select-statements) - [Internal Tables as Target Data Objects in SELECT Queries](#internal-tables-as-target-data-objects-in-select-queries) - [SELECT Queries with Internal Tables as Data Sources](#select-queries-with-internal-tables-as-data-sources) @@ -1891,6 +1893,148 @@ ASSERT sy-tabix = 0.
+#### Specifying a WHERE Condition in READ TABLE Statements + +- The syntax `READ TABLE ... WHERE ...` searches for the first line that matches a `WHERE` condition. +- The value of `sy-tabix` is set when a line is found, while `sy-subrc` are set to either 0 or 4. +- The `WHERE` condition can use comparison and predicate expressions (see the code snippet comments below for examples). +- Syntax options: + - The read result must be specified before the `WHERE` condition. + - The `TRANSPORTING NO FIELDS` and `USING KEY` additions are possible. + - Dynamic `WHERE` conditions are possible. +- If a `READ TABLE` statement can be expressed using `WITH KEY`, this approach - instead of using a `WHERE` condition - is recommended as it is more performant. A syntax warning occurs but can be suppressed with the pragma `##read_where_ok`. +- For optimized searches with a `WHERE` condition: + - No optimized search in standard tables without a secondary table key. + - For sorted and hashed keys (sorted/hashed tables, secondary table keys), the search is optimized as described [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenitab_where_optimization.htm), if specifications are transferable to a key access. + +```abap +"Creating and populating demo internal tables +TYPES: BEGIN OF s_demo, + comp1 TYPE i, + comp2 TYPE c LENGTH 5, + comp3 TYPE i, + comp4 TYPE c LENGTH 5, + END OF s_demo, + t_type_so TYPE SORTED TABLE OF s_demo WITH UNIQUE KEY comp1 WITH NON-UNIQUE SORTED KEY sk COMPONENTS comp3. + +DATA(itab) = VALUE t_type_so( ( comp1 = 1 comp2 = 'lorem' comp3 = 30 comp4 = 'ipsum' ) + ( comp1 = 2 comp2 = 'dolor' comp3 = 20 comp4 = 'sit' ) + ( comp1 = 3 comp2 = 'amet' comp3 = 40 comp4 = 'hello' ) + ( comp1 = 4 comp2 = 'world' comp3 = 50 comp4 = 'ABAP' ) + ( comp1 = 5 comp2 = 'test' comp3 = 10 comp4 = '' ) ). + +DATA wa TYPE s_demo. + +"--------- WHERE condition with comparison expressions --------- + +"Examples: =/EQ, <>/NE, >/GT, =,GE, <=/LE, +" CO, CN, CA, NA, CS, NS, CP, NP, +" [NOT] BETWEEN ... AND +" [NOT] IN ranges_tables + +READ TABLE itab INTO wa WHERE comp1 > 3. +ASSERT sy-tabix = 4. + +"'or' also available in other lines in this component, but the first found line +"is returned (lines 1, 2, 4) +READ TABLE itab INTO wa WHERE comp2 CS 'or'. +ASSERT sy-tabix = 1. + +"'d' occurs in lines 2, 4 +READ TABLE itab INTO wa WHERE comp2 CS 'd'. +ASSERT sy-tabix = 2. + +READ TABLE itab INTO wa WHERE comp2 CS 'd'. +ASSERT sy-tabix = 2. + +"--------- WHERE condition with predicate expressions --------- + +"Examples: IS [NOT] INITIAL +" IS [NOT] BOUND +" IS [NOT] INSTANCE OF + +READ TABLE itab INTO wa WHERE comp1 > 4 AND comp4 IS INITIAL. +ASSERT sy-tabix = 5. + +"--------- WITH KEY instead of WHERE condition --------- + +"Syntax warning in READ TABLE ... WHERE ... statements +READ TABLE itab INTO wa WHERE comp4 IS INITIAL. +ASSERT sy-tabix = 5. + +"For a better performance, the previous statement should be +"replaced by a READ TABLE ... WITH KEY ... statement. +READ TABLE itab INTO wa WITH KEY comp4 = ''. +ASSERT sy-tabix = 5. + +"You can also suppress the syntax warning by a pragma. +READ TABLE itab INTO wa WHERE comp4 IS INITIAL ##read_where_ok. +ASSERT sy-tabix = 5. + +"------------------- Further additions ------------------- + +"TRANSPORTING NO FIELDS addition is possible +READ TABLE itab TRANSPORTING NO FIELDS WHERE comp2 CS 'd'. +ASSERT sy-tabix = 2. + +"USING KEY addition +READ TABLE itab USING KEY primary_key INTO wa WHERE comp2 CS 't'. +ASSERT sy-tabix = 3. + +READ TABLE itab USING KEY sk INTO wa WHERE comp3 > 40. +ASSERT sy-tabix = 5. + +"------------------- Excursions ------------------- + +"Note the comparison rules for character-like data types +READ TABLE itab INTO wa WHERE comp2 = 'lorem' ##read_where_ok. +ASSERT sy-tabix = 1. + +"In the following case, the length of the comp2 value increased to match the +"length of the specified text field, i.e. the surplus characters from the right +"side text field are not truncated. As a consequence, the line is not found. + +DATA(some_text) = 'loremXYZ'. + +READ TABLE itab INTO wa WHERE comp2 = some_text ##read_where_ok. +ASSERT sy-tabix = 0 AND sy-subrc <> 0. + +"When using READ TABLE ... WITH KEY ... the behavior is different. In that +"case, the surplus characters are truncated because of a conversion. Therefore, +"the following statement finds a line. +READ TABLE itab INTO wa WITH KEY comp2 = some_text. +ASSERT sy-tabix = 1 AND sy-subrc = 0. + +"Note: The read target can only be placed before WHERE conditions. +"The following statements are not possible. +"READ TABLE itab WHERE comp2 CS 'd' INTO wa. +"READ TABLE itab WHERE comp2 CS 'd' TRANSPORTING NO FIELDS. + +"READ TABLE ... WHERE ... statements can replace LOOP AT ... WHERE ... +"statements including EXIT. +LOOP AT itab INTO wa WHERE comp2 CS 'd'. + ASSERT sy-tabix = 2. + EXIT. +ENDLOOP. + +"------------------- Dynamic WHERE condition ------------------- + +"Character-like data objects or standard tables with character-like line type +"can be specified + +DATA(dyn_where_cond_str) = `comp2 CS 'd'`. + +READ TABLE itab INTO wa WHERE (dyn_where_cond_str). +ASSERT sy-tabix = 2. + +DATA(dyn_where_cond_tab) = VALUE string_table( ( `comp2` ) ( `CS` ) ( `'d'` ) ). +READ TABLE itab INTO wa WHERE (dyn_where_cond_tab). +ASSERT sy-tabix = 2. +``` + + + + #### COMPARING and TRANSPORTING Additions: Comparing Fields and Specifying Fields for Transport `... TRANSPORTING NO FIELDS`: It is only checked whether the line exists. No target area is specified. As mentioned above, the system fields are filled. @@ -3721,6 +3865,256 @@ ASSERT tabix = 5. +### Inserting and Deleting Lines in Internal Tables in Loops + +- In a loop, you can change the values of the current table line's components. You can also delete or insert entire lines. +- The position of these new or deleted lines is determined by the table index or a sorted key. For hashed tables or hash keys, the position is determined by the insertion order (though this can be changed with the `SORT` statement). +- After the currently processed table line, ... + - inserting a line: The new line is processed in the next loop pass (watch out for a non-terminating loop). + - deleting a line: The deleted line is not processed in the next loop pass. +- Before the currently processed table line, ... + - inserting a line: The internal loop counter (`sy-tabix`) increases accordingly (in the case of index tables, sorted keys). + - deleting a line: The internal loop counter (`sy-tabix`) decreases accordingly. +- You cannot replace or clear the entire table body in a loop. If detected statically, a syntax error is displayed. + +The following examples explore the insertion and deletion of lines when looping across internal tables: + +```abap +"Creating and populating a demo standard internal table to +"work with in the example loops +TYPES: BEGIN OF s_loop_mod, + text TYPE string, + num TYPE i, + END OF s_loop_mod, + t_loop_mod TYPE TABLE OF s_loop_mod WITH EMPTY KEY. + +"Inserting 10 entries into the demo table +DATA(itab_original) = VALUE t_loop_mod( FOR x = 1 WHILE x <= 10 ( text = x ) ). +DATA(itab) = itab_original. + +"---------- Inserting a line after the current line ---------- + +"The example inserts a line after the currently processed line +"using an INSERT statement and specifying the index value +"(sy-tabix value + 1). The 'num' component is assigned the +"current sy-tabix value. +"Note: In all statements, the sy-tabix value is stored in a +"variable right after the LOOP statement. Assume that multiple +"statements are included before the statement that actually uses +"the current sy-tabix value. Other statements that potentially +"change the sy-tabix value might interfere. +"An EXIT statement takes care of exiting the loop. In the example, +"all values of the 'num' component in the original table lines +"(except the first line) are initial as the loop is exited. +LOOP AT itab ASSIGNING FIELD-SYMBOL(