TYPE numeric, "Numeric ((b, s), i, int8, p, decfloat16, decfloat34, f)
TYPE p, "Packed number (generic length and number of decimal places)
"Internal table types
TYPE ANY TABLE, "Internal table with any table type
TYPE HASHED TABLE,
TYPE INDEX TABLE,
TYPE SORTED TABLE,
TYPE STANDARD TABLE,
TYPE table, "Standard table
"Other types
TYPE simple, "Elementary data type including enumerated types and
"structured types with exclusively character-like flat components
TYPE REF TO object. "object can only be specified after REF TO; can point to any object
"Data objects to work with
DATA: BEGIN OF s,
c3 TYPE c LENGTH 3,
c10 TYPE c LENGTH 10,
n4 TYPE n LENGTH 4,
str TYPE string,
time TYPE t,
date TYPE d,
dec16 TYPE decfloat16,
dec34 TYPE decfloat34,
int TYPE i,
pl4d2 TYPE p LENGTH 4 DECIMALS 2,
tab_std TYPE STANDARD TABLE OF string WITH EMPTY KEY,
tab_so TYPE SORTED TABLE OF string WITH NON-UNIQUE KEY table_line,
tab_ha TYPE HASHED TABLE OF string WITH UNIQUE KEY table_line,
xl1 TYPE x LENGTH 1,
xstr TYPE xstring,
structure TYPE zdemo_abap_carr, "character-like flat structure
oref TYPE REF TO object,
END OF s.
"The following static ASSIGN statements demonstrate various assignments
"Note:
"- The statements commented out show impossible assignments.
"- If a static assignment is not successful, sy-subrc is not set and no
" memory area is assigned. Dynamic assignments, however, set the value.
"----- Any data type -----
ASSIGN s-c3 TO .
ASSIGN s-time TO .
ASSIGN s-tab_std TO .
ASSIGN s-xstr TO .
ASSIGN s-pl4d2 TO .
ASSIGN s-date TO .
"----- Character-like types -----
ASSIGN s-c3 TO .
ASSIGN s-c10 TO .
"ASSIGN s-str TO .
ASSIGN s-c10 TO .
ASSIGN s-str TO .
ASSIGN s-n4 TO .
ASSIGN s-date TO .
ASSIGN s-time TO .
ASSIGN s-structure TO .
ASSIGN s-c10 TO .
ASSIGN s-str TO .
"ASSIGN s-n4 TO .
ASSIGN s-n4 TO .
"ASSIGN s-int TO .
"ASSIGN s-time TO .
ASSIGN s-xl1 TO .
"ASSIGN s-xstr TO .
ASSIGN s-xl1 TO .
ASSIGN s-xstr TO .
"----- Numeric types -----
ASSIGN s-dec16 TO .
ASSIGN s-dec34 TO .
ASSIGN s-int TO .
ASSIGN s-pl4d2 TO .
"ASSIGN s-n4 TO .
ASSIGN s-dec16 TO .
ASSIGN s-dec34 TO .
ASSIGN s-pl4d2 TO .
"ASSIGN s-dec34 TO
.
"----- Internal table types -----
ASSIGN s-tab_std TO .
ASSIGN s-tab_so TO .
ASSIGN s-tab_ha TO .
ASSIGN s-tab_std TO .
ASSIGN s-tab_so TO .
"ASSIGN s-tab_ha TO .
"ASSIGN s-tab_std TO .
ASSIGN s-tab_so TO .
"ASSIGN s-tab_ha TO .
ASSIGN s-tab_std TO .
ASSIGN s-tab_std TO .
"ASSIGN s-tab_so TO .
"ASSIGN s-tab_so TO .
"ASSIGN s-tab_ha TO .
"ASSIGN s-tab_ha TO .
"ASSIGN s-tab_std TO .
"ASSIGN s-tab_so TO .
ASSIGN s-tab_ha TO .
"----- Other types -----
ASSIGN s-c10 TO .
ASSIGN s-str TO .
ASSIGN s-dec34 TO .
ASSIGN s-date TO .
ASSIGN s-structure TO .
ASSIGN s-xl1 TO .
"ASSIGN s-tab_ha TO .
ASSIGN s-oref TO .
s-oref = NEW zcl_demo_abap_objects( ).
ASSIGN s-oref TO .
s-oref = cl_abap_random_int=>create( ).
ASSIGN s-oref TO .
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) Checking Field Symbol Assignment` ) ).
"When working with field symbols, you should make sure that they are
"assigned. Otherwise, a runtime error occurs.
"You can make use of a logical expression with IS [NOT] ASSIGNED.
"The example includes data object declarations. One data object is
"assigned, the other is not. Consequently, the expression is
"true for the one and false for the other.
DATA num_b TYPE i VALUE 123.
FIELD-SYMBOLS: TYPE i,
TYPE string.
ASSIGN num_b TO .
IF IS ASSIGNED.
out->write( `Field symbol is assigned.` ).
ELSE.
out->write( `Field symbol not assigned.` ).
ENDIF.
out->write( |\n| ).
IF IS ASSIGNED.
out->write( `Field symbol is assigned.` ).
ELSE.
out->write( `Field symbol is not assigned.` ).
ENDIF.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) Unassigning Data Objects from Field Symbols` ) ).
"If you use an unassigned field symbol, an exception is raised. Before
"using it, you can check the assignment with the following logical
"expression. The statement is true if the field symbol is assigned.
"Using the statement UNASSIGN, you can explicitly remove the assignment
"of the field symbol.
DATA num_c TYPE i VALUE 123.
FIELD-SYMBOLS: TYPE i.
ASSIGN num_c TO .
IF IS ASSIGNED.
out->write( `1. Field symbol is assigned.` ).
ELSE.
out->write( `1. Field symbol is not assigned.` ).
ENDIF.
out->write( |\n| ).
UNASSIGN .
IF IS ASSIGNED.
out->write( `2. Field symbol is assigned.` ).
ELSE.
out->write( `2. Field symbol is not assigned.` ).
ENDIF.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) Type Casting with Field Symbols` ) ).
"The example demonstrates the CASTING addition. Various additions after
"CASTING are possible.
TYPES type_d_l9 TYPE c LENGTH 9.
DATA: dobj_d_l5 TYPE c LENGTH 5,
dobj_d_l10 TYPE c LENGTH 10 VALUE '1234567890',
type_name_d TYPE string VALUE 'TYPE_D_L9'.
FIELD-SYMBOLS: TYPE data,
TYPE type_d_l9.
"Casting to a statically, completely specified type
"CASTING addition without any more additions: Field symbol inherits
"the data type of the data object. The field symbol must be either
"completely typed or with one of the generic built-in ABAP types
"c, n, p, or x. The other field symbol declared in the example
"cannot be used.
ASSIGN dobj_d_l10 TO CASTING.
out->write( data = name = `` ).
out->write( |\n| ).
ASSIGN dobj_d_l10 TO CASTING TYPE type_d_l9.
out->write( data = name = `` ).
out->write( |\n| ).
"Casting to a generic type
ASSIGN dobj_d_l10 TO CASTING TYPE c.
out->write( data = name = `` ).
out->write( |\n| ).
"Casting to a static field type
ASSIGN dobj_d_l10 TO CASTING LIKE dobj_d_l5.
out->write( data = name = `` ).
out->write( |\n| ).
"Casting to a dynamic field type
ASSIGN dobj_d_l10 TO CASTING LIKE .
out->write( data = name = `` ).
out->write( |\n| ).
"Anticipating dynamic specification of data types
"for the CASTING addition.
"The type name is specified as a character-like data
"object within parentheses.
ASSIGN dobj_d_l10 TO CASTING TYPE (type_name_d).
out->write( data = name = `` ).
out->write( |\n| ).
"Anticipating RTTS
"A type description object is created which can be
"specified after the TYPE HANDLE additions.
DATA(sometype) = CAST cl_abap_datadescr(
cl_abap_typedescr=>describe_by_name( 'TYPE_D_L9' ) ).
ASSIGN dobj_d_l10 TO CASTING TYPE HANDLE sometype.
out->write( data = name = `` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6) Addressing Field Symbols` ) ).
"The example includes multiple data objects that are assigned to field
"symbols. It is demonstrated that field symbols are addressed in various
"occasions. Among them: Changing the value of data objects assigned to
"field symbols, the use of field symbols in expressions, structures, and
"internal tables.
DATA: num_e TYPE i VALUE 456,
struc_e TYPE zdemo_abap_carr,
tab_e TYPE TABLE OF zdemo_abap_carr WITH EMPTY KEY.
SELECT SINGLE *
FROM zdemo_abap_carr
WHERE carrid = 'LH'
INTO @struc_e.
FIELD-SYMBOLS: TYPE i,
TYPE zdemo_abap_carr,
LIKE tab_e,
TYPE ANY TABLE.
"Without an assignment, this would result in a runtime error:
" = 1.
ASSIGN num_e TO .
ASSIGN struc_e TO .
ASSIGN tab_e TO .
ASSIGN tab_e TO .
"Changing values
= 789.
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = num_e name = `num_e` ).
out->write( |\n| ).
"Use in expressions
DATA(calc_e) = + 211.
out->write( data = calc_e name = `calc_e` ).
out->write( |\n| ).
IF < 1000.
out->write( `The value of is less than 1000` ).
ELSE.
out->write( `The value of is greater than 1000` ).
ENDIF.
out->write( |\n| ).
out->write( |\n| ).
"Structure
out->write( data = name = `` ).
out->write( |\n| ).
DATA(comp_e1) = -carrid.
out->write( data = comp_e1 name = `comp_e1` ).
out->write( |\n| ).
-url = 'www.lh.com'.
out->write( data = -url name = `-url` ).
out->write( |\n| ).
"Internal table
SELECT *
FROM zdemo_abap_carr
ORDER BY carrid
INTO TABLE @
UP TO 3 ROWS.
out->write( data = name = `` ).
out->write( |\n| ).
TRY.
DATA(comp_e2) = [ 2 ]-carrname.
out->write( data = comp_e2 name = `comp_e2` ).
CATCH cx_sy_itab_line_not_found INTO DATA(error_e).
ENDTRY.
out->write( |\n| ).
SELECT *
FROM zdemo_abap_carr
ORDER BY carrid
INTO TABLE @
UP TO 3 ROWS.
out->write( data = name = `` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) Using Field Symbols when Processing Internal Tables` ) ).
"By using field symbols in the context of loops across internal tables,
"you can avoid an actual copying of content to a work area during
"the loop.
"The example includes multiple loops. First, internal tables are
"declared. One of them is filled. Then, field symbols are declared to
"which data objects are assigned. In the first loop, a previously
"declared field symbol is used as target area to hold the table line
"that is processed. In the course of the loop, some values are changed. The
"components are accessed using the component selector '-'. At the end of
"the loop, another internal table is filled using the currently
"processed line for the second loop. The second loop (the loop is
"carried out based on
"an internal table a field symbol points to) uses a directly declared
"field symbol using ASSIGNING FIELD-SYMBOL(<...>). Also here, some
"values are changed and another internal table is filled. This table
"is of a generic type. At the end, this internal table is output, too.
DATA: tab_f1 TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY,
tab_f2 LIKE tab_f1,
tab_f3 LIKE tab_f1.
SELECT *
FROM zdemo_abap_fli
ORDER BY carrid
INTO TABLE @tab_f1
UP TO 3 ROWS.
FIELD-SYMBOLS: LIKE LINE OF tab_f1,
LIKE tab_f1,
TYPE ANY TABLE.
ASSIGN tab_f2 TO .
ASSIGN tab_f3 TO .
LOOP AT tab_f1 ASSIGNING .
-connid = '99'.
-fldate = cl_abap_context_info=>get_system_date( ).
-price = -price + 100.
-currency = 'EUR'.
CLEAR: -paymentsum,
-seatsocc,
-seatsocc_b,
-seatsocc_f.
"Filling another itab
= VALUE #( BASE ( ) ).
ENDLOOP.
out->write( data = tab_f1 name = `tab_f1` ).
out->write( |\n| ).
"The following example shows a field symbol declared inline.
LOOP AT ASSIGNING FIELD-SYMBOL().
-connid = '100'.
-fldate = cl_abap_context_info=>get_system_date( ) + 1.
-price = -price - 50.
-currency = 'USD'.
"Filling another itab
= VALUE #( BASE ( ) ).
ENDLOOP.
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = name = `` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) Structure Assigned to Field Symbol: Accessing Structure Components` ) ).
"In this example, all components of a structure are processed using
"field symbols and an ASSIGN COMPONENT ... OF STRUCTURE ... statement.
"First, a field symbol is declared with a generic type. A structure is
"filled with values from a demo table. The structure is assigned to the
"field symbol. Using a DO loop, all components are processed. The
"sy-index value represents the position of the component in the
"structure. Once all components have been processed (i. e. if sy-subrc
"does not return '0' for a sy-index value), the loop is exited. The output
"shows all components and their values.
"See more examples for accessing structure components below.
FIELD-SYMBOLS TYPE data.
DATA comp_tab TYPE string_table.
SELECT SINGLE carrid, carrname, currcode, url
FROM zdemo_abap_carr
WHERE carrid = 'LH'
INTO @DATA(struct).
FIELD-SYMBOLS TYPE data.
ASSIGN struct TO .
DO.
"sy-index represents the position of a structure component
ASSIGN -(sy-index) TO .
"Old syntax
"ASSIGN COMPONENT sy-index OF STRUCTURE TO .
IF sy-subrc <> 0.
"If all components are processed, the loop is exited.
EXIT.
ELSE.
out->write( |sy-index: { sy-index }, component content:| ).
out->write( ).
out->write( |\n| ).
ENDIF.
ENDDO.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Data References` ) ).
out->write( |9) Declaring Data References\n\n| ).
"Like field symbols, data reference variables can be declared with both
"a complete and a generic data type using DATA statements and the
"addition REF TO. The type after REF TO represents the static data type.
"The example shows multiple ways of declaring a data reference variable
"using both complete and generic data types.
DATA: some_string TYPE string.
TYPES: ref_type TYPE REF TO zdemo_abap_flsch.
DATA: ref_a1 TYPE REF TO i, "Complete data type
ref_a2 TYPE REF TO zdemo_abap_carr, "Complete data type
ref_a3 LIKE REF TO some_string,
ref_a4 LIKE ref_a1,
ref_a5 TYPE ref_type,
ref_a6 TYPE REF TO data. "Generic data type
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) Creating Data References ` &&
`to Existing Data Objects` ) ).
"The example includes data reference variables with both complete and
"generic type. When using the REF operator, the '#' sign means that the
"type is derived from the data object. You can also explicitly specify
"the data type after REF before the parenthesis. Within the parentheses,
"you can provide a value.
"Declaring data object
DATA number_b TYPE i VALUE 5.
"Declaring data reference variables
DATA ref_b1 TYPE REF TO i.
DATA ref_data_b TYPE REF TO data.
"Creating data references to data objects.
"The '#' sign means that the type is derived from the data object.
ref_b1 = REF #( number_b ).
ref_data_b = REF #( number_b ).
"You can also use inline declarations to omit the explicit declaration.
DATA(ref_b2) = REF #( number_b ).
"You can explicitly specify the data type after REF.
"DATA(ref_b3) = REF #( g ).
out->write( zcl_demo_abap_aux=>no_output ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `11) Dynamically Creating Data Objects at Runtime Using Static Type Definitions` ) ).
"The example code shows the creation of anonymous data objects. They
"can be created using the statement CREATE DATA, the instance operator
"NEW, or the addition NEW of the INTO clause in a SELECT statement.
"A data reference variable is expected when anonymous objects are
"declared. They cannot be addressed by a name (hence anonymous).
"Note:
"- The examples cover static type definitions. As shown further down,
" there are options to dynamically specify the type definitions.
"- To output the content of the data reference variables, they
" must be dereferenced first. The details are shown further down.
"CREATE DATA statements
"Note that there are many additions available. The examples show a selection.
"Behind TYPE and LIKE, the syntax offers the same possibilities as the DATA statement.
"Creating an anonymous data object with an implicit type.
"If neither of the additions TYPE or LIKE are specified, the data reference variable
"must be completely typed.
DATA dref_c1 TYPE REF TO string.
CREATE DATA dref_c1.
"Creating anonymous data objects with explicit data type specification.
"Data reference variable with a generic type to be used in the following examples
"for the anonymous data object.
DATA dref_c2 TYPE REF TO data.
"Elementary, built-in ABAP type
CREATE DATA dref_c2 TYPE p LENGTH 8 DECIMALS 3.
"Anomyous internal table ...
"using the LIKE addition to refer to an existing internal table
DATA itab_c TYPE TABLE OF zdemo_abap_carr.
CREATE DATA dref_c2 LIKE itab_c.
"by specifying the entire table type
CREATE DATA dref_c2 TYPE HASHED TABLE OF zdemo_abap_carr WITH UNIQUE KEY carrid.
"Anonymous structures
CREATE DATA dref_c2 LIKE LINE OF itab_c.
CREATE DATA dref_c2 TYPE zdemo_abap_carr.
"Creating reference variable
TYPES elem_type_c TYPE c LENGTH 3.
CREATE DATA dref_c2 TYPE REF TO elem_type_c.
"NEW operator
"- Works like CREATE DATA dref TYPE type statements and can be used in general
" expression positions.
"- Allows to assign values to the new anonymous data objects in parentheses
"Creating data reference variables
DATA: dref_c3 TYPE REF TO i,
dref_c4 TYPE REF TO data.
"# character after NEW if the data type can be identified completely
"instead of the explicit type specification (only non-generic types)
dref_c3 = NEW #( 123 ).
dref_c3 = NEW i( 456 ).
dref_c4 = NEW zdemo_abap_carr( ). "not assigning any values
dref_c4 = NEW string( `hi` ).
"Creating anonymous data objects inline
"In doing so, you can omit a prior declaration of a variable.
DATA(dref_c5) = NEW i( 789 ).
DATA(dref_c6) = NEW zdemo_abap_carr( carrid = 'AB' carrname = 'AB Airlines' ).
"ABAP SQL SELECT statements
"Using the NEW addition in the INTO clause, an anonymous data object
"can be created in place.
SELECT *
FROM zdemo_abap_carr
INTO TABLE NEW @DATA(dref_c7) "Internal table
UP TO 3 ROWS.
SELECT SINGLE *
FROM zdemo_abap_carr
WHERE carrid = 'LH'
INTO NEW @DATA(dref_c8). "Structure
out->write( data = dref_c6->* name = `dref_c6->*` ).
out->write( |\n| ).
out->write( data = dref_c7->* name = `dref_c7->*` ).
out->write( |\n| ).
out->write( data = dref_c8->* name = `dref_c8->*` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `12) Data References and Assignments` ) ).
"Regarding the assignment, note that static types of both data
"reference variables must be compatible. As a result of an assignment,
"both the target reference variable and the source reference variable
"point to the same data object.
"Upcast/downcasts: For an assignment to work, the basic rule applies:
"The static type of the target reference variable must be more general
"than or the same as the dynamic type of the source reference variable.
"In the example below:
"Upcast: The target data reference variable is of generic type, the
"source variable is of complete type. The assignment is done with the
"assignment operator '='.
"Downcast: The target data reference variable is of complete type, the
"source variable is of generic type. The assignment is done with casting
"operators, either with the constructor operator CAST or the older ?=.
"Declaring data reference variables
DATA ref_d1 TYPE REF TO i.
DATA ref_d2 TYPE REF TO i.
ref_d1 = NEW #( 789 ).
"Assigning data reference
ref_d2 = ref_d1.
"Casting
"Complete type
DATA(ref_d3) = NEW i( 321 ).
"Generic type
DATA ref_data_d1 TYPE REF TO data.
"Upcast
ref_data_d1 = ref_d3.
"Downcasts
DATA ref_d5 TYPE REF TO i.
"Generic type
DATA ref_data_d2 TYPE REF TO data.
ref_data_d2 = NEW i( 654 ).
ref_d5 = CAST #( ref_data_d2 ).
ref_d5 ?= ref_data_d2.
out->write( data = ref_d2->* name = `ref_d2->*` ).
out->write( |\n| ).
out->write( data = ref_data_d1->* name = `ref_data_d1->*` ).
out->write( |\n| ).
out->write( data = ref_d5->* name = `ref_d5->*` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `13) Addressing Data References ` ) ).
"Before addressing the content of data objects a data reference points
"to, you must dereference data reference variables. Use the
"dereferencing operator ->*.
"The example includes multiple occasions in which data reference are
"addressed: Changing the content of a referenced data object, the use in
"logical expressions, structures, and internal tables.
"Creating data reference variables and assigning values
DATA(ref_e1) = NEW i( 1 ).
DATA(ref_e2) = NEW zdemo_abap_carr( carrid = 'LH'
carrname = 'Lufthansa' ).
"Generic type
DATA ref_data_e TYPE REF TO data.
"Copying reference
ref_data_e = ref_e1.
"Addressing
"Variable receives the content.
DATA(some_num) = ref_e1->*.
out->write( data = ref_e1->* name = `ref_e1->*` ).
out->write( |\n| ).
"Content of referenced data object is changed
ref_e1->* = 10.
out->write( data = ref_e1->* name = `ref_e1->*` ).
out->write( |\n| ).
"Data reference used in a logical expression
IF ref_e1->* > 5.
out->write( `The value of ref_e1 is greater than 5.` ).
ELSE.
out->write( `The value of ref_e1 is lower than 5.` ).
ENDIF.
out->write( |\n| ).
"Dereferenced generic type
DATA(calc) = 1 + ref_data_e->*.
out->write( data = calc name = `calc` ).
out->write( |\n| ).
"Complete structure
DATA(struc) = ref_e2->*.
out->write( data = ref_e2->* name = `ref_e2->*` ).
out->write( |\n| ).
"Individual structure component
"When dereferencing a data reference variable that has a structured
"data type, you can use the component selector -> to address individual components.
DATA(carrid) = ref_e2->carrid.
ref_e2->carrid = 'UA'.
out->write( data = ref_e2->carrid name = `ref_e2->carrid` ).
out->write( |\n| ).
"The following syntax also works (dereferencing operator and the component selector).
ref_e2->*-carrname = 'United Airlines'.
out->write( data = ref_e2->*-carrname name = `ref_e2->*-carrname` ).
out->write( |\n| ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `14) Checking if Data Reference ` &&
`Can Be Dereferenced` ) ).
"You can check if a data reference can be dereferenced by using
"a logical expression with IS [NOT] BOUND.
"The example shows both a data reference that is bound and not bound.
DATA(ref_f1) = NEW string( `hello` ).
DATA ref_f2 TYPE REF TO i.
out->write( `IF statement:` ).
IF ref_f1 IS BOUND.
out->write( `ref_f1 is bound.` ).
ELSE.
out->write( `ref_f1 is not bound.` ).
ENDIF.
out->write( |\n| ).
out->write( `COND operator:` ).
DATA(is_bound) = COND #( WHEN ref_f1 IS BOUND THEN `ref_f1 is bound.` ELSE `ref_f1 is not bound.` ).
out->write( is_bound ).
out->write( |\n| ).
out->write( `IF statement:` ).
IF ref_f2 IS BOUND.
out->write( `ref_f2 is bound.` ).
ELSE.
out->write( `ref_f2 is not bound.` ).
ENDIF.
out->write( |\n| ).
out->write( `COND operator:` ).
is_bound = COND #( WHEN ref_f2 IS BOUND THEN `ref_f2 is bound.` ELSE `ref_f2 is not bound.` ).
out->write( is_bound ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `15) Explicitly Removing a Reference` ) ).
"Note that the garbage collector takes care of removing the references
"automatically once the data is not used any more by a reference.
DATA(ref_g1) = NEW string( `hello` ).
IF ref_g1 IS INITIAL.
out->write( `Before CLEAR: ref_g1 is initial.` ).
ELSE.
out->write( `Before CLEAR: ref_g1 is not intial.` ).
ENDIF.
out->write( |\n| ).
IF ref_g1 IS BOUND.
out->write( `Before CLEAR: ref_g1 is bound.` ).
ELSE.
out->write( `Before CLEAR: ref_g1 is not bound.` ).
ENDIF.
out->write( |\n| ).
CLEAR ref_g1.
IF ref_g1 IS INITIAL.
out->write( `After CLEAR: ref_g1 is initial.` ).
ELSE.
out->write( `After CLEAR: ref_g1 is not initial.` ).
ENDIF.
out->write( |\n| ).
IF ref_g1 IS BOUND.
out->write( `After CLEAR: ref_g1 is bound.` ).
ELSE.
out->write( `After CLEAR: ref_g1 is not bound.` ).
ENDIF.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `16) Overwriting Data Reference Variables` ) ).
"A data reference variable is overwritten if a new object is created
"with a data reference variable already pointing to a data object
DATA ref_h1 TYPE REF TO data.
ref_h1 = NEW i( 111 ).
out->write( data = ref_h1->* name = `ref_h1->*` ).
out->write( |\n| ).
ref_h1 = NEW string( `ref_h1 overwritten.` ).
out->write( data = ref_h1->* name = `ref_h1->*` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `17) Retaining Data References` ) ).
"Storing data reference variables in an internal table using
"TYPE TABLE OF REF TO prevents the overwriting.
"The example demonstrates that three data references are created with
"the same reference variable in the course of a DO loop. There, the data
"reference is overwritten. However, due to saving the data reference
"variable in an internal table, the content is preserved.
DATA: ref_data_i TYPE REF TO data,
itab_i TYPE TABLE OF REF TO data,
number_i TYPE i VALUE 0.
DO 3 TIMES.
"Adding up 1 to demonstrate a changed data object
number_i += 1.
"Creating a data reference and assigning value
"In the course of the loop, the variable is overwritten.
ref_data_i = NEW i( number_i ).
"Adding the reference to an internal table
itab_i = VALUE #( BASE itab_i ( ref_data_i ) ).
ENDDO.
out->write( data = itab_i name = `itab_i` ).
out->write( |\n| ).
out->write( data = `The derefenced value of the data reference - which ` &&
`was changed in the course of the loop - in the second table ` &&
`entry is ` && itab_i[ 2 ]->* && `.` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `18) Processing Internal Tables Using ` &&
`Data References ` ) ).
"Similar use case to using field symbols: In a loop across an internal table,
"you can store the content of the line in a data reference variable
"instead of actually copying the content to boost performance.
"In the example, an internal table is created including values of
"database table. A data reference variable is declared as target area of
"the loop. In the course of the loop, some values are changed.
SELECT *
FROM zdemo_abap_flsch
WHERE distid = 'MI'
ORDER BY carrid
INTO TABLE @DATA(flsch_tab)
UP TO 3 ROWS.
"The table line is written into a data reference variable
"that is declared inline.
LOOP AT flsch_tab REFERENCE INTO DATA(ref_k).
ref_k->connid = '123'.
ref_k->distance = ref_k->distance * '1.609344'.
ref_k->distid = 'KM' .
ENDLOOP.
out->write( data = flsch_tab name = `flsch_tab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `19) Data References as Part of ` &&
`Structures and Internal Tables` ) ).
"In contrast to field symbols, data reference variables can be used as
"components of structures or columns in internal tables.
"The example includes the declaration of a structure that contains a
"data reference variable.
"The structure is filled. Based on this structure, an
"internal table is created. In a DO loop, the internal table is filled.
"Declaring a structure
DATA: BEGIN OF struc_l,
number_l TYPE i,
ref_l TYPE REF TO i,
END OF struc_l,
some_int TYPE i VALUE 0.
"Filling structure
struc_l = VALUE #( number_l = 1 ref_l = NEW #( 2 ) ).
out->write( data = struc_l ).
out->write( |\n| ).
"Declaring an internal table
DATA itab_l LIKE TABLE OF struc_l WITH EMPTY KEY.
"Filling internal table.
DO 3 TIMES.
some_int += 1.
"Filling structure
struc_l = VALUE #( number_l = some_int
ref_l = NEW #( some_int ) ).
"Filling internal table
itab_l = VALUE #( BASE itab_l ( struc_l ) ).
ENDDO.
out->write( data = itab_l name = `itab_l` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `20) Excursion: Generic Data References` ) ).
"In modern ABAP, variables and field symbols of the generic types
"'any' and 'data' can be used directly, for example, in LOOP and READ statements.
DATA dref_gen TYPE REF TO data.
CREATE DATA dref_gen TYPE TABLE OF zdemo_abap_carr.
SELECT *
FROM zdemo_abap_carr
INTO TABLE @dref_gen->*.
"Note: In case of a fully generic type, an explicit or implicit index operation
"is not possible (indicated by the examples commented out).
LOOP AT dref_gen->* ASSIGNING FIELD-SYMBOL().
...
ENDLOOP.
"LOOP AT dref->* ASSIGNING FIELD-SYMBOL() FROM 1 TO 4.
"ENDLOOP.
"The following examples use a dynamic key specification.
"See more syntax examples below.
READ TABLE dref_gen->* ASSIGNING FIELD-SYMBOL() WITH KEY ('CARRID') = 'AA'.
"READ TABLE dref->* INDEX 1 ASSIGNING FIELD-SYMBOL().
out->write( data = name = `` ).
out->write( |\n| ).
"Table expressions
DATA(line) = CONV zdemo_abap_carr( dref_gen->*[ ('CARRID') = 'AA' ] ).
out->write( data = line name = `line` ).
out->write( |\n| ).
dref_gen->*[ ('CARRID') = 'AA' ] = VALUE zdemo_abap_carr( BASE dref_gen->*[ ('CARRID') = 'AA' ] carrid = 'XY' ).
out->write( data = dref_gen->*[ ('CARRID') = 'XY' ] name = `dref_gen->*[ ('CARRID') = 'XY' ]` ).
out->write( |\n| ).
dref_gen->*[ ('CARRID') = 'XY' ]-('CARRID') = 'AA'.
out->write( data = dref_gen->*[ ('CARRID') = 'AA' ]-('CARRNAME') name = `dref_gen->*[ ('CARRID') = 'AA' ]-('CARRNAME')` ).
out->write( |\n| ).
"Table functions
DATA(num_tab_lines) = lines( dref_gen->* ).
out->write( data = num_tab_lines name = `num_tab_lines` ).
out->write( |\n| ).
DATA(idx) = line_index( dref_gen->*[ ('CARRID') = 'LH' ] ).
out->write( data = idx name = `idx` ).
out->write( |\n| ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `Dynamic ABAP Statements` ) ).
out->write( |21) Dynamic ASSIGN Statements (1) - Specifying the Memory Area Dynamically\n\n| ).
"The memory area is not specified directly, but as content of a
"character-like data object in parentheses.
"Note:
"- When specified as unnamed data object, the compiler treats the
" specifications like static assignments. Do not use named data objects
" for ASSIGN statements in ABAP for Cloud Development. It is recommended
" that existing named data objects are put in a structure. Then, the syntax
" for assigning components dynamically can be used so as to avoid a syntax
" warning.
"- Most of the following examples use an unnamed data object.
"- The specification of the name is not case-sensitive.
"Creating and populating various types/data objects to work with
TYPES: BEGIN OF st_type,
col1 TYPE i,
col2 TYPE string,
col3 TYPE string,
END OF st_type.
DATA structure TYPE st_type.
DATA it TYPE TABLE OF st_type WITH EMPTY KEY.
structure = VALUE #( col1 = 1 col2 = `aaa` col3 = `Z` ).
APPEND structure TO it.
DATA(struc_ref) = NEW st_type( col1 = 2 col2 = `b` col3 = `Y` ).
DATA dobj TYPE string VALUE `hallo`.
"The following examples use a field symbol with generic type
FIELD-SYMBOLS TYPE data.
ASSIGN ('IT') TO .
ASSIGN ('SRUCTURE') TO .
"Field symbol declared inline
"Note: The typing is performed with the generic type data.
ASSIGN ('DOBJ') TO FIELD-SYMBOL().
"The statements set the sy-subrc value.
ASSIGN ('DOES_NOT_EXIST') TO .
IF sy-subrc <> 0.
out->write( `Dynamic assignment not successful` ).
ENDIF.
"The memory area can also be a dereferenced data reference
ASSIGN struc_ref->* TO .
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `22) Dynamic ASSIGN Statements (2) - Assigning Components Dynamically` ) ).
"You can chain the names with the component selector (-), or, in
"case of reference variables, the object component selector (->).
ASSIGN structure-('COL1') TO .
ASSIGN it[ 1 ]-('COL1') TO .
ASSIGN struc_ref->('COL1') TO .
"The following example uses the dereferencing operator explicitly
"followed by the component selector.
ASSIGN struc_ref->*-('COL1') TO .
"Using a named data object for the component specification
DATA columnname TYPE string VALUE `COL1`.
ASSIGN structure-(columnname) TO .
"Fully dynamic specification
"If the compiler can fully determine the data object in ASSIGN structureatements
"in ABAP for Cloud Development, a warning is not issued.
ASSIGN ('STRUCTURE-COL1') TO .
"Numeric expressions are possible. Its value is interpreted
"as the position of the component in the structureructure.
ASSIGN structure-(3) TO .
"If the value is 0, the memory area of the entire structureructure is
"assigned to the field symbol.
ASSIGN structure-(0) TO .
"The structureatements above replace the following, older structureatements.
ASSIGN COMPONENT 'COL1' OF STRUCTURE structure TO .
ASSIGN COMPONENT 3 OF STRUCTURE structure TO .
"More examples
SELECT SINGLE * FROM zdemo_abap_carr INTO @DATA(wa).
"Reading into data reference variable
SELECT SINGLE * FROM zdemo_abap_carr INTO NEW @DATA(ref_m).
DATA(comp_name) = lcl_det_at_runtime=>get_dyn_field( ).
ASSIGN wa-(comp_name) TO FIELD-SYMBOL().
ASSIGN wa-('CARRNAME') TO FIELD-SYMBOL().
IF sy-subrc = 0.
DATA(subrc1) = sy-subrc.
ENDIF.
"No exception occurs in case of an unsuccessful assignment.
ASSIGN wa-('CRRNM') TO FIELD-SYMBOL().
IF sy-subrc <> 0.
DATA(subrc2) = sy-subrc.
ENDIF.
"Numeric expressions are possible. Its value is interpreted as the position
"of the component in the structure.
ASSIGN wa-(4) TO FIELD-SYMBOL().
"If the value is 0, the memory area of the entire structure is assigned to the field symbol.
ASSIGN wa-(0) TO FIELD-SYMBOL().
"Old syntax
ASSIGN COMPONENT 'CARRID' OF STRUCTURE wa TO FIELD-SYMBOL().
ASSIGN COMPONENT 5 OF STRUCTURE wa TO FIELD-SYMBOL().
"Dynamically specifying components of structures that are referenced by
"a data reference variable
ASSIGN ref_m->('CARRNAME') TO FIELD-SYMBOL().
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = subrc1 name = `subrc1` ).
out->write( |\n| ).
out->write( data = subrc2 name = `subrc2` ).
out->write( |\n| ).
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data = name = `` ).
out->write( |\n| ).
out->write( data =