SAP - ABAP Basics


SAP R/3 - ABAP - BASICS

 

SAP originally developed the ABAP/4 (Advanced Business Application Programming) language for internal use. It is being constantly improved  and modified  to meet the needs of the business world. Today, ABAP/4 is the sole tool used by SAP to develop all of its applications. Now ABAP/4 is just called "ABAP", by SAP. The ABAP/4 Development Workbench (DW) contains all the tools needed to create and maintain ABAP programs. ABAP is a fourth generation language which supports structured programming.  Some of its basic features include the following:

    ABAP/4 contains

    -   declarative elements for declaring data of various types

    -   operational elements for data manipulation

    -   control elements for controlling the program flow

    -   event elements for reacting external events

     

    ABAP/4 supports several languages.  Text elements (e.g. titles, headers, and other text) are stored separately from the program code.  You can change translate and maintain these at any time without changing the program code.

     

    ABAP/4 supports business data types and operations.  You can perform calculations with special date and time fields.

     

    ABAP/4 contains a subset of SQL called Open SQL which will enable the user to read and access database tables regardless of the database system (Oracle, DB/2, etc.).

     

    ABAP/4 allows the user to define and process internal tables which exist  only as long as the program is running.  Internal tables make it easier to work with database tables.

     

    ABAP/4 allows you to define and call subroutines.  You can also call the subroutines of other programs.  Parameters can be passed to and from subroutines in various ways.

     

    ABAP/4 contains a special kind of subroutine known as a function module.  You create and maintain function modules in a central library. They can be tested in a stand-alone mode independently of the calling program.

     

    There are two main types of ABAP/4 programs

-   Report Programs
Report programs are used to analyze data from database tables.  In reporting you use ABAP/4 Open SQL to read data from the R/3 database. A report consists of a selection screen, on which you define the data set you wish to display, and a list, which displays the data itself.  Typically, reports merely read data from the database.  However, it is also possible  to make changes in the database using a report.  Report programs are based on logical databases which are special ABAP/4 programs which provide access to all databases. List in SAP is called a report.
-   Dialog Programs
Dialog programs are organized as module pools which contain dialog modules. Each "dynpro" (a Dynamic program which consists of a screen and its flow logic) is based on exactly one ABAP/4 dialog program. In dialog programming you use the Screen Painter to create screens and program the sequence in which they appear.  You write a ABAP program (ABAP/4 module pool) for your screens. Dialog program is a SAP transaction.

Creating an ABAP Program

Creating a simple ABAP Program involves the following basic steps:
    Naming the program

    Specifying the program attributes

    Writing the program code

    Testing the program

The procedures described here apply to reports and short training programs. To create dialog programs (module pools for a new transaction, you can proceed in a different way than for a report program).  There are two menu paths to create a new program.
Tools -> ABAP/4 Workbench; Choose ABAP/4 Editor; Enter a name for the program you want to create in the program field; Choose Create; ABAP/4 Program attribute screen appears.

Tools -> ABAP/4 Workbench; Choose Object Browser; Choose Program under Object List; Choose Program Objects under Single Object list; Enter the name of program and click Display.   If the program does not already exist, you are asked whether you want to create it - yes

Some rules and conventions for naming a program
    Use at least one, but not more than eight, characters

    Do not use the following characters:

    -   period .

    -   comma ,

    -   space

    -   parentheses ( )

    -   single or double quotation marks ' "

    -   equal sign =

    -   asterisk *

    -   percentage % and underscore _

    Report programs (for outputting  data analyses in list format): Yaxxxxxx or Zaxxxxxx. Replace a with the classification letter of the application area (f for finance, s for sales, etc.). Replace x with any valid character. SAP report programs follow a similar naming convention: Raxxxxxx.

    Any other ABAP/4 programs (training programs or programs for transactions); SAPMYxxx or SAPMZxxx. Replace x with any valid character. Standard SAP programs follow a similar naming convention: SAPMaxxx, where a represents an application area.

