This commit is contained in:
danrega
2024-01-15 12:09:31 +01:00
parent a31d0cf48f
commit c11100bcca
5 changed files with 171 additions and 42 deletions

View File

@@ -1504,6 +1504,7 @@ DELETE dbtab FROM TABLE @( VALUE #( ( comp1 = ... )
INTO ...
```
- [This topic](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_sql.htm) serves as the entry point for topics about ABAP SQL in the ABAP Keyword Documentation. For the full details, check the subtopics there, especially topics not covered in this cheat sheet.
- There are [RAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenarap_glosry.htm)-specific variants of ABAP SQL statements that use the `MAPPING FROM ENTITY` addition. Find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapmapping_from_entity.htm).
## Executable Example
[zcl_demo_abap_sql](./src/zcl_demo_abap_sql.clas.abap)

View File

@@ -35,7 +35,7 @@
`VALUE` operator:
``` abap
... VALUE string( ... ) ...
... VALUE some_type( ... ) ...
... VALUE #( ... ) ...
```
@@ -298,7 +298,7 @@ DATA(itab7) = VALUE tabtype( b = 'aaa' ( a = 1 c = `xxx` )
*4 bbb zzz
"This option can be handy in various contexts, for example, in a
"range table.
"ranges table.
TYPES int_tab_type TYPE TABLE OF i WITH EMPTY KEY.
"Populating an integer table with values from 1 to 20 (see iteration
"expressions with FOR furhter down)
@@ -406,7 +406,7 @@ options of the constructor operators in the ABAP Keyword Documentation.
type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentable_type_glosry.htm "Glossary Entry")
or [structured
type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstructured_type_glosry.htm "Glossary Entry")).
- The components or columns of the target data object are filled using
- The components or columns of the target data object are populated using
assignments of the parameters specified within the parentheses.
- The assignments are made using identical names or based on [mapping
relationships](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencorresponding_constr_mapping.htm)
@@ -418,51 +418,153 @@ options of the constructor operators in the ABAP Keyword Documentation.
field is of type `string`).
The following table includes a selection of various possible additions to
this constructor operator. There are more variants available like the
addition `EXACT`, using a lookup table, the option of discarding
duplicates or
[RAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrap_glosry.htm "Glossary Entry")-specific
variants that are not part of this cheat sheet. Find the details in
[this
topic](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconstructor_expr_corresponding.htm).
this operator. There are more variants available (also
[RAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrap_glosry.htm "Glossary Entry")-specific ones)
that are not covered. Find more information in [this
topic](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconstructor_expr_corresponding.htm) of the ABAP Keyword Documentation.
| Addition | Details |
|---|---|
| `BASE` | Keeps original values. Unlike, for example, the operator `VALUE`, a pair of parentheses must be set around `BASE`. |
| `MAPPING` | Enables the mapping of component names, i. e. a component of a source structure or source table can be assigned to a differently named component of a target structure or target table (e. g. `MAPPING c1 = c2`). |
| `EXCEPT` | You can specify components that should not be assigned content in the target data object. They remain initial. In doing so, you exclude identically named components in the source and target object that are not compatible or convertible from the assignment to avoid syntax errors or runtime errors. |
| `DISCARDING DUPLICATES` | Relevant for tabular components. Handles duplicate lines and prevents exceptions when dealing with internal tables that have a unique primary or secondary table key. |
| `DEEP` | Relevant for deep tabular components. They are resolved at every hierarchy level and identically named components are assigned line by line. |
| `[DEEP] APPENDING` | Relevant for (deep) tabular components. It ensures that the nested target tables are not deleted. The effect without `DEEP` is that lines of the nested source table are added using `CORRESPONDING` without addition. The effect with `DEEP` is that lines of the nested source table are added using `CORRESPONDING` with the addition `DEEP`. |
See the executable example for demonstrating the effect of the variants:
Examples:
``` abap
"Assignment of a structure/internal table to another one having a different type
struc2 = CORRESPONDING #( struc1 ).
"-------------- Patterns -------------
tab2 = CORRESPONDING #( tab1 ).
"The following examples demonstrate simple assignments
"with the CORRESPONDING operator using these syntax patterns.
"Note:
"- The examples show only a selection of pssible additions.
"- There are various combinations of additions possible.
"- The effect of the statements is shown in lines commented out.
"BASE keeps original content, does not initialize the target
struc2 = CORRESPONDING #( BASE ( struc2 ) struc1 ).
"... CORRESPONDING #( z ) ...
"... CORRESPONDING #( BASE ( x ) z ) ...
"... CORRESPONDING #( z MAPPING a = b ) ...
"... CORRESPONDING #( z EXCEPT b ) ...
"... CORRESPONDING #( it DISCARDING DUPLICATES ) ...
tab2 = CORRESPONDING #( BASE ( tab2 ) tab1 ).
"-------------- Structures -------------
"MAPPING/EXACT are used for mapping/excluding components in the assignment
struc2 = CORRESPONDING #( struc1 MAPPING comp1 = comp2 ).
"Data objects to work with in the examples
"Two different structures; one component differs.
DATA: BEGIN OF s1,
a TYPE i,
b TYPE c LENGTH 3,
c TYPE c LENGTH 5,
END OF s1.
tab2 = CORRESPONDING #( tab1 EXCEPT comp1 ).
DATA: BEGIN OF s2,
a TYPE i,
b TYPE c LENGTH 3,
d TYPE string,
END OF s2.
"Complex assignments with deep components using further additions
st_deep2 = CORRESPONDING #( DEEP st_deep1 ).
"Populating structures
s1 = VALUE #( a = 1 b = 'aaa' c = 'bbbbb' ).
s2 = VALUE #( a = 2 b = 'ccc' d = `dddd` ).
st_deep2 = CORRESPONDING #( DEEP BASE ( st_deep2 ) st_deep1 ).
"Non-identical components in the target are initialized
s2 = CORRESPONDING #( s1 ).
st_deep2 = CORRESPONDING #( APPENDING ( st_deep2 ) st_deep1 ).
*A B D
*1 aaa
st_deep2 = CORRESPONDING #( DEEP APPENDING ( st_deep2 ) st_deep1 ).
"Populating structure for the BASE example
s2 = VALUE #( a = 3 b = 'eee' d = `ffff` ).
"BASE addition: Retaining original content in target, no initialization
s2 = CORRESPONDING #( BASE ( s2 ) s1 ).
*A B D
*1 aaa ffff
"MAPPING addition: Mapping of component names
"For the assignment to work, note data convertibility.
s2 = CORRESPONDING #( s1 MAPPING d = c ).
*A B D
*1 aaa bbbbb
"EXCEPT addition: Excluding components
s2 = CORRESPONDING #( s1 EXCEPT b ).
*A B D
*1
"As noted, there are various combinations possible for the additions.
"The following example shows MAPPING and EXCEPT.
s2 = CORRESPONDING #( s1 MAPPING d = c EXCEPT b ).
*A B D
*1 bbbbb
"Specifying an asterisk (*) after EXCEPT in combination with specifying mappings
"means that all non-specified components after MAPPING remain initial in the target
s2 = CORRESPONDING #( s1 MAPPING d = c EXCEPT * ).
*A B D
*0 bbbbb
"-------------- Internal tables -------------
"Internal tables to work with in the examples
DATA it1 LIKE TABLE OF s1 WITH EMPTY KEY.
DATA it2 LIKE SORTED TABLE OF s2 WITH UNIQUE KEY a.
DATA it3 LIKE TABLE OF s1 WITH EMPTY KEY.
"Populating internal tables
it1 = VALUE #( ( a = 1 b = 'aaa' c = 'bbbbb' )
( a = 2 b = 'ccc' c = 'ddddd' ) ).
it3 = VALUE #( ( a = 3 b = 'eee' c = 'fffff' ) ).
it2 = VALUE #( ( a = 7 b = 'eee' d = 'fffff' ) ).
it2 = CORRESPONDING #( it1 ).
*A B D
*1 aaa
*2 ccc
it2 = CORRESPONDING #( BASE ( it2 ) it3 ).
*A B D
*1 aaa
*2 ccc
*3 eee
it2 = CORRESPONDING #( it1 MAPPING d = c ).
*A B D
*1 aaa bbbbb
*2 ccc ddddd
it2 = CORRESPONDING #( it1 EXCEPT b ).
*A B D
*1
*2
"DISCARDING DUPLICATES: Handling duplicate lines and preventing exceptions
it1 = VALUE #( ( a = 4 b = 'aaa' c = 'bbbbb' )
( a = 4 b = 'ccc' c = 'ddddd' )
( a = 5 b = 'eee' c = 'fffff' ) ).
"Without the addition, the runtime error ITAB_DUPLICATE_KEY is raised.
it2 = CORRESPONDING #( it1 DISCARDING DUPLICATES ).
*A B D
*4 aaa
*5 eee
```
> **✔️ Hint**<br>
> `CORRESPONDING` operator versus
[`MOVE-CORRESPONDING`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapmove-corresponding.htm):
[`MOVE-CORRESPONDING`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapmove-corresponding.htm) in the context of structures:
Although the functionality is the same, note that, as the name implies,
constructor operators construct and - without the addition
`BASE` - target objects are initialized. Hence, the following
@@ -578,11 +680,10 @@ dref2 = NEW string( `hallo` ).
"Using inline declarations to omit a prior declaration of a variable
"dref3 has the type TYPE REF TO i
DATA(dref3) = NEW i( 456 ).
dref2 = NEW string( `Hello world` ).
"Creating an anonymous structure
"Components are assigned values.
DATA(dref4) = NEW zdemo_abap_carr( carrid = 'AA' carrname = 'American Airlines' ).
DATA(dref4) = NEW zdemo_abap_carr( carrid = 'XY' carrname = 'XY Airlines' ).
"Creating an anonymous internal table
DATA dref5 TYPE REF TO string_table.
@@ -624,8 +725,8 @@ DATA(str2) = NEW zcl_demo_abap_objects( )->another_string.
"Chained attribute access
"... NEW some_class( ... )->attr ...
"Standalone NEW expression (in the case of the example method, there
"is no parameter available)
"Standalone method call with a NEW expression (in the case of the example
"method, there is no parameter available)
NEW zcl_demo_abap_objects( )->hallo_instance_method( ).
"Assumption in the following examples: The classes have an instance constructor
@@ -741,8 +842,7 @@ ENDTRY.
parenthesis determines the [static
type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstatic_type_glosry.htm "Glossary Entry")
of the result.
- The operator replaces [`GET
REFERENCE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapget_reference.htm)
- The operator replaces [`GET REFERENCE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapget_reference.htm), which should not be used anymore,
and is particularly useful for avoiding the declaration of helper
variables that are only necessary, for example, to specify data
reference variables as actual parameters.
@@ -752,24 +852,19 @@ Examples:
``` abap
"Data references
"Declaring data object and assign value
DATA num TYPE i VALUE 5.
"Declaring data reference variable
DATA dref_a TYPE REF TO i.
"Getting references
dref_a = REF #( num ).
"Inline declaration and explicit type specification
DATA(dref_b) = REF string( `hallo` ).
"Object references
DATA(oref_a) = NEW some_class( ).
DATA(oref_b) = REF #( oref_a ).
```
@@ -971,7 +1066,7 @@ DATA(time_of_day) = CONV string(
ELSE good && ` night` ) ).
"Getting a particular column name of an existing internal table using a RTTI
"Getting a particular column name of an existing internal table using RTTI
"An internal table (it contains information on the table's structured type; the
"component names, among others) is assigned to a data object that is declared
"inline. This is an example of how powerful constructor expressions (and inline

View File

@@ -24,6 +24,7 @@
- [Excursions](#excursions)
- [Enumerated Types and Objects](#enumerated-types-and-objects)
- [Getting Type Information and Creating Types at Runtime](#getting-type-information-and-creating-types-at-runtime)
- [Ranges Tables](#ranges-tables)
- [Executable Example](#executable-example)
## Introduction
@@ -1110,8 +1111,8 @@ DATA do_6_dcfl34 TYPE decfloat34 VALUE '2.78'.
DATA(do_7_i) = CONV i( do_6_dcfl34 ).
"Result: do_7_i: 3
"A maybe surprising result of a conversion
"from type i to string according to the rules
"Note conversions according to the rules, e.g. from
"type i to string
DATA(i2str) = CONV string( -10 ).
"Result: i2str: `10-`
```
@@ -1438,6 +1439,38 @@ Using [Runtime Type Creation (RTTC)](https://help.sap.com/doc/abapdocu_cp_index_
Find more information in the [cheat sheet about dynamic programming](06_Dynamic_Programming.md#runtime-type-services-rtts).
### Ranges Tables
- Internal tables that have the predefined columns `SIGN`, `OPTION`, `LOW`, and `HIGH`
- Declared with the `TYPE RANGE OF` addition in `DATA` and `TYPES` statements
- Used to store range conditions that can be evaluated in expressions using the `IN` operator (each row in the table represents a separate comparison)
```abap
"Populating an integer table with values from 1 to 20 (see iteration
"expressions with FOR furhter down)
TYPES int_tab_type TYPE TABLE OF i WITH EMPTY KEY.
DATA(inttab) = VALUE int_tab_type( FOR x = 1 WHILE x <= 20 ( x ) ).
"Declaring a ranges table
DATA rangestab TYPE RANGE OF i.
"Populating a ranges table using VALUE
rangestab = VALUE #( sign = 'I'
option = 'BT' ( low = 1 high = 3 )
( low = 6 high = 8 )
( low = 12 high = 15 )
option = 'GE' ( low = 18 ) ).
"Using a SELECT statement and the IN addition to retrieve internal table
"content based on the ranges table specifications
SELECT * FROM @inttab AS tab
WHERE table_line IN @rangestab
INTO TABLE @DATA(result).
"result: 1, 2, 3, 6, 7, 8, 12, 13, 14, 15, 18, 19, 20
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
## Executable Example

View File

@@ -275,7 +275,7 @@ The following concepts are related to the SAP LUW to ensure transactional consis
- Note the information on the `CL_ABAP_LOCK_OBJECT_FACTORY` class that is related to this context [here](https://help.sap.com/docs/sap-btp-abap-environment/abap-environment/lock-objects).
> **💡 Note**<br>
> RAP comes with its own implementation features to cover these concepts. See the topics [Authorization Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/375a8124b22948688ac1c55297868d06.html) and [Concurrency Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/d315c13677d94a6891beb3418e3e02ed.html) in the *Development guide for the ABAP RESTful Application Programming Model*.
> For more information about related topics in RAP, see the sections [Authorization Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/375a8124b22948688ac1c55297868d06.html) and [Concurrency Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/d315c13677d94a6891beb3418e3e02ed.html) in the *Development guide for the ABAP RESTful Application Programming Model*.
<p align="right"><a href="#top">⬆️ back to top</a></p>

View File

@@ -556,7 +556,7 @@ ENDMODULE.
```abap
"Getting the GUI status
GET PF-STATUS status.
st = sy-pf-key.
st = sy-pfkey.
****************************************