SAP ABAP Coding Example
SAP ABAP Coding Example
On ABAP Coding as a SAP technical consultant should have better grip on all objects coding so that i have made small effort to gather all SAP application coding examples to understand ABAP coding ,If you new to ABAP .Here i covered all objects examples programs in one lesson .By reading this,you can understand overview of SAP ABAP program at one step and how much difficult it to understand the programming basics and code levels in ABAP .As a ABAP consultant ,you can learn here sample programs of all objects in SAP applications.
SAP ABAP Control Break Statements Example Program
REPORT
zabap_controlbreak.
TYPES: BEGIN OF ty_marc,
matnr TYPE
marc-matnr,
werks TYPE
marc-werks,
END OF ty_marc.
DATA: it_marc TYPE STANDARD TABLE OF ty_marc,
wa_marc TYPE
ty_marc,
wa_temp TYPE
ty_marc.
SELECT matnr werks FROM marc INTO TABLE it_marc UP TO 10
ROWS WHERE matnr ge 40.
SORT it_marc BY matnr.
FIELD-SYMBOLS : <matnr> type matnr.
WRITE:/ 'FULL TABLE'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
WRITE: / sy-tabix ,
wa_temp-matnr, wa_temp-werks.
ENDLOOP.
WRITE:/ 'AT FIRST AND LAST'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT FIRST.
WRITE:/ 'First
Entry'.
WRITE:/ wa_temp-matnr, wa_temp-werks.
ENDAT.
AT LAST.
WRITE:/ 'Last
Entry'.
WRITE:/ wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'AT END OF'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT END OF matnr.
WRITE: / sy-tabix
, wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'AT NEW'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
AT NEW matnr.
WRITE: / sy-tabix
, wa_temp-matnr, wa_temp-werks.
ENDAT.
ENDLOOP.
WRITE:/ 'ON CHANGE OF'.
LOOP AT it_marc INTO wa_marc.
wa_temp = wa_marc.
ASSIGN wa_marc-matnr
TO <matnr>.
ON CHANGE OF
wa_marc-matnr.
WRITE: / sy-tabix
,wa_temp-matnr, wa_temp-werks.
ENDON.
ENDLOOP.
SAP ABAP Loop Program Example
DO – ENDDO – Unconditional Loop
DO can be used to execute a certain lines of codes specific
number of times.
DO 5 TIMES.
WRITE sy-index. " SY-INDEX (system variable) - Current
loop pass
ENDDO.
Output
Loops-1
WHILE ENDWHILE – Conditional Loop
WHILE can be used to execute a certain lines of codes as
long as the condition is true.
WHILE sy-index < 3.
WRITE sy-index.
ENDWHILE.
Output
Loops-2
CONTINUE – Terminate a loop pass unconditionally.
After continue the control directly goes to the end
statement of the current loop pass ignoring the remaining statements in the
current loop pass, starts the next loop pass.
DO 5 TIMES.
IF sy-index = 2.
CONTINUE.
ENDIF.
WRITE sy-index.
ENDDO.
Output
Loops-3
CHECK – Terminate a loop pass conditionally.
If the condition is false, the control directly goes to the
end statement of the current loop pass ignoring the remaining statements in the
current loop pass, starts the next loop pass.
DO 5 TIMES.
CHECK sy-index <
3.
WRITE sy-index.
ENDDO.
Output
Loops-4
EXIT – Terminate an entire loop pass unconditionally.
After EXIT statement the control goes to the next statement
after the end of loop statement.
DO 10 TIMES.
IF sy-index = 2.
EXIT.
ENDIF.
WRITE sy-index.
ENDDO.
SAP ABAP Subroutine Example Program
FORM <subroutine name>.
...
ENDFORM.
A subroutine can be called using PERFORM statement.
PERFORM <subroutine name>.
Example Program.
PERFORM sub_display.
WRITE:/ 'After Perform'.
*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Inside
Subroutine'.
ENDFORM.
" sub_display
Output
ABAP Subroutine
Subroutines can call other subroutines and may also call
themselves. Once a subroutine has finished running, the control returns to the
next statement after the PERFORM statement.
We can terminate a subroutine by using the EXIT or CHECK
statement.
EXIT statement can be used to terminate a subroutine
unconditionally. The control returns to the next statement after the PERFORM
statement.
PERFORM sub_display.
WRITE:/ 'After Perform Statement'.
*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Before Exit
Statement'.
EXIT.
WRITE:/ 'After Exit
Statement'. " This will not be
executed
ENDFORM.
" sub_display
Output
Exit in ABAP Subroutine
CHECK statement can be used to terminate a subroutine
conditionally. If the logical expression in the CHECK statement is untrue, the
subroutine is terminated, and the control returns to the next statement after
the PERFORM statement.
DATA: flag TYPE c.
DO 2 TIMES.
PERFORM sub_display.
ENDDO.
WRITE:/ 'After Perform Statement'.
*&---------------------------------------------------------------------*
*& Form sub_display
*&---------------------------------------------------------------------*
FORM sub_display.
WRITE:/ 'Before
Check Statement'.
CHECK flag NE 'X'.
WRITE:/ 'Check
Passed'.
flag = 'X'.
ENDFORM.
" sub_display
Output
abap-subroutine-check
Function Module Example Program
REPORT Z_abaptutor.
data result like SPELL.
selection-screen begin of line.
selection-screen comment 1(15) text-001.
parameter num_1 Type I.
selection-screen end of line.
CALL FUNCTION 'function_module'
EXPORTING
AMOUNT = num_1
IMPORTING
IN_WORDS = result.
IF SY-SUBRC <> 0.
Write: 'Value
returned is:', SY-SUBRC.
else.
Write: 'Amount in
words is:', result-word.
ENDIF.
SAP ABAP Class Example Program
Report Zabaptutor.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type
char25 Value 'Public Data'.
Methods meth1.
PROTECTED Section.
Data: text2 Type
char25 Value 'Protected Data'.
PRIVATE
Section.
Data: text3 Type
char25 Value 'Private Data'.
ENDCLASS.
CLASS class1 Implementation.
Method meth1.
Write: / 'Public
Method:',
/ text1,
/ text2,
/ text3.
Skip.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data: Objectx Type
Ref To class1.
Create Object:
Objectx.
CALL Method:
Objectx→meth1.
Write: /
Objectx→text1.
From this lesson,you can learn coding examples of all SAP ABAP objects used in the SAP modules.
Comments
Post a Comment