Program attributes

 
When you have assigned  a name to the program and chosen Create on the ABAP/4 editor: the Program Attributes screen appears - To enter the program attributes:
    Enter the title for the program in the field Title.  Choose a title which describes the function of the program.  The system automatically incorporates the title in the text elements. If you want to change the title later you can change it: Choose Text Elements or Attributes on the ABAP/4 Editor: Initial Screen, Choose Change.

    Complete the  two mandatory fields Type and Application

-   For a report program enter 1 and for module pool enter M in the Type. You can look at possible values in the list box.

-   Enter the classification letter for your application in the Application field i.e. F for financial accounting

If you are creating a report (type = 1) Choose, Enter. The system automatically inserts input fields for report specific attributes:
    Type

    Apart from type 1 and type M, there is a type I for include programs. An include program is an independent program which contains program code, that can be used by different programs. In addition, include programs serve to modularize program source codes into logically related parts.  Each of these parts is stored in a different include program.  Include programs improve readability of source code and facilitate maintenance.

    Development Class

    The development class is important for transporting between systems.  When performing a transport, you can combine workbench objects (different programs, tables, transactions, etc.) assigned to one development class together.  If you are working in a team, you may have to assign your program to an existing development class or create a new one. Programs assigned to development calass $TMP are private objects and can not be transported to other systems.  You can, however change the development class to which a program is assigned by choosing Program -> Reassign on the ABAP/4 Editor, Initial Screen.

    Logical database from application (report programs only)

    These attributes determine which logical database the report uses to retrieve data and to which application the logical database belongs.  Database names within an application must be unique. However, the entire system may contain several databases with the same name.   It is therefore important to specify an application.  If your program reads data directly instead of using a logical database, you should specify an application as usual but leave the logical database field blank.

    Selection Screen Version (report programs only)

    If you do not specify a selection screen version, the system automatically creates a selection screen based on the logical database selection criteria.  If you want to use your own selection screen, enter a number in this field.  The number must be smaller than 1000.

    Editor lock

    If you set this attribute, other users cannot modify, rename, or delete your program. Only you can release the lock.

    Fixed point arithmetic

    If you set this attribute the system rounds type P fields according to the number of decimal places.

 

After entering the program attributes save the attributes by choosing Save.   Maintain Object Catalog Entry window appears. Enter the development class $TMP or choose Local Object. Save the development class by choosing Save.  You will be now in the Program Attributes Screen.

Leave the screen by choosing back. If you want to branch to the ABAP/4 editor directly,

choose Goto -> Source code.

Writing the Program

You write your ABAP/4 program in the ABAP/4 editor.  Choose Source Code in the ABAP/4 Program Attributes Screen, or Select Source Code and Choose Change on the ABAP/4 Editor - Initial Screen.

The ABAP/4 Editor: Edit Program screen appears. The editor can run in three different modes: "Command Mode", "PC Mode with line numbering", and "PC Mode without line numbering".  You can switch between editor modes by choosing Settings -> Editor Mode.  The system automatically enters the first  ABAP/4 statement, e.g. REPORT <report name> or PROGRAM <program name>.  For report name/ program name the system uses the name you entered on the ABAP/4 Editor: Initial Screen.

Enter the program code.  Check the Syntax by choosing Check.   The system then scans the program code for any syntax errors and incompatibilities.  In an error, is detected, a message appears to report this and, if possible proposes a solution or correction. The cursor is placed in the appropriate position.  Then save the code by choosing Save.  The source text is stored in the program library.  You can also change the source code of the program by Choosing Change from ABAP/4 editor. You can again check for syntax errors after making changes and then save the source code.  If the development class is $TMP the changed version is saved immediately. If it is not $TMP you see a window where you can specify a correction number.

Testing the Program

