ABAP - Text, String, Template
How do I use character strings in modern ABAP development and how do they behave together with inline declarations. We also look at errors that can happen with it.
In the past, when defining and creating texts, you had to attach less importance to the correct filling, since the variables were defined at the beginning of the method and the type was already fixed. With the introduction of the inline declaration, however, this distinction is becoming increasingly important, since it can lead to erroneous behavior.
As a reminder, here is an example of how to create variables using inline declarations. There are plenty of other examples, here just to show how useful the statement is.
" Loop LOOP AT lt_table INTO DATA(ls_structure). ENDLOOP. " Result from method DATA(lt_result) = get_some_data_for_me( ). " Variables DATA(ld_integer) = 5.
In the first example, we create a structure in the loop to process the data. In the second example, we get data from a method and bind the data locally into a variable. In the third example we create a variable by value. In the last example, the variable is very likely to be of the Integer data type, but the decision is up to the system.
Chars and Strings
Now we want to create a character string using an inline declaration and check which data type is created. To do this, we use the three common methods for character strings.
DATA(ld_var1) = 'Text'. DATA(ld_var2) = `Text`. DATA(ld_var3) = |Text|.
Now let's look at the information in the debugger and what data types are specified for it.
In the first case, a variable of data type CHAR is created that has exactly 4 digits, just like the text that is passed. In cases two and three, a 4-digit string is generated. As you can see, different ways produce different types of data.
How then should texts actually be generated? There is a chapter in Clean ABAP that deals with the topic. If you want to create a simple string literal, use `. If you build the text with variables, then you should use | use. Simply creating text with the pipe makes it rather illegible.
So what kind of errors can occur when using literals, you're probably wondering? Here we have an example for you:
DATA: ld_pos TYPE i VALUE 3. CASE ld_pos. WHEN 1. DATA(ld_symbol) = 'ONE'. WHEN 2. ld_symbol = 'TWO'. WHEN 3. ld_symbol = 'THREE'. WHEN OTHERS. ld_symbol = 'OTHERS'. ENDCASE.
In this case, let's take a look at LD_SYMBOL in the debugger and what the current content of the variable is.
The variable contains the wrong value, or rather 2 characters are missing because the variable was defined as a CHAR with a length of 3 characters. The reason for this is the definition of the variable in the first CASE, where it is initialized with a 3-digit CHAR and thus the final type is determined. The definition takes effect at this point, even if it is not run through. According to Clean ABAP, such a variable should logically be defined at the beginning of the method and then it probably gets the appropriate type, since this must be specified.
The VALUE statement also makes it clear once again which data type it wants when filling. This example can also be applied to the INSERT. We have to use the right literal to fill the lines, otherwise the compiler will complain with an error message. Here is the corresponding example:
DATA: lt_char TYPE STANDARD TABLE OF char4 WITH EMPTY KEY, lt_string TYPE STANDARD TABLE OF string WITH EMPTY KEY. lt_char = VALUE #( ( 'AB' ) ( 'CD' ) ( 'EF' ) ). lt_string = VALUE #( ( `GH` ) ( `IJ` ) ( `KL` ) ).
How do you get the right data type and what distinguishes the individual text types from each other, this should now be a little clearer with our article. When using it, you should be clear about what you need the literal for and use the appropriate type.