SAP Tutorials Blog


 


The VALUE operator can be used to construct the content of complex data objects such as structures or internal tables.

It is particularly useful because assigning values by addressing the structure components individually can be very cumbersome, especially when assigning values to structure components at the operand position.

If the type of the operand can be inferred implicitly, the # character can be used used before the parentheses. Otherwise, the type must be specified explicitly.

The VALUE operator and inline declarations can be used to create and populate structures in one go.

Note that there are special conversion and comparison rules for structures. See the ABAP Keyword Documentation for more details.

"# used: type of the operand can be implicitly derived

address = VALUE #( name   = `Mr. Duncan Pea`

                   street = `Vegetable Lane 11`

                   city   = `349875 Botanica` ).


"Declaring a structure inline

"Type used explicitly: type of the operand cannot be implicitly derived

DATA(addr) = VALUE addr_struc( name   = `Mr. Duncan Pea`

                               street = `Vegetable Lane 11`

                               city   = `349875 Botanica` ).



"Using the BASE addition to retain existing component values

addr = VALUE #( BASE addr street = `Some Street 1` ).

*NAME              STREET           CITY           

*Mr. Duncan Pea    Some Street 1    349875 Botanica


"Without the BASE addition, the components are initialized

addr = VALUE #( street = `Another Street 2` ).

*NAME       STREET              CITY   

*           Another Street 2           


"Nesting value operators

TYPES: BEGIN OF struc_nested,

        a TYPE i,

        BEGIN OF nested_1,

          b TYPE i,

          c TYPE i,

        END OF nested_1,

        BEGIN OF nested_2,

          d TYPE i,

          e TYPE i,

        END OF nested_2,

      END OF struc_nested.


DATA str_1 TYPE struc_nested.


str_1 = VALUE #( a        = 1 

                 nested_1 = VALUE #( b = 2 c = 3 ) 

                 nested_2 = VALUE #( d = 4 e = 5 ) ).


"Inline declaration

"Component a is not specified here, i.e. its value remains initial.

DATA(str_2) = VALUE struc_nested( nested_1 = VALUE #( b = 2 c = 3 ) 

                                  nested_2 = VALUE #( d = 4 e = 5 ) ).


"Apart from the VALUE operator, the NEW operator can be used to create

"a data reference variable (and populate the structure)

DATA(str_ref) = NEW struc_nested( a        = 1 

                                  nested_1 = VALUE #( b = 2 c = 3 ) 

                                  nested_2 = VALUE #( d = 4 e = 5 ) ).


Serkan AKKAVAK

Computer Engineer BSc

Head of SAP & Software Department

Contact : serkurumsal@yandex.com