Test the program to check whether the code works correctly. Choose Program -> Execute  on the ABAP/4 Editor: Edit Program screen. If you are creating an ABAP/4 program module (dialog programs), you must create a transaction code before running the program.  For testing purposes, you can run a program that is not part of a module pool without saving it first.  The editor keeps a temporary version containing the changes.  However, you must return to the editor after the test is over to ensure that all changes are saved.

Assigning Transaction Codes to Report Programs

You can assign transaction codes to standalone programs of type 1 and handle these programs like transactions afterwards. Choose Development -> Further tools -> Transactions at the ABAP/4 DW screen.
Fill in a name for the transaction and choose  Create on Maintain Transaction screen.
Select Report Transaction on the following dialog window. Fill in required entries Transaction Text and Program on the Crate Report Transaction screen.  Save the transaction code in a development class.

ABAP/4 Program Syntax

Syntax Elements
Statements - must end with a period. First word should be a key word.
PROGRAM SAPMTEST.

WRITE 'FIRST PROGRAM'.

Keywords
Declarative Keywords
Define data types or declare the data objects which the program can access. Ex: TYPES, DATA, TABLES    They are processed independently of their position in the program.
Event Keywords
Define processing blocks in a program. Processing blocks are groups of statements which are processed as soon as a particular event occurs. Ex: AT SELECTION SCREEN, START-OF-SELECTION, AT USER-COMMAND
Control Keywords
Control the flow of a program according to certain conditions. Ex: IF, WHILE, CASE
Operational Keywords
The process the data (defined by declarative keywords) when certain events (triggered by event keywords) and conditions (defined by control keywords) occur. Ex: WRITE, MOVE, ADD
Comments
Comments are text elements which you can write between the statements of the ABAP program to explain the purpose to a reader. Comments are flagged by special characters which cause the system to ignore them. Comment should be used to document the program. Example:
**********************************************

*    PROGRAM SAPMTZTST

*    Created by Dr. Prattipati, 11/22/1998

*     Last Change by Dr. Grambo 11/23/1998

*     Purpose Demonstration

***********************************************

PROGRAM SAPMTZTST

***********************************************

*    Declaration Part

***********************************************

DATA.....

All lines beginning with an asterisk (*) are comments and are ignored by the system.

 

Syntax Structure

