Page 1 of 57
ABAP BASICS
Intelligroup Asia Private Limited
TABLE OF CONTENTS
SOME FACTS ABOUT SAP
11
14
DATA DICTIONARY
16
22
Page 2 of 57
ABAP BASICS
Intelligroup Asia Private Limited
27
Data Types....................................................................................................................................27
Data Objects.................................................................................................................................28
Kinds of Data Objects..................................................................................................................28
Parameters....................................................................................................................................30
WRITE STATEMENT
32
Formatting output........................................................................................................................33
Formatting options for all data types.........................................................................................34
Formatting options for numeric fields.......................................................................................34
BRANCHES
36
38
Page 3 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 4 of 57
ABAP BASICS
Intelligroup Asia Private Limited
OSS: Online Service System is one of the primary sources of service and support provided by
SAP. With OSS, customers can search the SAP information database and find solutions for
errors and problems with R/3 systems. You can also submit your problems to SAP.
Consulting Service: with remote consulting service customer receives immediate and updated
technical support and answers to their questions.
Maintenance service: This is the basic and most common type of support for customers in
technical support and answers to their questions.
Information Service: These are the various information sources for receiving detailed
information about the R/3 system, marketing brochures, system documentation, training
information and many more things.
Preventive services: The primary one is the Early Watch Service, which ensures successful and
efficient installation of the R/3 system in all phases. This service makes regular/performance
checks and analyzes the system to identify potential problems, help system managers and SAP
administrators to tune the system. Soon after the Early Watch session, SAP sends the customer a
report with the result of the analysis and recommendations for avoiding potential problems such
as database becoming full.
So overall SAP R/3 is an open client/server software system, designed to manage business
information needs of an entire enterprise. The whole dataflow of SAP R/3 works in an
integrated way, which means the data needs to be entered just once and the system automatically
updates other logically related data.
Page 5 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Client: Here you enter the client number. The client is group of users who has similar rights. It
can be group of users in a business entity or a whole business entity or a whole company.
User: The name of the SAP user identification. Users of the SAP system are client-specific,
which means that user belonging to one client is valid to only the particular client.
Password: It is the password that has been assigned by the system administrator.
Language: SAP R/3 system supports multinational language on the same system at the same
time, which is very useful for multinational companies with different branches in several
countries and possibly using different languages.
After entering all the fields press ENTER key and system will take you to MAIN MENU screen.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 6 of 57
ABAP BASICS
Intelligroup Asia Private Limited
User might get different screens when he logs on, depending upon default settings of the user
master record i.e., if user is DEVELOPER then the screen which he often works on is editor
screen and he can go directly to this screen, if system administrator sets this screen for the user.
Main features of any R/3 window are as follows:
R/3 standard window elements behave exactly the same, as any other standard window
application would, like minimizing a screen, setting the active window etc.
From TOP to BOTTOM, R/3 window can contain typical elements such as check boxes,
push buttons, input fields and following elements:
Menu bar is the first element of the every R/3 window. It contains the menu item
corresponding to the particular R/3 application. The two menu options SYSTEM and HELP
are always present in every R/3 window. SYSTEM menu option contains all utilities and
functions, and is available to user at all the times. The HELP menu contains all the available
options for the different types and methods of obtaining online help in the system.
Standard tool bar. The second R/3 window element is present in every R/3 window. It is
nothing but a collection of icons, which perform common functions like saving the object,
exit etc. The various icons on std. Tool bar are as follows (from left to right):
Enter
Command Field
Save
Back
Exit
Cancel
Print
Find
Find Next
First Page
Previous Page
Next Page
Last Page
Help
All icons in R/3 window application support FOCUS property. It means, if you place cursor
over an icon, the system will show the function of the icon.
Application tool bar: The next part of the screen contains icons most commonly used in that
particular task or transaction.
Status bar is the bottom line of the screen and usually shows errors or information messages
to the user. It also includes other information such as system id, session number, client,
server name and the response time.
In between application tool bar and status bar you have working area, which is different for
different screens.
Page 7 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Logging Off
User can log off the R/3 system from any screen. There are three ways of logging off the R/3 system,
which are as follows:
From the Menu bar choose SYSTEM LOG OFF. In this case, you get the log off dialog
box, which informs the user that any data not saved will be lost if continuing with the log off
procedure.
Use/NEX transaction code in the command field. This is dangerous, since it does not ask if
you want to save the data.
Clicking on the EXIT button on the R/3 initial screen.
Page 8 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Function Builder
ABAP/4 Editor
Menu Painter
Screen Painter
Form Painter
Dictionary
SE11 Initial ABAP/4 dictionary maintain screen.
Browsers
SE80 Object browser.
SE16 Data browser.
Testing Tools
SE30 Runtime Analysis
ST05 SQL Trace
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 9 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 10 of 57
ABAP BASICS
Intelligroup Asia Private Limited
R/3 Architecture
The overall R/3 system includes the following components:
Business
Applications
Basis System
Or
Middle Layer
Operating System
Data Base
Network
The UPPER layer, the functional layer contains the different business application. The
integration of all application depends upon basis system. Applications are developed in ABAP/4
Lang. (Advanced Business Application the 4th generation language)
Page 11 of 57
ABAP BASICS
Intelligroup Asia Private Limited
The R/3 basis software is the set of programs and tools, which interfaces with the operating,
system, the underlying database, protocols and the presentation interface. This layer enables all
the application to work exactly the same way no matter what operating system or database, the
system is installed on. It is an independent layer and ensures the integration of all modules.
Besides all these specific jobs, BASIS system also contains following components and thus
provides more additional features.
ABAP/4 development workbench, which in turn includes many features like repository, data
dictionary, workbench organizer, which will be discussed in later part of the topics.
ABAP/4 language, system administrative tools, all these components are used to control,
tune the R/3 system.
Spool system manages the formatting of data for printing and passing it to the host spool
system.
Mail system you can send and receive mail from the outside world (Internet).
Communication interface to external system from R/3 system: Manages communication at
the OS level (TCP/IP), at the database level & between applications too. (RFC, EDI, and
ALE)
Database interface This component supports different Relational databases from different
vendors. The main task of database interface is to convert the SQL request from the SAP
development environment to the databases own SQL environment.
Background processing with this facility you can submit your program for background
execution.
BASIS system contains the layered components that facilitate the development of client/server
architecture.
Page 12 of 57
ABAP BASICS
Intelligroup Asia Private Limited
You can have client and server on the same machine or on different machines. Each client has a
corresponding process inside the server.
One of the most used client/server configurations with the R/3 system is the 3 tiered architecture,
which separates a systems computer into 3 functional groups:
Application
Server
Presentation
Server
Presentation
Server
Application
Server
Presentation
Server
Presentation
Server
Application
Server
Presentation
Server
Presentation
Server
In above case database server stores the data centrally. Basically contains database engine and
associated processes. The database layers contain the database system used by all servers.
Application server contains software components to run the program. It contains a SAP kernel,
which can run ABAP/4 program.
The presentation server is your client through which you send your request to application server.
It is also called as SAP graphical user interfaces known as SAPGUI and is available in windows
3.1, Windows NT, Windows 95, and Macintosh. They all look similar whatever underlying
system they are running on.
The SAPGUI includes all graphical capabilities of window interface with menu bars, tool bars,
focus property, and the entire mouse clicking operations.
Updated on - Apr 20, 2004
Proprietary & Confidential
Page 13 of 57
Created on - Jan 01, 2006
ABAP BASICS
Intelligroup Asia Private Limited
The R/3 system is open system in the sense that it can run on any operating system or any
database and any communication technology. It means that:
R/3 system can run on any operating system platform such as UNIX, NT, 95, AS/400.
It supports various RDBMS such as SQL server, Oracle, Informix, DB2.
Standard GUIs supported by R/3 are Windows 95, NT, Windows 3.1, and Macintosh.
SAP can use standard communication protocols TCP/IP, CPIC, OSF/DCE/DME for network.
Page 14 of 57
ABAP BASICS
Intelligroup Asia Private Limited
ABAP/4 dictionary Defining, maintaining and storing the data dictionary of the SAP R/3
system stores all the dictionary objects including tables relationship and Help information.
Transaction code for this is SE11.
ABAP/4 editor Creating and maintaining the ABAP/4 program, editing function modules,
logical database, and screens. Transaction code is SE38.
Function library Defining and maintaining the ABAP/4 function modules. Transaction code
is SE37.
Screen painter Designing and maintaining the screens in transaction. Transaction Code is
SE51.
Menu painter Designing and maintaining the means for graphical user interface. Transaction
code SE41.
For Navigating:
Object browser Managing and organizing the development object in a hierarchical form.
Transaction code is SE80.
ABAP/4 repository information Navigating and searching for the dictionary Objects,
development objects and relationship objects. Transaction code SE84.
Data browser Navigating in the data tables of the database. Transaction code is SE 16.
For Debugging:
SOL trace tracking the database calls from the system transaction and programs. Transaction
code is ST05.
Debugger Stopping the program and analyzing the results of the execution of every program
statement.
Runtime Analysis Analyzing the performance the system calls Transaction code is SE30
For Organizing:
Workbench organizer controlling and keeping track of development work and team related
development projects and managing versions of development objects. Transaction code is
SE09.
Transport system performing and managing the transport of development object across
different system. Transaction code is SE01
Page 15 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Data Dictionary
The ABAP/4dictionary is central workbench repository utility providing the data definition and
the information relationship that are later used in all the business application within R/3
The ABAP/4 dictionary can be seen as a logical representation or a superior layer over the
physical underlying database. This database must support the relational data model. This model
is strictly followed by data dictionary.
Advantages
Advantage of using data dictionary is avoiding inconsistencies when defining data type that will
later be used in different applications. This avoids redundancies.
When a type is defined in the dictionary, it is available to any program in the application. A
change in the definition of a type of data in the dictionary automatically affects any other data or
program, which has this data.
Again, data dictionary is a fast and efficient way to answer questions such as which entries exist
in a table of the database, what the structure of table is.
Page 16 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 17 of 57
ABAP BASICS
Intelligroup Asia Private Limited
To call ABAP/4 dictionary, from the main menu, Tools ABAP/4 workbench data
dictionary or enter transaction SE11.
Page 18 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Domain: is formal definition of the data type from a technical point of view. It sets the
attributes such as data type, length, possible value range and so on.
Lock objects: These types of objects are used for locking the access to database records in
table. This mechanism is used to enforce data integrity that is two users cannot update the
same data at the same time. With lock objects you can lock table-field or whole table.
Search Help Objects: , which gives list of possible values for either primary keys or nonprimary keys.
Transparent tables
Pool tables
Cluster tables
From user point of view, all tables are used to store data whatever be the type of table. There is
no difference in the behavior or operation of these tables. All of them can be managed by using
standard OPEN SQL. However from an administrator point of view transparent table do exists
with the same structure both in the dictionary as well as in the database, exactly with the same
data and fields. While other two are not transparent in the sense that they are not manageable
directly using database system tools. You can access these tables in R/3 environment from the
ABAP/4 dictionary. You cannot use native SQL on these tables. Pool or cluster tables are
logical tables, which are arranged as records of transparent table.
A table is made up of rows and columns. When the table is created, its columns are named; data
type is supplied for each column. There can be only one data value in each column of each row
in a table. Record or as it is called in different RDBMS is nothing but group of fields. While a
column is a field of a table, a table is an indexed file. The main index is called as primary key,
which can be a single field or combination of keys or fields. A primary key can be defined as a
field, which indefinites a single unique record of the table. A table cannot have record with
duplicate primary key.
In any RDBMS, tables are related to each other. But to relate table to each other it is necessary
that one of the tables contain some information of other table. Mostly tables are related to each
other through primary keys. The primary key of one table, if it exists in other table then it is
called foreign key. This type of database management system means that there is some
redundancy of data. But using normalization procedures available can minimize it. One of the
most important functions of foreign key is to ensure data integrity. For example say you have
Updated on - Apr 20, 2004
Proprietary & Confidential
Page 19 of 57
Created on - Jan 01, 2006
ABAP BASICS
Intelligroup Asia Private Limited
EMP table, which has fields: emp. no., emp.name, dept.code, salary and you have DEPT tables,
which has dept.code and dept.desc. Then in DEPT table dept.code is primary key while
dept.code in EMP table is foreign key. If you enter dept.code for particular employee in EMP
table the dept.code should exist in DEPT table. System will check the value for dept.code in
DEPT table, and if does not exist then will flash error. In this case DEPT is called check table
while EMP is foreign key table.
Creation of table
Steps to create a table
Create domain
Create data element
Create actual table
Creating Domain
Domain as already explained defines the technical properties of a field such as type and value
range. A domain can be created from initial screen of data dictionary by clicking on create and
clicking domain Radiobutton. Parameters to be passed are:
Data type: Where you need to enter the data type available in SAP.
Field length: Field length is the number of valid position.
Value table: Name of a table to be entered. The fields referring to this domain may only assume
values contained in the value table.
Once the domain is created, save and activate it, so that it can be used for further objects (basic
rule of dictionary).
Page 20 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Domain:
A mandatory field. If the domain does not exist, SAP can take you directly to
domain definition screen.
Text element: You can enter description is short or long text for the field. This text is used when
entering data for these fields.
Save and activate.
Creation of actual table
Parameters to be passed for creation of table:
Short description:
Delivery class:
Table fields:
element.
Data class:
Size category:
Mandatory field.
As per User Requirement
Specify whether primary key. In this case it is mandatory to enter data
Establishes the physical area of the database.
Allows you to specify estimated space requirement for the table.
Further down under buffering square box, the system allows specifying whether table is going to
be buffered. When a table is buffered, it is loaded into the table buffer from the application
server memory and it will remain there until you switch off or reboot system.
If the table is to be buffered, you need to specify the type of buffering. Full is for entire table
while partial is for only those records which are being accessed.
Once the table is created, it has to be generated or activated to be able to access by other objects
like programs.
Page 21 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 22 of 57
ABAP BASICS
Intelligroup Asia Private Limited
We mainly use programs that consist of a single processing block only and, therefore, behave
most likely like programs of other sequential programming languages. For programming
applications, the entirely sequential concept is not sufficient. SAP distinguishes between two
general types of application programs:
Reports: You use reports to read databases and represent the results in lists. Reports are
collections of processing blocks that the system calls depending on events.
Dialog programs: You can dialog programs to execute transactions, which usually read and
change databases. Dialog programs are collections of processing blocks (so-called module
pools) that are called by a screen flow logic. The third part of the Users Guide describes dialog
programming in detail.
Reports can call dialog programs and vice versa.
In its easiest version, an ABAP/4 program contains one single sequential piece of coding and,
thus, one single processing block.
Characteristics of the ABAP/4 programming languages
Declarative elements for declaring data of different type and structures.
Operational elements for manipulating data.
Control elements to control processing flow.
ABAP/4 is multi-lingual. Text elements such as titles, headings, and text body are stored
separately, independent of the program codes. Thus, you can change, translate, and maintain
text elements without having no adapt the coding.
ABAP/4 supports business-related data types and operations. You can execute calculations
using special data and time fields. The system automatically executes all necessary type
conversions.
ABAP/4 provides a number of functions for processing character strings.
ABAP/4 allows you to define and call subroutines. You can even call subroutines of other
programs. There are different ways of how to pass parameters to and from the Subroutines.
ABAP/4 contains a special type of subroutine, called function module. Function modules are
stored and maintained in a central library. They have clearly defined data interfaces to the
calling program. You can test function modules in a stand-alone mode independent of the
calling program.
ABAP/4 contains an SQL subset called OPEN SQL. OPEN SQL allows you to read and
change database tables independent of the underlying database system.
ABAP/4 allows you to define and process internal tables that exist only for the execution
period of the program. Internal tables efficiently support the usage of database tables and
allow you to implement complex data structures in a program.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 23 of 57
ABAP BASICS
Intelligroup Asia Private Limited
ABAP/4 allows you to store data not only in databases but also as sequential files on
application and presentation servers.
REPORTS
Reports are ABAP/4 programs.
You use reports to evaluation data from database tables. The results of such an evaluation
can be displayed on the screen or printed form.
Reports are stand-alone programs.
The user can execute reports directly via the program name, for example, by choosing
System Utilities Reporting.
A report program contains a collection of processing blocks for different events that are
always triggered externally. In a report, you can react on events by programming the
corresponding processing blocks or ignore the events by not writing the corresponding
processing blocks. A report itself never creates events.
Reports can use logical databases or select statements defined by developer.
For each application, SAP supplies logical databases. Or you can easily create logical
database yourself.
Event control of a report corresponds to a certain scheme:
When a report is executed, the ABAP/4 processor creates together with the logical database
used (if any) a sequence of certain events for which you can program processing blocks. The
chronology of the events is (more or less)
Steps involved in creating a Report:
1. Processing the selection screen
After starting a report, the selection screen allows the user to enter limits or control
values for further report processing. The report can contain several processing blocks for
events during selection screen processing, for example, for checking the input values.
2. Reading the database
After selection screen processing come the events for reading the database. Either the
report reads data from relational databases it using the corresponding ABAP/4 statements
(open SQL) or leaves this task to a logical database. In the latter case, the logical database
creates a sequence of events to allow the report to copy the data.
3. Evaluating data and creating lists
During or after reading the database the report creates the output list. During list creation,
several events allow you to layout the output list (for example, layout the page header).
4. Outputting a list
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 24 of 57
ABAP BASICS
Intelligroup Asia Private Limited
The last part of the processing sequence controlled by the ABAP/4 processor is the list
output on the screen or printer. When displaying the list on the screen, user can trigger
other reports, that are interactive and are event driven. For example, by clicking the
mouse. By programming processing blocks for these events, you change a normal report
to a so-called Interactive report. If a report does not contain event keywords, the entire
coding of the report belongs to a single processing block, which is called by a standard
event. This standard event is triggered directly after processing the selection screen.
DIALOG PROGRAMS
You use dialog programs to execute transactions. The users of dialog programs in dialog
sessions read and change database tables. Apart from the actual data processing (Open
SQL), update and enqueue concepts are of great importance when programming dialogs.
Dialog programs are not stand- alone
To execute dialog programs, they must be linked to at least one screen that itself is linked to
a transaction code. The transaction code determines the initial screen with which the dialog
session starts.
Dialog programs are controlled by screen flow logic
The actual ABAP/4 dialog program is a so-called module pool. A module pool contains a
collection of dialog modules that are called by the screen flow logic.
To each module pool, at least one, but usually several screens are allocated. Each screen has
flow logic. The flow logic consists of PBO (process Before output) and PAI (process After
Input) blocks. This flow logic does not use the ABAP/4 programming language and the
ABAP/4 Editor tool, but a special statement set and the Screen Painter tool, which you also
use to layout screens. The flow logic mainly contains the chronologically ordered calls of the
modules in the corresponding module pool.
The collection of PBO flow logic, screen, and PAI flow logic is called Dynamic program
(Dynpro). A module pool must have at least one dynpro. Each screen of a dialog session
thus is the visible part of a dynpro, to which also the flow logic belongs. The processing logic
of a dialog session is stored in the corresponding module pool in the form of ABAP/4
modules.
The ABAP/4 modules in the module pool are separated into PBO and PAI modules. The
PBO or PAI blocks of the flow logic of each dynpro of a module pool can call each PBO or
PAI module of this module pool.
You can use ABAP/4 statements in the processing logic of the module pool to control the
chronology of the different dynpros. After starting a dialog session via the transaction code,
which is firmly connected to a dynpro of the module pool, the screen flow logic passes user
entries to the processing logic in the ABAP/4 module pool. The processing logic processes
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 25 of 57
ABAP BASICS
Intelligroup Asia Private Limited
the user entries (database accesses) and, if required, defines the appropriate subsequent
screens.
Page 26 of 57
ABAP BASICS
Intelligroup Asia Private Limited
ELEMENTARY
STRUCTURED
Predefined
User-defined
C, D, F, I, N, P, T, X
E.g.,
TYPES: number types I.
Cant allocate memory to
types.
Page 27 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Data Objects
Data objects are units created during runtime.
Data object cannot exist without data type.
Occupies memory space.
Kinds of Data Objects
1.
Literal
A literal has a fixed value.
Ex WRITE: WORK HARD
Variables
DATA statement is used to create variables
Ex DATA: NUM TYPE I
NUM is a variable declared by DATA statement. Any variable, which you use in program,
need to be declared before you use it and can be done by DATA statement.
Here variable is declared by referring to existing data type.
Variable can also be declared by referring existing data object.
Ex. We have already declared NUM by DATA statement.
DATA: PRICE LIKE NUM.
Here variable is declared by using LIKE parameter, which tells system that price has all the
attributes of data object NUM i.e., PRICE is also of type I.
The main difference between TYPE and LIKE parameter when defining or declaring the
object is that TYPE is used to refer existing DATA TYPE (elementary or structured or user
defined) while LIKE is used to declare data objects with reference to existing DATA
OBJECTS.
Constant
Constant is a data object, which contains fixed value through out the program. Constant can
be declared in program by using CONSTANT statement.
Ex. CONSTANT: INT TYPE I VALUE 15.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 28 of 57
ABAP BASICS
Intelligroup Asia Private Limited
In program value of INT cannot be changed. If you give a statement like INT = 20.
In this case system will give error.
2.
3.
4.
Page 29 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Parameters
Parameter statement is used to accept input from user. PARAMETER statement is used when
you want user to enter data and depending upon what he enters you need to take action. The
parameter statement declares the variable and also allows system to accept data into that
variable.
Syntax.
Parameters: num type I.
Here parameter statement declares the variable and creates the selection screen on which user
enters the data i.e., in this case num is declared of type I and user can enter any number. Entered
value is stored in the same variable and can be used in program.
Data: m type I
Parameters: num type I
M = num 5
Write: / The number is, m.
You can define default values with parameter statement for example
Parameter: num type I default 12.
In this case when selection screen is displayed the default value is displayed. User can either use
same value or overwrite the value.
Parameter of type character and length = 1, can be displayed as Checkbox and Radiobutton.
Parameter:
C1 as Checkbox,
C2 as Checkbox.
Parameter:
R1 Radiobutton group g1,
R2 Radiobutton group g1.
When parameter is defined as Radiobutton, it needs to be attached to one group. Only one
Radiobutton of one group can be clicked.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 30 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Every parameter can be associated with language dependent text that is displayed on the
selection screen. This can be done with the help of text elements.
Page 31 of 57
ABAP BASICS
Intelligroup Asia Private Limited
WRITE Statement
The basic APAB/4 statement for outputting data on the screen is WRITE.
Syntax:
WRITE <field> <option>.
This statement outputs the field <f> to the current list in its standard output format.
By default, the list is displayed on the screen.
The field <field>can be any variable or table field or just literal.
PROGRAM ZDEMO
WRITE: /HELLO.
When you start this program, the system leaves the current screen i.e., your editor screen and
branches to the output screen, which is also called as list screen:
The list screen has the same name as the title of the program specified in the program attributes.
First line on the screen contains the list header. By default, the list header is the same as the title
of the program. The current page number (1) appears on the right. The list header is followed
by one line and then the output is displayed.
Write : HELLO.
Write : WORK HARD
On the screen, the output is normally left justified. But in above case, because we have used two
WRITE statements, the output fields are displayed one after the other, each separated by one
column (i.e., one blank). If there is not enough space for an output field on the current line, a
new line is started.
Almost all system-defined fields are right justified except FLOAT, INTEGER, and PACKED
i.e., number field. The numeric data types F, P, and I are right justified and padded with blanks
on the left. If there is sufficient space, thousands of separators are also output. If a type P field
contains decimal places, the default output length is increased by one.
With the data type D, the internal format of a date differs from its output format. When you use
the WRITE statement for outputting data, the system automatically outputs dates of type D in the
format specified in the users master record (e.g. DD/MM/YYYY or MM/DD/YYYY).
Page 32 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Formatting output
You can position the output of a WRITE statement on the screen by making a format
specification before the field name as follows:
Syntax:
WRITE AT [/][<pos>][(<len>)] <f>,
Where
For variables you need to mention the AT, for direct values it is not necessary.
DATA: LEN TYPE I VALUE 10,
POS TYPE I VALUE 11,
TEXT (10) VALUE 1234567890
WRITE AT POS (LEN) TEXT.
This produces the following output on the screen;
The text 1234567890 appears in the text.
If the output length <len> is too short, fewer characters are displayed. Numeric fields are
truncated on the left and prefixed with an asterisk (*). All other fields are truncated on the right,
but no indication is given that the field is shorter.
DATA: NUMBER TYPE I VALUE 1234567890,
TEXT (10) VALUE abcdefghij.
WRITE: (5) NUMBER, /(5) TEXT.
This produces the following output:
7890
abcde
In the default setting, you cannot create empty lines with the WRITE statement.
WRITE: One,
/ ,
/ Two
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 33 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Purpose
Output is left justified.
Output is centered.
Output is right justified.
The blank after the field <f> is omitted.
If a field contains only zeros, these are
replaced by blanks. For type C and N
fields, leading zeros are replaced
automatically.
Purpose
The leading sign is not output.
<d> defines the number of digits after the
decimal point.
In type F fields, the exponent is defined in
<e>
Type P fields are multiplied by 10**(-r)
and then rounded
Format according to currency <c> in table
TCURX.
The number of decimal places is fixed
according to the unit
<u> specified in table T006 for type P
fields.
Proprietary & Confidential
Page 34 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Horizontal lines
You can generate horizontal lines on the output screen by using the following syntax:
Syntax
ULINE
Will draw a horizontal line.
ULINE (10)
Will start drawing horizontal line from 10th column position.
WRITE at 10(40) SY-ULINE
This statement draws a horizontal line from 10th position.
Vertical lines
You generate vertical lines one the output screen by using the following syntax:
Syntax
WRITE [AT [/] [<pos>]] SY-VLINE.
Blank lines
You can generate blank lines on the screen by using the following syntax :
Syntax
SKIP [<number>]
Starting on the current line, this statement generates <number> blank lines on the output screen.
If no value is specified for <number>, one blank line is output. In the standard setting, you
cannot create empty lines with the WRITE statement alone.
To position the output on a specific line on the screen use:
Syntax
SKIP TO LINE <number>
This statement allows you to move the output position upwards or downwards.
Page 35 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Branches
Like other higher programming languages, ABAP/4 provides standard keywords to control the
flow of a program.
Usually ABAP/4 programs get executed statement by statement. Many times you need to skip
few statements depending upon certain conditions i.e., you change the flow of program. This
can be done by:
branching (IF, CASE)
looping (DO, WHILE)
However, unlike other language where you have only internal control, ABAP/4 has internal
control and external control of the program flow.
The internal control is steered by standard keywords as mentioned above. You define this
in your program code.
The external control is stored by events. Events are generated either from other ABAP/4
programs or from interactive user input (like, for example, using the mouse to click on the
screen). The system does not necessarily process the statements in the same sequence as they
are listed in an ABAP/4 program. This makes ABAP/4 an event-driven programming
language. The external control plays an important role mainly for report programs.
Page 36 of 57
ABAP BASICS
Intelligroup Asia Private Limited
To introduce alternative conditions, you can use ELSEIF statements. If the first condition is
false, the system processes the following ELSEIF statement in the same way as the IF statement.
ELSE begins a statement block which is processed if none of the IF and ELSEIF conditions is
true. The end of the last statement block must always be concluded with ENDIF.
IF <condition1>.
<statement block>
ELSEIF <condition2>.
<statement block>
ELSEIF <condition3>.
<statement block>
ELSE.
<statement block>
ENDIF.
ABAP/4 allows unlimited nesting of IF ENDIF statement blocks, but they must terminate
within the same processing block. In other words, an IF ENDIF block cannot contain an event
keyword.
Branching with CASE statement
To execute different statement blocks depending on the contents of particular data fields, you can
either use IF statement or the CASE statement as follows:
Syntax
CASE <f>.
WHEN <f1>.
<statement block>
WHEN <f2>.
<statement block>
WHEN <f3>.
<statement block>
WHEN OTHERS.
<statement block>
ENDCASE.
The system executes the statement block after the WHEN statement if the contents of <f> equals
the contents of <fi>, and continues processing after the ENDCASE statement. The statement
block after the optional WHEN OTHERS statement is executed if the contents of <f> do not
equal any of the <fi> contents. The last statement block must be concluded with ENDCASE.
The conditional branching using CASE is a shorter and simpler form of similar processing with
IF. When you have many conditions IF becomes more complicated in such cases CASE is used.
Updated on - Apr 20, 2004
Proprietary & Confidential
Page 37 of 57
Created on - Jan 01, 2006
ABAP BASICS
Intelligroup Asia Private Limited
LOOPING
Looping with DO statement
If you want to write your name say for 10 times, you need to write WRITE statement for 10
times.
When you want to process a statement more than once, you can write this statement within a
loop with the DO statement as follows:
Syntax
DO 5 times.
Write : / name.
ENDDO.
The system continues processing the statement block for 5 times introduced by DO and
concluded by ENDDO.
The system field SY-INDEX contains the number of times the loop has been processed so in this
case when the loop is over value of sy-index will be 5.
In this case you know that, you want to perform WRITE statement for 5 times. But that is not
the case always. Many times you need to terminate the loop depending upon certain conditions.
This can be done, by using EXIT or STOP statement.
The important point to remember when you dont you use TIMES option, is to avoid endless
loops when working with the DO statement. If you do not use the TIMES option, include at least
one EXIT, STOP statement so that the system can leave the loop.
EXIT and STOP takes you out of that loop.
Looping with WHILE Statement
If you want to process a statement block more than once as long as a condition is true, you can
program a loop with the WHILE statement as follows:
Syntax
DATA: M TYPE I VALUE 0.
WHILE M < 10.
WRITE: / M.
M = M + 1.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 38 of 57
ABAP BASICS
Intelligroup Asia Private Limited
ENDWHILE.
The system continues processing the statement block introduced by WHILE and concluded by
ENDWHILE statements as long as M is less than 10 or until the system finds an EXIT, STOP.
The system field SY-INDEX contains the number of times the loop has been processed.
You can nest WHILE loops any number of times and also combine them with other loops.
Difference between DO loop and WHILE is that in WHILE, condition is checked first and if
condition is true then loop is executed while in DO loop, the loop gets executed first if you dont
have TIMES option and then the condition is checked (if you have any).
You can have nested DO and WHILE or DO and IF or IF and IF or any possible situation.
Page 39 of 57
ABAP BASICS
Intelligroup Asia Private Limited
String Operations
ABAP/4 provides several keywords for processing data objects of type C, also known as
character strings.
Shift command
To shift the contents of a field, by one position or one character you can use the SHIFT
statement. Using SHIFT allows you to shift field contents, byte-by-byte or character-bycharacter.
With the SHIFT statement, you can execute the following:
String = HELLO.
String 1 = ALL OF YOU.
String 2 = WORK HARD.
Shift string
Shift string1 by 2 places.
Shift string2 right.
Shift string1 by 2 places circular.
The output will be
ELLO By default if nothing is specified then string is shifted by one position.
L OF YOU Here the string is shifted by 2 places.
_WORK HARD In this case the string is shifted to right by one place (with leading blanks)
K HARDWOR In this case the string is shifted to the left so that 3 characters on the left appear
on the right.
Replace command
You use the REPLACE statement.
Syntax
REPLACE<strl>WITH<str2>INTO<c>[LENGTH<1>].
ABAP/4 searches the field <c> for the first occurrence of the first, <1> positions of the pattern
<str1>. If no length is specified, it searches for the pattern <str1> in its full length.
Then, the statement replaces the first occurrence of the pattern <str1> in field <c>with the string
<str2>. If a length <l> was specified, only the relevant part of the pattern is replaced.
REPLACE STR1 WITH STR2 INTO STRING.
Here whole string is searched for string1 and is replaces with str2.
REPLACE & WITH M
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 40 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Here the system searches string for & and replaces it with M.
TRANSLATE command
Syntax
TRANSLATE<c> TO UPPER CASE.
TRANSLATE<c> TO LOWER CASE.
These statements convert all lower case letters in the field <c> to upper case or vice versa.
You can use TRANSLATE to substitute the characters in a string like replace. But the main
difference between Translate and Replace is that Replace statement replaces only one occurrence
of particular character while Translate replaces all the occurrences of the character.
When using substitution rules, use the following syntax:
Syntax
TRANSLATE <c> USING <r>.
STRLEN command
To determine the length of a character string up to the last character other than SPACE, use the
built-in function STRLEN as follows:
Syntax
N = STRLEN ( STR ).
Here N is defined in DATA statement as type i.
STRLEN processes any operand <c> as a character data type, regardless of its real type. No
conversions are performed.
CONDENSE command
To delete superfluous blanks in character fields, use the CONDENSE statement:
Syntax
CONDENSE <c> [NO-GAPS].
This statement removes any leading blanks in the field <c> and replaces other sequences of
blanks by exactly one blank. The result is a left-justified sequence of words, each separated by
one blank. If the addition NO-GAPS is specified, all blanks are removed.
CONCATENATE command
To concatenate separate character strings into one, use the CONCATENATE statement:
Syntax
CONCATENATE <c1> <cn> INTO <c> [SEPARATED BY <s>].
This statement concatenates the character fields <c1> to <cn> and assigns the result to <c>.
Trailing blanks are ignored during this operation.
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 41 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 42 of 57
ABAP BASICS
Intelligroup Asia Private Limited
OPEN SQL
In the R/3 System, long-life data is stored in relational database tables. Structured Query
Language (SQL) was created for accessing relational Database. SQL has two statement types:
Data Definition Language (DDL) statements and Data Manipulation Language (DML)
statements.
TO include SQL statements in an ABAP/4 program, use Native SQL. To avoid incompatibilities
between different database tables and also to make ABAP/4 program independent of the
database system in use, SAP has created a set of separate SQL statements called Open SQL.
Open SQL contains a subset of standard SQL statements as well as some enhancements, which
are specific to SAP. Using Open SQL enables you to access any database tables available to the
SAP system regardless of the manufacturer be it Oracle, Informix etc.
The difference between Open SQL and Native SQL is as follows:
A database interface translates SAPs Open SQL statements into SQL commands specific to the
database in use. Native SQL statements access the database directly.
Open SQL keywords
Keywords Used for
SELECT: Reading Data from Database Tables
INSERT: Adding Lines to Database Tables
UPDATE: Changing Lines in Database Tables
MODIFY: Adding or Changing Lines
DELETE: Deleting Lines from Database Tables
When using Open SQL statements in an ABAP/4 program, you must ensure the following:
1) The database system being addressed must be supported by SAP.
2) The database tables being addressed must be defined in the ABAP/4 Dictionary.
Page 43 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Select statement
The following system fields play an important role in Open SQL operations:
SY-SUBRC
As with other ABAP/4 statements, the return code value in the system field SY-SUBRC
indicates after each Open SQL operation whether or not the operation was successful. If an
operation is successful, SY-SUBRC = 0. If an operation is unsuccessful SY-SUBRC <> 0
SY-DBCNT
The value in the SY-DBCNT field indicates how many lines were affected by the operation or
how many lines have already been processed.
To read data from a database table, use the SELECT command.
Syntax
SELECT<what> FROM <table name> [INTO <variable, another table>] [WHERE
<condition>].
This statement has several basic clauses. Each clause is described in the following table.
SELECT<what> FROM <table name> INTO <variables or another table> WHERE <condition>
The SELECT clause defines whether the result of the selection is a single line or a whole table,
or few columns.
FROM <table name>
The FROM clause specifies the database table or view <source> from which the data is to be
selected.
INTO <variable, another table>
The INTO clause determines the target area <target> into which the selected data is to be read. It
can also be placed before the FROM clause. If you do not specify an INTO clause, the system
uses the table work area. The table work area is a header line, which is automatically
created by the TABLES statement.
WHERE <condition>
Updated on - Apr 20, 2004
Created on - Jan 01, 2006
Page 44 of 57
ABAP BASICS
Intelligroup Asia Private Limited
The WHERE clause specifies which lines are to be read by specifying conditions for the
selection. Choosing the Lines to be Read.
For Selecting All data from table:
i.e., read all columns and all the rows from database table
Syntax
SELECT * FROM <table>.
(Here you are not specifying WHERE condition)
Selecting All Data from a Single Line
To read all columns of a single line from a database table, use the SELECT statement as
follows :
Syntax
SELECT SINGLE * FROM <table> WHERE <condition>
The result of this statement is a single line. To make sure you retrieve desired unique single
record, you must link all the fields which form the primary key of the database table by AND in
the WHERE condition.
Prerequisite for SELECT SINGLE
1.
2.
3.
Aggregate Expressions
By using aggregate expressions, you can extract characteristic data from a column <a> of the
database table.
.MAX: returns the maximum value of the column
.MIN: returns the minimum value of the column
.AVG: returns the average value of the column
.SUM: returns the sum value of the column
Updated on - Apr 20, 2004
Proprietary & Confidential
Page 45 of 57
Created on - Jan 01, 2006
ABAP BASICS
Intelligroup Asia Private Limited
ABAP BASICS
Intelligroup Asia Private Limited
complex WHERE clauses. The system must split up those into single statements for the
database system.
Do not use the logical NOT in WHERE clauses but inverted operators instead.
The logical NOT is supported by the database indexes.
Keep the transferred data small
Transfer only those columns of a database table that you really need. Avoid SELECT* if
you do not want to read all columns of a database. Use a list in the SELECT clause instead.
Use aggregate expressions in the SELECT clause to perform calculations instead transporting
great amounts of data and calculating thereafter.
Keep the number of database accesses small
Use operations on packages of data instead of operations on single data if you want to
analyze selected data more than once. To do so, transfer the data in a single operation
between tables and internal tables.
Avoid nested SELECT loops. Instead, work with internal tables and SELECT statements
using the FOR ALL ENTRIES addition.
Insert statement
INSERT statement inserts a single record into the database table.
Syntax
Tables: sflight.
Sflight-carrid = LH.
Sflight-connid = 234.
Insert sflight.
Table sflight is inserted with the record. The SY_SUBRC is returned for this statement. If the
entry already exists then the SY_SUBRC is set to non-zero value and you can do processing for
existing record by giving some error message.
Update statement
Page 47 of 57
ABAP BASICS
Intelligroup Asia Private Limited
To update database table UPDATE statement is used. This allows you to change either a single
record or several records.
You can use UPDATE when you know which record you want to change. But if you do not
know whether the primary key of the line you want to insert already exists or not, you can use
the MODIFY statement. The MODIFY statement changes existing lines and inserts lines which
do not exist.
Sflight-carrid = MN.
Sflight-connid = 454.
UPDATE SFLIGHT where CARRID = LH.
Or
TABLES SFLIGHT.
UPDATE SFLIGHT SET PRICE = 1100
WHERE CARRID = LH.
Here price of sflight will get updated with new price 1100.
Delete statement
To delete records from a database table, you use the DELETE statement.
DELETE FROM SFLIGHT WHERE CARRID = LH AND CONNID = 454.
Will delete the single record where conditions are met from SFLIGHT.
You can delete the multiple records from database table by putting all the records, which you
want to delete in internal table. For example
DELETE SFLIGHT FROM TABLE ITAB.
In this case whatever you have in internal table will be deleted from SFLIHT.
Note: append internal table with all the entries, which you want to delete.
Page 48 of 57
ABAP BASICS
Intelligroup Asia Private Limited
EXERCISES
SIMPLE WRITE STATEMENTS
1
12/12/97
FIRST PROGRAM
______________________________________________________
This list is generated
on: 12/12/1997
at: 13:40:35
by: ABAP 1
______________________________________________________
2
Page 49 of 57
ABAP BASICS
Intelligroup Asia Private Limited
Page 50 of 57
ABAP BASICS
Intelligroup Asia Private Limited
GENERAL PROBLEMS
1
.
. .
. . .
. . . .
Write a program to accept the two numbers from the user and swap the values.
c
c h
c h o
c h
c
Page 51 of 57
ABAP BASICS
Intelligroup Asia Private Limited
1
4
9
Write a program to accept a number (say 2) from user and create a multiplication table.
2x1=2
2x2=4
2 x 10 = 20
Accept a number from user and find Factorial of the same. If the number is negative then
display some message.
Accept numbers and choice EVEN or ODD from the user and display the numbers in
that range according to users choice.
Create a calculator, which performs the four basic types of calculations on two whole
numbers. The two values and the option are to be entered on the selection screen as
parameters. Output the result with 2 decimal places.
Page 52 of 57
ABAP BASICS
Intelligroup Asia Private Limited
STRING OPERATIONS
1
Accept a string & number. Write the string that many number of times.
Accept one string with delimiter ( , or ; ) and split it into two strings.
Accept a string abcdef and use shift <string>, shift <string> right, shift <string> up to
def.
Accept two strings and compare the two strings using co, ca, cs
cp (out put shall be true or false for each comparison.)
10
11
12
13
Accept a number and swap first and last digit of the same.
14
15
Page 53 of 57
ABAP BASICS
Intelligroup Asia Private Limited
DATE PROBLEMS
1
Accept a date earlier to todays date and find the difference in number of days.
Accept a date from user and display first day of the month and last day of the previous
month.
Accept a date from user and add six months to the date.
Accept a date from user and convert month part to jan, feb etc., and display this date.
Accept birth date from user and output age in years, months and days.
Page 54 of 57
ABAP BASICS
Intelligroup Asia Private Limited
4.
Page 55 of 57
ABAP BASICS
Intelligroup Asia Private Limited
SELECT STATEMENTS
1
List all ERDATs. For better readability create a column heading in the list
List all the flights for which booking date is greater than 01.06.1995.
10
11
List the maximum capacity, occupied seats and total of current bookings for each flight in
the following format.
Carrier
id
12
Max. capacity
Occupied seats
From the given from-city and to-city, list all the available on this route:
From: <from-city> To: <to-city> (say from Frankfurt to Madras)
(Tables: SPFL1)
Carrier
id
Departure Time
Start Airport
Destination Airport
Page 56 of 57
ABAP BASICS
Intelligroup Asia Private Limited
OPEN SQL
1
Accept document no. from user and display particulars of Sales document.
document no. 0010000031)
(Table: VBAP)
(Default
Created on:xxxxx
Created by: xxxxx
Time:
xxxxx
2
Accept Sales Document number from user and display corresponding material
no, description of that material and item category (Table : VBAP)
Accept material no. & item category by default PP100 and KMN respectively
Display corresponding details of sales document (Table: VBAP)
Display single record for document where date = 05.02.1998, type = AB and document
no. = 0100000006.
Page 57 of 57