Getting Started
3 snippetsBasic ABAP program structure
Hello World
REPORT zhello_world.
WRITE: / 'Hello, World!'.Program Structure
REPORT zprogram_name.
DATA: lv_text TYPE string.
START-OF-SELECTION.
lv_text = 'Processing'.
WRITE: / lv_text.
END-OF-SELECTION.
WRITE: / 'Complete'.Comments
REPORT zcomments.
" This is a full-line comment
DATA: lv_value TYPE i. " Inline comment
* Old-style comment (column 1)Data Declarations
4 snippetsVariables and data types
Elementary Types
DATA: lv_integer TYPE i,
lv_packed TYPE p DECIMALS 2,
lv_char TYPE c LENGTH 50,
lv_string TYPE string,
lv_date TYPE d,
lv_time TYPE t,
lv_float TYPE f.Constants
CONSTANTS: gc_max_items TYPE i VALUE 100,
gc_company TYPE string VALUE 'ACME Corp',
gc_tax_rate TYPE p DECIMALS 4 VALUE '0.0825'.Structures
TYPES: BEGIN OF ty_employee,
id TYPE i,
name TYPE string,
salary TYPE p DECIMALS 2,
hire_date TYPE d,
END OF ty_employee.
DATA: ls_employee TYPE ty_employee.Internal Tables
DATA: lt_employees TYPE TABLE OF ty_employee,
ls_employee TYPE ty_employee.
" Or inline
DATA: lt_customers TYPE STANDARD TABLE OF ty_customer
WITH NON-UNIQUE KEY id.Control Structures
4 snippetsConditionals and loops
If-Else
IF lv_age >= 18.
WRITE: / 'Adult'.
ELSEIF lv_age >= 13.
WRITE: / 'Teenager'.
ELSE.
WRITE: / 'Child'.
ENDIF.Case Statement
CASE lv_grade.
WHEN 'A'.
WRITE: / 'Excellent'.
WHEN 'B'.
WRITE: / 'Good'.
WHEN 'C'.
WRITE: / 'Average'.
WHEN OTHERS.
WRITE: / 'Failed'.
ENDCASE.Do Loop
DATA: lv_counter TYPE i VALUE 1.
DO 10 TIMES.
WRITE: / lv_counter.
lv_counter = lv_counter + 1.
ENDDO.While Loop
DATA: lv_sum TYPE i VALUE 0.
WHILE lv_sum < 100.
lv_sum = lv_sum + 10.
WRITE: / lv_sum.
ENDWHILE.Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Internal Table Operations
5 snippetsWorking with table data
Append to Table
DATA: lt_employees TYPE TABLE OF ty_employee,
ls_employee TYPE ty_employee.
ls_employee-id = 1001.
ls_employee-name = 'John Doe'.
ls_employee-salary = '75000.00'.
APPEND ls_employee TO lt_employees.Loop at Table
LOOP AT lt_employees INTO ls_employee.
WRITE: / ls_employee-id,
ls_employee-name,
ls_employee-salary.
ENDLOOP.Read from Table
READ TABLE lt_employees INTO ls_employee
WITH KEY id = 1001.
IF sy-subrc = 0.
WRITE: / 'Found:', ls_employee-name.
ENDIF.Modify Table
LOOP AT lt_employees INTO ls_employee.
ls_employee-salary = ls_employee-salary * '1.05'.
MODIFY lt_employees FROM ls_employee.
ENDLOOP.Delete from Table
DELETE lt_employees WHERE salary < 50000.
" Or specific entry
DELETE TABLE lt_employees WITH TABLE KEY id = 1001.Database Operations
5 snippetsOpen SQL for database access
Select Single
DATA: ls_customer TYPE zcustomer.
SELECT SINGLE *
FROM zcustomer
INTO ls_customer
WHERE customer_id = '1001'.
IF sy-subrc = 0.
WRITE: / ls_customer-name.
ENDIF.Select Multiple
DATA: lt_customers TYPE TABLE OF zcustomer.
SELECT *
FROM zcustomer
INTO TABLE lt_customers
WHERE country = 'US'.
LOOP AT lt_customers INTO DATA(ls_customer).
WRITE: / ls_customer-name.
ENDLOOP.Insert
DATA: ls_customer TYPE zcustomer.
ls_customer-customer_id = '2001'.
ls_customer-name = 'Jane Smith'.
ls_customer-balance = '5000.00'.
INSERT zcustomer FROM ls_customer.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.Update
UPDATE zcustomer
SET balance = balance + 100
WHERE customer_id = '1001'.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.Delete
DELETE FROM zcustomer
WHERE balance = 0.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.Modularization
3 snippetsSubroutines and function modules
Form Subroutine
PERFORM calculate_tax USING lv_amount
CHANGING lv_tax.
FORM calculate_tax USING p_amount TYPE p
CHANGING p_tax TYPE p.
p_tax = p_amount * '0.0825'.
ENDFORM.Function Module Call
DATA: lv_result TYPE string.
CALL FUNCTION 'Z_CALCULATE_TOTAL'
EXPORTING
iv_quantity = 10
iv_price = '19.99'
IMPORTING
ev_total = lv_result.Method Call (OOP)
DATA: lo_calculator TYPE REF TO zcl_calculator.
CREATE OBJECT lo_calculator.
lo_calculator->calculate(
EXPORTING
iv_amount = 100
IMPORTING
ev_result = DATA(lv_result) ).String Operations
4 snippetsString manipulation
Concatenate
DATA: lv_first TYPE string VALUE 'John',
lv_last TYPE string VALUE 'Doe',
lv_full TYPE string.
CONCATENATE lv_first lv_last INTO lv_full
SEPARATED BY space.Split
DATA: lv_full TYPE string VALUE 'John,Doe,Engineer',
lt_parts TYPE TABLE OF string.
SPLIT lv_full AT ',' INTO TABLE lt_parts.Search & Replace
DATA: lv_text TYPE string VALUE 'Hello World'.
REPLACE 'World' WITH 'ABAP' INTO lv_text.
IF sy-subrc = 0.
WRITE: / lv_text. " Hello ABAP
ENDIF.String Functions
DATA: lv_text TYPE string VALUE ' ABAP ',
lv_length TYPE i,
lv_upper TYPE string.
lv_text = condense( lv_text ).
lv_length = strlen( lv_text ).
lv_upper = to_upper( lv_text ).Object-Oriented ABAP
2 snippetsClasses and objects
Class Definition
CLASS zcl_calculator DEFINITION.
PUBLIC SECTION.
METHODS: calculate
IMPORTING iv_amount TYPE p
EXPORTING ev_result TYPE p,
constructor.
PRIVATE SECTION.
DATA: mv_tax_rate TYPE p.
ENDCLASS.
CLASS zcl_calculator IMPLEMENTATION.
METHOD constructor.
mv_tax_rate = '0.0825'.
ENDMETHOD.
METHOD calculate.
ev_result = iv_amount * ( 1 + mv_tax_rate ).
ENDMETHOD.
ENDCLASS.Create & Use Object
DATA: lo_calc TYPE REF TO zcl_calculator,
lv_total TYPE p.
CREATE OBJECT lo_calc.
lo_calc->calculate(
EXPORTING iv_amount = 100
IMPORTING ev_result = lv_total ).
WRITE: / lv_total.