Usage of ABAP VALUE Operator

Bu yazıyı okumak yaklaşık 7 dakika sürer.

VALUE Definition

Using the VALUE operator, you can read data from an internal table (itab), input data into a work area (wa) or structure (for providing constant data), and utilize it for creating and reading itabs.

ABAP
report zae_value.

" Define a structure using types
types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

" Define a structure inline and assign values using VALUE
data(wa) = VALUE ty_value( name = 'Ahmet' role = 'Developer' ).

" Display the content of wa using the class below, for demo purposes only
cl_demo_output=>display( wa ).

In the example above, we prepared the types definition. Then, we made an inline definition of wa and assigned values using VALUE. The ty_value we wrote after the VALUE command indicates that the structure of wa should be like ty_value. Inside the parentheses, we provided data for the name and role fields we defined. The cl_demo_output class allows us to see the content of wa directly on the screen without debugging.

Since we made an inline definition of wa in the example above, we specified how this wa type should be after VALUE. If we had defined wa where we made DATA definitions in our program, its usage would be slightly different.

Let's examine the code below and explain it.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: wa type ty_value.
wa = value #( name = 'Enes' role = 'Analyst' ).
cl_demo_output=>display( wa ).

In this code block, the definition of wa was made earlier, and it was specified that its type would be ty_value. When assigning values inside wa using VALUE, instead of specifying the wa type after the VALUE keyword, we can directly write the # expression. This means; this wa definition was made earlier by specifying its type, and we will only assign values to it using VALUE.

Notice that our code starts with wa = value #… Instead of starting directly with wa =, this is because, as we mentioned, the wa definition was made in the previous line.

Using BASE with VALUE

With VALUE, we can transfer data from one itab to another. We can define an itab type for the newly created itab. We use the BASE command to do this.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: wa type ty_value.
data(wa2) = value #( base wa name = 'Enes' role = 'Analyst' ).
cl_demo_output=>display( wa2 ).
  • We defined a type named ty_value that will hold the name and role information.
  • We defined the structure named wa (also referred to as a work area) to be of type ty_value.
  • We made an inline definition of wa2. We started using value #, but inside the parentheses, we used the expression base wa. This expression means; define wa2, but its type should be the same as wa's type. When the program runs, it brings the type information of wa (ty_value) to replace the # in the value # expression and runs it accordingly.

Adding Multiple Rows with VALUE

Using VALUE, more than one record can be added to an itab based on the itab's type.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

" Not a commonly preferred method.
types: wa type table of ty_value with empty key.
data(itab) = value wa( ( name = 'Enes'    role = 'Analyst')
                       ( name = 'Ahmet'   role = 'Developer')
                       ( name = 'Mehmet'  role = 'Manager')
                       ( name = 'Veli'    role = 'Tea Maker')
                      ).
cl_demo_output=>display( itab ).

The most commonly used method to transfer data into an itab with value is as follows. This method should always be used.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.

itab = value #( ( name = 'Enes'    role = 'Analyst')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Manager')
                ( name = 'Veli'    role = 'Tea Maker')
               ).
cl_demo_output=>display( itab ).

Using APPEND VALUE

A single row can be added to an itab using APPEND VALUE.

Adding a single row to itab

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.

itab = value #( ( name = 'Enes'    role = 'Analyst')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Manager')
                ( name = 'Veli'    role = 'Tea Maker')
               ).

append value #( name = 'Name' role = 'Role' ) to itab. " Adding only one line

cl_demo_output=>display( itab ).

Adding multiple rows to itab

To add multiple rows to itab, we can use the structure below.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.

itab = value #( ( name = 'Enes'    role = 'Analyst')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Manager')
                ( name = 'Veli'    role = 'Tea Maker')
               ).
" Adding multiple rows to itab
 itab = value #( base itab ( name = 'name 1' role = 'role 1' )
                           ( name = 'name 2' role = 'role 2' )
               ).

cl_demo_output=>display( itab ).

Just as we might need to add multiple rows to an itab, we might also need to transfer data from one itab to another. In such a case, we can write the code as follows. The red-marked codes differ from the codes above.

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.

itab = value #( ( name = 'Enes'    role = 'Analyst')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Manager')
                ( name = 'Veli'    role = 'Tea Maker')
               ).
" Adding multiple rows from one itab to another itab
 data(itab2) = value #( base itab ( name = 'name 1' role = 'role 1' )
                           ( name = 'name 2' role = 'role 2' )
               ).

cl_demo_output=>display( itab2 ).

Reading a table using [ ] expression

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.

itab = value #( ( name = 'Enes'    role = 'Analyst')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Manager')
                ( name = 'Veli'    role = 'Tea Maker')
               ).

data(wa_read) = itab[ name = 'Enes' ].

cl_demo_output=>display( wa_read ).

In the code above, we read the entry with name 'Enes' into wa_read (structure). We can call and use it as wa_read-name. We should pay attention to case sensitivity when writing 'Enes' here. Otherwise, it will give a dump error. If we are going to read data that does not exist in the table (itab), the reading process in our code should be as follows:

ABAP
data(wa_read) = value #( itab[ name = 'Ali' ] optional ).

There is no record named Ali in the itab. In this case, wa_read will appear empty and will not dump. We prevent the dump by using the OPTIONAL keyword.

Default Value Assignment with VALUE

If we can't find the expression Ali in the itab and want to assign a default value, we need to write our code as follows:

ABAP
report zae_value.

types: begin of ty_value,
         name type string,
         role type string,
       end of ty_value.

data: itab type table of ty_value.
data: wa_def type ty_value.

itab = value #( ( name = 'Enes'    role = 'Analist')
                ( name = 'Ahmet'   role = 'Developer')
                ( name = 'Mehmet'  role = 'Müdür')
                ( name = 'Veli'    role = 'Çaycı')
               ).

wa_def = value ty_value( name = 'Adımız' role = 'Rolümüz' ).

data(wa_read) = value #( itab[ name = 'Ali' ] default wa_def ).

cl_demo_output=>display( wa_read ).

We can explain the above code as follows:

  • We have defined the values for the fields name and role inside wa_def.
  • We created wa_read and attempted to fetch the row from itab where the name field is Ali.
  • If there is no value with the name Ali in the data within itab, we stated to transfer the values inside wa_def to wa_read as default.
  • When you run the code, you will get the output "Name: Adımız Role: Rolümüz."

Change the name Ali in the wa_read line to Enes and run the program. This will clearly show you how the code works.

Resources

The sources I used while writing this article are as follows:

Video

Articles

Leave a Reply

Your email address will not be published. Required fields are marked *