ABAP Quick Reference
This is the first of two posts, providing a quick reference for ABAP developers. It primarily consists of code examples and doesn’t include major object oriented programming features, introduced in newer language versions.
The second part of this series, targeting OOP related topics, can be found here: ABAP Objects Quick Reference
ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. It is currently positioned, alongside Java, as the language for programming the SAP Application Server, which is part of the NetWeaver platform for building business applications.
Wikipedia ABAP article
Basic program structure
PARAMETERS pa_num TYPE i. * declare a variable DATA gv_output TYPE i. * assign value from pa_num to gv_output MOVE pa_num TO gv_output. WRITE: 'Input value', gv_output. NEW-LINE. " this is an inline comment WRITE 'Done'.
Types
Standardtypes
I: Integer (4 bytes)F: Floating point number (8 bytes)D: Date JJJJMMTT (8 bytes)T: Time HHMMSS (6 bytes)STRING: String of dynamic lengthXSTRING: Hexadecimal stringDECFLOAT16: Decimal floating point (8 bytes)DECFLOAT34: Decimal floating point (16 bytes)- The common data type “boolean” doesn’t exist in ABAP. Use a custom solution (e.g.
Xfor true and(whitespace) for false)
Incomplete data types
C: Character array of fixed lengthN: Numerical character of fixed lengthX: Hexadecimal byte array of fixed lengthP: Packed number of fixed length (and optionally number of digits)
Type definition
TYPES gty_default_float TYPE p LENGTH 16 DECIMALS 2.
Constants
CONSTANTS gc_constvar TYPE type_name VALUE value.
Value assignments
MOVE pa_input TO gv_output. gv_output = pa_input. * increment ADD 1 TO pa_input. pa_input = pa_input + 1.
Arithmetic operators
+addition-subtraction*multiplication/division**potentiateDIVinteger divisionMODmodulo
Branching
IF pa_input > 0. " code ELSEIF pa_input = 0. " code ELSE. " code ENDIF.
CASE pa_input.
WHEN 0.
" code
WHEN -1.
" code
WHEN OTHERS.
" code
ENDCASE.Loops
DO.
" loop code
IF <abort condition>.
EDIT.
ENDIF.
ENDDO.DO n TIMES. " loop code ENDDO.
WHILE <condition>. " loop code ENDWHILE.
SELECT <fields> FROM <table> WHERE <condition>. " select loop content iterates over the result table ENDSELECT.
LOOP AT <internal table>. " loop code ENDLOOP.
System fields
sy-mandt: login mandatorsy-uname: user namesy-langu: user languagesy-datum: local date of the ABAP systemsy-uzeit: local time of the ABAP systemsy-tcode: current transaction codesy-repid: name of the current ABAP programsy-index: loop counter for DO and WHILE loops
Message boxes
* general MESSAGE tnnn(message_class) [ WITH v1 [ v2 ] [ v3 ] [ v4 ] ]. * info message example MESSAGE i005(general_messages).
i: info messagew: warninge: errora: exit programs,x
Subroutine
* subroutine call
PERFORM calc_perc
USING
gv_inta
gv_intb
CHANGING
gv_result.
* subroutine defition
FORM calc_perc
USING
pv_act TYPE i
pv_max TYPE i
CHANGING
cv_pc TYPE f.
cv_pc = pv_act * 100 / pv_max.
ENDFORM.Functions
Call function
CALL FUNCTION 'FUNCTION_NAME'
EXPORTING
iv_inta = pa_inta
iv_intb = pa_intb
IMPORTING
ev_result = gv_result
EXCEPTIONS
division_by_zero = 1
OTHERS = 2.
* exception handling of classical exceptions
IF sy-subrc <> 0.
WRITE 'Exception raised'.
ELSE.
" work with data
ENDIF.Define function (partly)
FUNCTION FUNCTION_NAME.
" code
IF iv_intb = 0.
RAISE division_by_zero. " exception
ELSE
ev_result = 50.
ENDIF.
ENDFUNCTION.Exception handling
* classical exceptions
CASE sy-subrc.
WHEN 0.
" no exception has been raised
" process data
WHEN 1.
" exception with code 1
" ...
ENDCASE.
* class based exception handling
TRY.
" call method
CATCH exception_name.
" catch code
ENDTRY.Create objects
* create reference DATA reference_name TYPE REF TO class_name. * create object CREATE OBJECT reference_name.
Call methods
* call method via reference CALL METHOD reference_name->method_name. * call static method CALL METHOD class_name=>method_name.
Structure with local type
* define structure
TYPES:
BEGIN OF gty_s_structure,
" components
field_1 TYPE i,
field_2 TYPE d,
field_3 TYPE p LENGTH 4 DECIMALS 1,
END OF gty_s_structure.
* create structure
DATA gs_name TYPE gty_s_structure.
* access component
gs_name-field1 = 5.Copy from structure to structure
DATA: gs_a TYPE gty_s_abc, gs_b TYPE gty_s_xyz. * ... MOVE-CORRESPONDING gs_a TO gs_b.
Tables
Local tables
* declare with table type DATA gt_table TYPE table_type. * detailed declaration DATA gt_table TYPE < STANDARD / SORTED / HASHED > TABLE OF structure_type WITH < NON-UNIQUE / UNIQUE > KEY ... . * short form DATA gt_table TYPE TABLE OF structure_type. * create structure like table row DATA structure LIKE LINE OF table.
Manipulate tables
APPEND structure TO table. INSERT structure INTO table <condition>. READ TABLE table INTO structure <condition>. MODIFY TABLE table FROM structure [<condition>]. DELETE table <condition>. * condition example READ TABLE table INTO structure WHERE id = 17. SORT table [BY attribute < DESCENDING / ASCENDING >]. * reset content REFRESH table. CLEAR table. * free memory FREE table.
Get data
* select multiple rows (table to table) SELECT <fields> FROM <table> INTO <target_table> WHERE <condition>. * select into structure (table to structure) SELECT SINGLE <fields> FROM <table> INTO <target_structure> ... . * select into similar structure (table to similar structure) SELECT SINGLE <fields> FROM <table> INTO CORRESPONDING FIELDS OF <target_structure> ... .
Permission check
AUTHORITY-CHECK
OBJECT 'structure'
ID 'attribute' FIELD variable.
IF sy-subsrc = 0.
" access
ELSE
" no permission
ENDIF.