An ABAP program is a sequence of different statements which have a particular structure.   Comments can be inserted between statements.  Sequences of separate, but similar, statements can be combined into chain statements. There are no format restrictions.  Statements can be entered in free format.  This means that you can indent statements, write several statements on one line, or extend one statement over several lines.Words within a statement must be separated with at least one space.   You can insert comment lines anywhere in a program.  If you want to entire line to be a comment enter an asterisk (*) at the beginning of the line.  If you want part of a line to be a comment, enter a double quotation mark (") before the comment.   Some examples of valid Syntax Structures:
***********************************************
REPORT SAPMZTST.

TABLES SPFLT.

GET SPLFI.

    WRITE SPFLI-CITYFROM.

    WRITE SPFLI-CITYTO.

 

            WRITE SPFLI-CITYTO UNDER SPFLI-CITYFROM.
*************************************************

PROGRAM SAPMZTST.

WRITE 'THIS IS A STATEMENT'.

Could also be written as

PROGRAM SAPMZTST. WRITE 'THIS IS A STATEMENT'.

**************************************************

Statement sequence

WRITE SPFLI-CITYFROM.

WRITE SPFLI-CITYTO.

WRITE SPFLI-AIRPTO.

Chain statement

WRITE: SPFLI-CITYFROM, SPFLI-CITYTO, SPFLI-AIRPTO.

 

You could also write these statements as

WRITE:         SPFLI-CITIYFROM,

                    SPFLI-CITYTO,

                    SPFLI-AIRPTO.

**************************************************

Statement sequence;

SUM = SUM + 1.

SUM = SUM +2.

SUM = SUM + 3.

SUM = SUM + 4.

Can be written as a chain statement

SUM = SUM +:1,2,3,4.

****************************************************

Concepts of Data Types & Objects

Data types in ABAP are classified by structure and definition. Data types are either:
    elementary (non-structured) or structured

    Pre-defined or user-defined

Elementary Data Types - Pre-defined
C --  Text, character (alpha numeric characters default size = 1)

D --   Date format (yyyymmdd)

F --   Floating point number - accuracy range upto 15 decimals

I --   Integer (whole numbers - fractions are rounded not truncated)

N --  Numeric text (Telephone numbers, IDs, Account numbers, etc.)

P --  Packed Number - allows digits after the decimal point - "Fixed Point arithmetic" - rounds to the nearest decimal point specified. Used for such values as distances, weights, amounts of money, etc.

T -- Time format (HHMMSS)

X --  Hexadecimal

Numeric pre-defined data types: I,P, and F

Elementary Data Types - User defined
 
User defined elementary data types are based on pre-defined elementary data types.  To define your own elementary data types, you use the TYPES statement. Examples are   given later.
 

Structured data types

 
In general, structured data types in ABAP programs are user defined. There are two kinds of structured data types:
    Field Strings: A consecutive collection of other data types. A data object with a field string   data type occupies a continuous area in the memory. The components of the field strings can themselves field strings or internal tables. The field strings are defined with  TYPES  statement or with the DATA statement

    Internal tables: An internal table consists of several lines of the same type. Unlike field strings, which extend  only horizontally, internal tables also extend vertically.   You define internal tables with OCCURS parameter of the TYPES or DATA statements.

Data Objects

In ABAP/4  you can work with several kinds of data objects, such as:
Internal data objects: They are created for use in one particular program. They have no validity outside this program.  Internal data objects are literals (text literals, and number literals), variables and constants. Text literals are sequences of alphanumeric characters enclosed  in single quotation marks ex: 'Antony Smith'. Example of number literals: '-8473.67'.

Variables: A variable is used to store and reference data under a certain name and in a certain format.  Variables can differ in name, type, length, and structure.   You declare variables in your program with the DATA statement.

Constants: A constant is a data object containing a value which you fix at initialization.   Their values can not be changed during the execution of the program.  You declare constants with the CONSTANTS statement.  Use constants if you need specific value frequently in your program. In this case do not use literals.

External Data Objects: External data objects exist independently of programs.   You can not work with them directly, but you can copy them to internal data objects and write them back when you have finished.  External data objects can be used globally throughout the system environment.  External data objects  are stored in tables defined in ABAP/4 dictionary.  To access this data from within a program, you declare the tables in the program with the TABLES statement.

System Defined Data Objects: Some data objects are defined automatically by the system and do not need to be declared.  System-defined data objects include: SPACE (constant of type C) and SYSTEM FIELDS. All system fields have names with the format SY-<name>.  Some examples of system fields are: SY-SUBRC (return code value '0' if an operation is successful) SY-UNAME (log on name of the user), SY-DATUM (current date), SY-UZEIT (current time), SY-TCODE (current transaction), etc.

Special Data Objects: ABAP/4 also includes some data objects with special features - namely: PARAMETERS, and SELECTION CRITERIA.

Declaring Data - Examples

Pre-Defined Elementary Data Types and Objects
PROGRAM SAPMZTST.
 

DATA TEXT1 (14) TYPE C.

DATA TEXT2 LIKE TEXT1.

DATA NUMBER TYPE I.

 

TEXT1 = 'The Number'.

NUMBER = 100.

TEXT2 = 'is an integer.'.

 

WRITE: TEXT1, NUMBER, TEXT2.

 

OUTPUT:   The Number      100 is an integer.

In this example the data objects TEXT1, TEXT2, and NUMBER are declared with data statement.   The data type of each is specified with the TYPE or the LIKE parameter of the DATA statement. The data types used here (C,I) are pre-defined in the system.  The values are assigned to the data objects, contents of which are displayed.

**********************************************************************

User Defined-Elementary Data Types and Objects

PROGRAM SAPMZTST.

TYPES MYTEXT (10) TYPE C.

TYPES MYAMOUNT TYPE P DECIMALS 2.

DATA TEXT TYPE MYTEXT.

DATA AMOUNT TYPE MYAMOUNT.

TEXT = '4 /3 = '.

AMOUNT = 4/3.

 

WRITE : TEXT,  AMOUNT.

Output:   4 /3 =          1.33

 
Here user-defined data types MYTEXT and MYAMOUNT are defined  with the TYPES statement with reference to elementary data types  that are pre-defined in the system.   Then the data objects TEXT and AMOUNT are declared with DATA statement.  Their data types are determined to be MYTEXT, and MYAMOUNT.

*******************************************************************

Example of field Strings (Structured)
PROGRAM SAPMZTST.

TYPES:     BEGIN OF NAME,

                    TITLE (5)                       TYPE C,

                    FIRST_NAME (10)        TYPE C,

                    LAST_NAME (10)         TYPE C,

                END OF NAME.

TYPES:     BEGIN OF MYLIST,

                    CLIENT                          TYPE NAME,

                    NUMBER                        TYPE I,

                END OF MYLIST.

DATA LIST TYPE MYLIST.

LIST-CLIENT-TITLE = 'Lord'.

LIST-CLIENT-FIRST_NAME = 'Howard'.

LIST-CLIENT-LAST_NAME = 'Mac Duff'.

LIST-NUMBER = 1.

WRITE LIST-CLIENT-TITLE.

WRITE LIST-CLIENT-FIRST_NAME.

WRITE LIST-CLIENT-LAST_NAME.

WRITE / 'Number'.

WRITE LIST-NUMBER.

Output:            Lord Howard   Mac Duff

                      Number             1

In this example, the structured data types NAME and MYLIST are defined with the TYPES statement.  The field string, NAME contains three components, TITLE, FIRST_NAME, and LAST_NAME, which have pre-defined elementary data type C.  The field string MYLIST contains two components, namely CLIENT and NUMBER, CLIENT it self is already structured because it is given data type NAME.  A structured data object LIST is declared with data type MYLIST. Values are assigned  to the components and their contents are then displayed.

******************************************************************

Example of Internal Tables
PROGRAM SAPMZTST.

TYPES:     BEGIN OF MYSTRING,

                        NUMBER TYPE I,

                        NAME TYPE C,

                END OF MYSTRING.

TYPES MYTAB TYPE MYSTRING OCCURS 5.

DATA STRING TYPE MYSTRING.

DATA ITAB TYPE MYTAB.

STRING-NUMBER = 1.    STRING-NAME = 'John'.

APPEND STRING TO ITAB.

STRING-NUMBER = 2.    STRING-NAME = 'Paul'.

APPEND STRING TO ITAB.

STRING-NUMBER = 3.    STRING-NAME = 'Ringo'.

APPEND STRING TO ITAB.

STRING-NUMBER = 4.    STRING-NAME = 'George'.

APPEND STRING TO ITAB.

LOOP AT ITAB INTO STRING.

    WRITE: / STRING-NUMBER, STRING-NAME.

ENDLOOP.

Output

   1  John

   2  Paul

   3  Ringo

   4  George

First   a data type MYSTRING is defined as a field string.  Then, based on field string MYSTRING, a data type MYTAB is defined  as an internal table with the OCCURS parameter of the TYPES statement.  The data objects STRING and ITAB are declared with data types MYSTRING and MYTAB. The fields of the internal table ITAB are then filled line by line. By using the field string STRING, the contents of ITAB are displayed on the screen.

__________________________________________________________________________________________________________________________
 

Compilation - Arun KK 

 

 

 

 

1 comments:

  1. nice post thanks for sharing from Swathi

    on April 10, 2014 at 10:21 AM