0% found this document useful (0 votes)
53 views

ABAP Programming - Day 2

Submit

Uploaded by

Smack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

ABAP Programming - Day 2

Submit

Uploaded by

Smack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

Day 2

ABAP Programming
Execute one "Hello SAP Yard" Program

Types, Data Type, Variables, Constants, Operators, Expressions etc.

Selection Screens in ABAP Programming (SELECT OPTIONS & PARAMETERS)

Radio Buttons & Check Boxes in ABAP Programming

Events in ABAP Program & Sub-Routines and make use of Std FM

Internal Tables (Indexed & Hashed), Two Types of Indexed Tables (Standard & Sorted)

How to define Internal Table Data Types and Work Area?

Input/Output Statements, Formatting Commands

Alpha Conversions (external/internal) display

Strings & Text Elements, Concatenate Statement

All Operations in Internal Tables

SELECT Statements, JOINS, For All Entries

SY-SUBRC Checks

Conditional Statements (IF/ENDIF, CASE), LOOPs, READs, Termination Statements

Modularization - Subroutines (Performs), Function Modules, Includes

Field Symbols Declaration, Usage and Assign Statement

Quizzes & Assignments

ABAP Programming
Execute one "Hello SAPYard" Program

Types, Data Type, Variables, Constants, Operators, Expressions


Data Type: - While programming in ABAP, we need to use a variety of variables to store various
information. Variables are nothing but reserved memory locations to store values. You may like to store
information of various data types like character, integer, floating point, etc. Based on the data type of a
variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Refer to ZDEMO_TRAINING_PROG in System

Page | 1 Course Content


2
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

