What are the common statements in ABAP?

ABAP (Advanced Business Application Programming) is the language utilized in the SAP applications and is important in creating reports, interfaces, forms and enhancements.

ABAP (Advanced Business Application Programming) is the language utilized in the SAP applications and is important in creating reports, interfaces, forms and enhancements. If you are starting out with ABAP online certification , or you intend to improve your language skills, this blog will cover some of the frequently used statements in the ABAP programming language. This article will discuss data declaration, control statements, input/output operations, database access, exception processing and method invocation with examples and their explanations for understanding.

1. Data Declaration Statements:

DATA:

The DATA statement is an essential part of ABAP for defining variables. It's possible to define custom variable types such as elementary types which include integers, characters, etc. as well as complex types like structures and internal tables.

DATA: lv_name TYPE string,

      lv_age TYPE i

In the example shown lv_name has been defined as a string and lv_age as an integer. The specification of types is very important in making sure that the variable contains the appropriate data.

CONSTANT:

The CONSTANTS statement introduces constant characteristics that cannot be changed during the run of the program. This helps especially for the information that is not intended to change, which helps to improve the readability and maintenance of the code.

CONSTANTS: c_pi TYPE p DECIMALS 5 VALUE '3.14159'

In this case, c_pi equals the value of the mathematical constant π and is declared in the form of a packed number of five decimal digits. The use of constants protects against unintentional changes and enhances the clarity of the code.

2.Control Structures:

IF...ENDIF:

An IF instruction facilitates the execution of certain parts of a program only under certain conditions, indicating that the program may take different actions depending on whether a condition is satisfied or not. This type of control is very important when it comes to making decisions in your coding.

IF lv_age >= 18.

  WRITE: 'Adult'.

ELSE.

  WRITE: 'Minor'.

ENDIF.

In the code below, the program validates if a person's age (lv_age) is 18 years and above. If the statement is true, it prints 'Adult;' if false, it prints 'Minor.'

CASE:

The use of the CASE statement helps eliminate the need for concern about multiple condition checks as it is more efficient than the IF statements method which is prone to nesting. In this case, the problem of checking a variable against several values ​​is best suited for this type of case.

lv_age.

  WHEN 1.

    WRITE: 'Infant'.

  WHEN 13.

    WRITE: 'Teenager'.

  WHEN 18.

    WRITE: 'Adult'.

  WHEN OTHER.

    WRITE: 'Another Age'.

ENDCASE

This method helps to improve the structure and clarity of the code especially if there are several conditions to check.

WHILE:

The WHILE construct forms a construct whereby actions within it will be performed continuously until the specific condition remains valid. This helps perform actions until a particular condition is satisfied.

while lv_age < 100.

  lv_age = lv_age + 1.

ENDWHILE

In this example, the loop increments lv_age until it hits 100. In other words, loops are very important in cases where an action is to be repeated but is dependent on changing conditions.

LOOP...ENDLOOP:

The INDIRECT Statement of LOOP is quite important as it is used in traversing internal tables. In ABAP, internal tables are of utmost importance whenever a collection of data is to be worked with.

DATA: lt_friends TYPE TABLE OF string,

      lv_friend TYPE string.

 

LOOP AT lt_friends INTO lv_friend.

  WRITE: / lv_friend.

ENDLOOP.

This loop goes through each entry in the internal table lt_friends writing down the name of each friend. The data processing tasks can be made easier by the effective use of the loops.

3. Input and Output Statements:

WRITTEN:

The display output is best done using the WRITE statement as it is very simple. It can also be used to output with different formats which makes it applicable in many situations.

WRITE: / 'Hello,', lv_name.

In the given scenario, the program first outputs "Hello" and then proceeds to output the value of lv_name. The character / in this context denotes the beginning of a new line in the output.

READ:

The READ command retrieves records from internal tables or database tables and it provides options for retrieval based on the specified keys or conditions.

READ TABLE lt_friends INTO lv_friend WITH KEY name = 'John'.

IF sy-subrc = 0.

  WRITE: / 'Found:', lv_friend.

ENDIF.

In this case, the program tries to read the entry from the lt_friends that corresponds to the person whose name is “John”. The sy-subrc variable is set after the read operation to indicate whether it was successful or not (with 0 signifying success).

4.Database Operations:

Select:

The data manipulation operations of retrieval, insertion, and deletion can be performed via appropriate statements eg SELECT, INSERT and DELETE, depending on the relational database management system in use. In this case, we need to write about one operation, hence SELECT statement is discussed in this section. 

DATA: lt_users TYPE TABLE OF usr02,

      lv_user TYPE usr02-bname.

 

SELECT * FROM usr02 INTO TABLE lt_users WHERE bname = 'USERNAME'.

In the above example, data is retrieved from the table usr02 to the given username. The records fetched are kept in the internal table lt_users.

INSERT IN:

The INSERT command is utilized for the purpose of entering additional records into a table in a database that is necessary for carrying out data entry functions.

INSERT usr02 VALUES (lv_user).

This is meant to add a new user lv_user to the table usr02. When talking about the various types of operations implemented on the database, insert operations should be properly taken care of if data integrity is to be observed.

DELETE:

The DELETE command is utilized to erase entries from a specific data table, thus, it is important for data cleanup processes.

DELETE FROM usr02 WHERE bname = lv_user.

In this case, the program attempts to remove an entry in the table usr02 with the specific condition when the user's name is equal to the referenced variable lv_user. Caution is exercised while deleting records to avoid loss of data inadvertently.

The more you immerse yourself in ABAP, the more advanced concepts and techniques you will come across, including performance tuning, data modeling, and system integration. Nevertheless, understanding the basic statements in details will equip you with the appropriate building blocks for programming in ABAP. Happy Coding!


Richard Charles

9 Blog posts

Comments