Standard Data Type (Complete(D,T,I,F,STRING and XSTRING) and Incomplete(C,N,X,P)); Local Data
Type(Created Locally in a Program>> Can’t be accessed in other programs and created using Syntax TYPES;
Global Data Type(Created globally in DATA Dictionary and accessible throughout the SAP system>> Show
via SE11).

Selection Screens in ABAP Programming (SELECT OPTIONS & PARAMETERS) & Radio
Buttons & Check Boxes in ABAP Programming
Refer to program Z_ABAP_TRAINING

Events in ABAP Program & Sub-Routines & Making use of Std FM


Refer to Program ZDEMO_TRAINING_PROG
"Events Based Program
"Requirement : Develop a CUSTOMER Master Report to display Custno,country,nam
e1,city
TYPES: BEGIN OF ty_kna1,
kunnr TYPE kna1-kunnr,
land1 TYPE kna1-land1,
name1 TYPE kna1-name1,
ort01 TYPE kna1-ort01,
END OF ty_kna1.
DATA :it_kna1 TYPE TABLE OF ty_kna1 .
DATA : wa_kna1 TYPE ty_kna1 .
DATA : v_title(25) TYPE c .
DATA : v_land1 TYPE land1_gp.
DATA : v_fname TYPE string .
********************SELECTION SCREEN
PARAMETERS : p_land1 TYPE kna1-land1 .
PARAMETERS : p_fname TYPE rlgrap-filename MODIF ID dld.
PARAMETERS : p_dload AS CHECKBOX .

"The load of Program Event loads the program into Memory for Execution and it
s always the first event in Execution Sequence,

*LOAD-OF-PROGRAM.

"This event is used for initializing the Variables, Screen Values and Other
Default Actions

INITIALIZATION .
PERFORM init_variables.

"This is used to manipulate the Dynamic Selection Screen Changes

AT SELECTION-SCREEN OUTPUT.
PERFORM screen_modification.

"To validate the Screen Input Parameters and it validates a Single Input Fi
eld.
"If we want to Validate Multiple Input Fields we need to use AT SELECTION-
SCREEN.

Page | 2 Course Content


3
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

AT SELECTION-SCREEN ON p_land1.
PERFORM validate_land1 .

"This event allows Value-Help ot Field-Help for an Input Field

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.


PERFORM get_file_name .

"This event enables key F1 help for an input field

AT SELECTION-SCREEN ON HELP-REQUEST FOR p_fname .


PERFORM get_help .

"Default Event

START-OF-SELECTION .
PERFORM get_data.

END-OF-SELECTION .
IF p_dload = 'X' AND p_fname IS NOT INITIAL.
PERFORM download_data .
ELSE.
PERFORM display_data .
ENDIF.

"For printing the Heading for all pages

TOP-OF-PAGE .
PERFORM display_heading . "SubRoutine

"To print the same Footer for all pages


END-OF-EDITING.
PERFORM display_footer .
*
*
*&---------------------------------------------------------------------*
*& Form INIT_VARIBALES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text * <-- p2 text
*----------------------------------------------------------------------*
FORM init_variables .
v_title = 'CUSTOMER MASTER REPORT FOR TRAINING' .
p_land1 = 'IN'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SCREEN_MODIFICATION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text * <-- p2 text

Page | 3 Course Content


4
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

*----------------------------------------------------------------------*
FORM screen_modification.
IF p_dload IS INITIAL.
LOOP AT SCREEN.
CHECK screen-group1 = 'DLD'.
screen-input = '0'.
MODIFY SCREEN.
ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form VALIDATE_LAND1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text * <-- p2 text
*----------------------------------------------------------------------*
FORM validate_land1 .
SELECT land1 FROM kna1 INTO v_land1 UP TO 1 ROWS WHERE land1 = p_land1.
ENDSELECT .
IF sy-subrc <> 0 .
MESSAGE 'INVALID COUNTRY CODE' TYPE 'E' .
ENDIF .
ENDFORM. " VALIDATE_LAND1
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_data .
SELECT kunnr land1 name1 ort01 FROM kna1 INTO TABLE it_kna1 WHERE land1 = p
_land1 .
IF sy-subrc <> 0 .
MESSAGE 'NO DATA FOUND' TYPE 'I' .
ENDIF .
ENDFORM. "GET_DATA
*&---------------------------------------------------------------------*
*& Form DISPLAY_HEADING
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_heading .
WRITE : / sy-uline .
WRITE : /45 v_title COLOR 6 .
WRITE : / sy-uline .
ENDFORM. "DISPLAY_HEADING

*&---------------------------------------------------------------------*
*& Form DISPLAY_FOOTER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_footer.

Page | 4 Course Content


5
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

WRITE : / sy-uline .
WRITE : /55 'SAPYARD' COLOR 6 .
WRITE : / sy-uline .
ENDFORM. "DISPLAY_FOOTER
*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_data .
LOOP AT it_kna1 INTO wa_kna1.
WRITE : / wa_kna1-kunnr ,
wa_kna1-land1 ,
wa_kna1-name1 ,
wa_kna1-ort01 .
ENDLOOP .
ENDFORM. "DISPLAY_DATA

*&---------------------------------------------------------------------*
*& Form DOWNLOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM download_data .
v_fname = p_fname .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = v_fname "'C:\KNA1.TXT'
filetype = 'ASC' "ASC MEANS NOTEPAD FILE/DAT MEASN EXCEL F
ILE
write_field_separator = 'X'
TABLES
data_tab = it_kna1.
IF sy-subrc = 0.
MESSAGE 'DATA IS SUCCESSFULLY DOWNLOADED' TYPE 'I'.
ENDIF.
ENDFORM. "DOWNLOAD_DATA
*&---------------------------------------------------------------------*
*& Form GET_FILE_NAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_file_name .
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
program_name = syst-repid
dynpro_number = syst-dynnr
field_name = 'P_FNAME'
CHANGING
file_name = p_fname.
IF sy-subrc <> 0.
ENDIF.
ENDFORM. "GET_FILE_NAME

Page | 5 Course Content


6
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

*&---------------------------------------------------------------------*
*& Form GET_HELP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_help .
MESSAGE 'PLEASE CLCIIK ON F4 FOR FILE NAME' TYPE 'I'.
ENDFORM. "GET_HELP
*******************************************

Text-Elements: - Cover any descriptive text that appears on the Selection


Screen or on the output screen of an ABAP program. They are also used to
avoid any form of hardcoding and also from the re-usability point of view.

Internal tables are the core of ABAP. They are like soul of a body. For any
program we use internal tables extensively. We can use Internal tables like
normal data base tables only, but the basic difference is the memory allocated
for internal tables is temporary. Once the program is closed the memory
allocated for internal tables will also be out of memory.
But while using the internal tables, there are many performance issues to be
considered. i.e. which type of internal table to be used for the program like
standard internal table, hashed internal table or sorted internal table etc..

ALL PRACTICAL AND THEORATICAL CONCEPTS RELATED TO BELOW TOPICS Refer to PROGRAM: -
Z_ABAP_TRAINING

• Internal Tables (Indexed & Hashed), Two Types of Indexed Tables (Standard & Sorted)
• How to define Internal Table Data Types and Work Area?
• Alpha Conversions (external/internal) display
• Strings & Text Elements, Concatenate Statement
• All Operations in Internal Tables
• SELECT Statements, JOINS, For All Entries
• SY-SUBRC Checks
• Conditional Statements (IF/ENDIF, CASE), LOOPs, READs, Termination Statements
• Modularization - Subroutines (Performs), Function Modules, Includes
• Field Symbols Declaration, Usage and Assign Statement

"Function modules are subprograms that contain a set of reusable statements


with importing and exporting parameters.
"Unlike Include programs, function modules can be executed independently.
"SAP system contains several predefined function modules that can be called
from any ABAP program.
"Goto SE37 for FM.

Page | 6 Course Content


7
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

The Code within the program:-

"Select Statements, JOINS and For all Entries


"Select Single along with use of Case EndCase
*TABLES: mara.
*DATA : wa_mara TYPE mara, " Declare work area
* lv_matnr(18) TYPE c,
* lv_output TYPE string,
* lv_output1 TYPE string,
* lv_output2 TYPE string,
* lv_text TYPE text30.
*PARAMETERS: p_matnr TYPE char4.
*
*CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* input = p_matnr
* IMPORTING
* output = lv_matnr.
*
*SELECT SINGLE * FROM mara INTO wa_mara WHERE matnr = lv_matnr. " Read exact
record from MARA table
*IF sy-subrc EQ 0.
* CASE wa_mara-mtart.
* WHEN 'FERT'.
* lv_text = 'Finished Products'.
* WHEN 'HALB'.
* lv_text = 'Semifinished products'.
* WHEN 'HAWA'.
* lv_text = 'Trading goods'.
* WHEN OTHERS.
* lv_text = 'Not Provided'.
*ENDCASE.
*CONCATENATE 'The material type for the material number:' lv_matnr 'is' wa_ma
ra-mtart INTO lv_output1 SEPARATED BY space.
*CONCATENATE 'And the material description is' lv_text INTO lv_output2 SEPARA
TED BY space.
*CONCATENATE lv_output1 lv_output2 INTO lv_output SEPARATED BY space.
*WRITE : wa_mara-matnr, wa_mara-mtart. " Print data to screen
*ULINE.
*WRITE:/ lv_output.
*ELSE.
* WRITE: 'Not a valid Material Number'.
*ENDIF.

"Select EndSelect
*DATA : it_mara TYPE TABLE OF mara. " Declare internal table
*DATA : wa_mara TYPE mara. " Declare work area
*
*SELECT * FROM mara INTO wa_mara UP TO 50 ROWS WHERE mtart = 'FERT'. " Read 5

Page | 7 Course Content


8
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

0 appropriate records from MARA table here MTART is not a keyfield


* APPEND wa_mara TO it_mara.
*ENDSELECT.
*LOOP AT it_mara INTO wa_mara.
* WRITE :/ wa_mara-matnr, wa_mara-mtart. " Print data to screen
*ENDLOOP.

"Select with JOIN


**DATA DECLERATIONS
*TYPES: BEGIN OF t_mara,
* matnr LIKE mara-matnr, "FIELD1 FROM MARA TABLE
* mtart TYPE mara-mtart, "FIELD2 FROM MARA TABLE
* maktx TYPE makt-maktx, "FIELD1 FROM MAKT TABLE
* spras TYPE makt-spras, "FIELD2 FROM MAKT TABLE
* END OF t_mara.
*
*DATA: it_mara TYPE TABLE OF t_mara .
*DATA : wa_mara TYPE t_mara.
*SELECT mara~matnr
* mara~mtart
* makt~maktx
* makt~spras
* INTO TABLE it_mara
* FROM mara INNER JOIN makt ON ( mara~matnr = makt~matnr )
* UP TO 50 ROWS.
*
*LOOP AT it_mara INTO wa_mara.
* WRITE : / wa_mara-matnr, wa_mara-mtart, wa_mara-maktx, wa_mara-spras .
*ENDLOOP.

"For all entries is the best alternative for SELECT WITH JOINS, this stateme
nt is very helpful for reading data from more than 2 tables.
*Parent internal table must not be empty ( If it is empty, where condition fa
ils and it will get all records from database).
*Remove all duplicate entries in parent internal table
*DATA: it_mara TYPE TABLE OF mara,
* wa_mara TYPE mara.
*DATA: it_makt TYPE TABLE OF makt,
* wa_makt TYPE makt.
*SELECT *
* FROM mara
* INTO TABLE it_mara
* WHERE mtart = 'FERT'.
*IF it_mara IS NOT INITIAL.
* SELECT *
* FROM makt
* INTO TABLE it_makt
* FOR ALL ENTRIES IN it_mara

Page | 8 Course Content


9
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

* WHERE matnr = it_mara-matnr.


*ENDIF.
*LOOP AT it_makt INTO wa_makt.
* WRITE : / wa_makt-matnr, wa_makt-maktx.
*ENDLOOP.

Quizzes & Assignments


All the following are complete Standard Data Types except: -

• STRING
• C
• D
• I

In SAP ABAP there are 3 types of data Type. What are those 3 types??

We can create DATA objects by using Syntax: -

• DATA
• TYPES
• VALUE
• DEFAULT

The following code indicates:


data : lt_scarr type table of scarr with header line .
clear lt_scarr . (Hint:- We do not have to create a work area when we create an Internal Table with
Header Line).
• Initializing the Internal Table
• Initializing the work area.
• Initializing database table.
• Initializing the program.

Which of the following codes are true to assign a value to data object VAR.
• DATA VAR TYPE string. VAR <> ‘AA’.
• DATA VAR TYPE string VALUE ‘AA’.
• DATA VAR TYPE string. VAR == ‘AA’.
• DATA VAR TYPE string. VAR > ‘AA’.

Page | 9 Course Content


10
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

To create a Local Data Type, we make use of _______.

There are 3 types of DATA OBJECT. Name them??

While designing a Selection-Screen what can be the max length of a Parameter Variable name?
• 7
• 5
• 8
• 9

A sub-routine is attached to a ……………………… and a Fn. Module is attached to a ……………


• Program
• Function Group
• Data Dictionary

Can a Fn. Group have more than one Fn. Module??


• True
• False

To access Database in ABAP Program we make use of ________________


• Native SQL
• SQL syntax
• Open SQL
• SQL Language

When should we use Select Single and when should we use Select upto 1 rows?

While using FOR ALL ENTRIES in a SELECT query if the parent Internal Table is initial what will happen??

While creating an Include Program what is the Type to be chosen under Program Attributes?
• Executable
• Include
• Module Pool
• Function Group

Page | 10 Course Content


11
ABAP PROGRAMMING FOR BEGINNERS & FUNCTIONAL CONSULTANTS

What is the t-code for accessing a Fn. Module?


• SE38
• SE80
• SE37
• SE11

Exercise: -

• What is the difference between AT SELECTION-SCREEN and AT SELECTION-SCREEN ON FIELD?

• Write a events-based program to display data on screen or if the Radio Button Download is
selected then the data should be downloaded to your local machine by giving the desired file
path?

• Write a small program to display all radio-buttons horizontally rather than vertically along with
the Radio Button Text.

• Explore SELECTION-OPTIONS with different additions like NO INTERVALS, NO-EXTENSION,


combination of both, NO DATABASE SELECTION, NO-DISPLAY, VALUE-REQUEST FOR LOW/HIGH,
HELP-REQUEST AND so on with small use cases.

Page | 11 Course Content

You might also like