You are on page 1of 15

1

2. ABAP Programming Techniques and Reports


Reports are an important tool within SAP to systematically display processed data picked up from
different database tables in a specific format requested by the business.
In SAP, there are four different types of reporting strategies available namely,
1. Classical simple
2. Classical interactive
3. ALV simple
4. ALV interactive
But before we get into the reporting concept, we need to understand the basic principles and
statements that are used to create small programs and further generate report programs.
Data Statement:
This statement is used to declare and/or to initiate different types of data containers within a
specific ABAP program.
Syntax:
Data < DC > type/like < Ref >.
[Value < int. val >].
Constant statement:
This statement is mainly used for declaring an initialising constants within a specific ABAP program.
Syntax:
Constants < C > type/like < Ref >
Value < int. value >.
Write Statement:
Unlike the above two statements which are used mainly on the declaration segment, this statement
is used anywhere in the program which is used to display results on the output list.
Syntax:
Write < entity >
Chain operator:
This operator is used for chaining of multiple lines of code under a single statement. This is a global
operator which can be used with any ABAP statements.
Write < e1 > Write: e1,
Write < e2 > e2.
New line operator:
This operator is used exclusively with the write statement to display information on different lines.

2

Co-ordinate Spacing:
Using this clause, one can print a value on the output list on a specific co-ordinate position. There is
a limit of 255 characters which can be extended as well.
X (Y) where X is the starting point and Y is the Interval. (Like rows and columns).
Color Statement:
This option can be used to display a value or a series of values shaded with specific color options.
There are total 7 colors identified by SAP with numbers 1 to 7.
A simple program in ABAP:
Go to SE38 type ZZY_first_demo_program click on create. In attributes, provide a title, type of
program executable program and save.
Constants v_num1 TYPE i Value 10.
Constants v_num2 TYPE i Value 20.
DATA v_result TYPE i.
V_result = v_num1 + v_num2.
Write The result is i.
Write v_result.
Save and click on the pretty printer, activate and execute the program. To execute, press on F8.
Another way of writing the program:
REPORT zzy_first_demo_program.
CONSTANTS: v_num1 TYPE i VALUE 10,
v_num2 TYPE i VALUE 20.

DATA v_result TYPE i.

v_result = v_num1 + v_num2.


WRITE: 10(23) 'The Result Is:' COLOR 4,
/33 v_result COLOR 6.
Internal Tables:
Internal tables are an important data container which can store multiple records picked up from
multiple data/sources which can be later on accessed to carry out different processing within the
program to generate the final set of data that can be displayed on the output list. As per the latest
SAP standards, there are 3 different types of internal tables.



3

Internal tables
Standard Internal Table Sorted Internal Table Hashed Internal Table
1. Time taken to read
record is directly
proportional to n,
where n is the number
of records in that
internal table.
Time taken = log n, n = number
of records.
Time taken = constant
2. Search technique =
Linear search. (Search
one record after the
other till the end).
Search technique = binary
search.
Search technique = hashed
collision technique. h(x).
3. No by default sorting
takes place.
By default, non-unique sort
takes place.
By default, unique sort takes
place.

Example for a sorted internal table:
F1 F2 F3
1 A A
1 B B sorted with duplicates
2 C C
2 D D
Declaration of Internal Tables:
To declare an internal table, we should have a structure and table type in place which will be used to
declare the internal table. To declare these data types which dont have any re-usable value in most
cases can be declared through the concept of type casting which is a process of creating user defined
data types by referring standard data types supported by a statement types.
TYPES < UDD > type < Ref DD >.
Declaration of structures:
Syntax:
TYPES: Begin of < str >,
F1 type < Ref 1 >,
F2 type < Ref 2 >,
.
.
.
.
Fn type < Ref n >,
4

End of < str >
Or
Types: Begin of < str >.
Include structure < AD table/AD str >.
Types: End of < str >.
AD ABAP Dictionary
Table type:
Declaration of table type:
Syntax:
Types: < tt> type standard table of < str >.
Types: < tt > type sorted table of < str >.
With non-unique key f1, f2, ..fn.
Internal table declaration:
Data: < it > type < tt >.
An internal table once generated reserves 8 KB pages of memory from which memory
allocation happens for the records to be inserted inside the body of the internal table.

The body of the internal table cannot be directly accessed for any kind of operations.
Instead, it requires the service of a specially designed work area container to carry out the
operations.

Work Area
F1 F2 F3 F4


The work area can be declared by referring the internal tables underlying structure to help
carry out manual operations within the body of the internal table.
Data: < wa > type < str >.
Processing on Internal Tables:
Append Statement:
Append statements adds a record inside the body of the Internal table either at the last available
position or at the first position if the internal tables body is empty.
Syntax:
Append < wa > to < it >.

5

Insert statement:
Insert statement adds the records from the work area to the body of the internal table at a specific
index position.
Syntax:
Insert < wa > into < it >.
Index < >.
Sequential read operation:
Sequential read operation is controlled through a command Loop End Loop which starts an
iteration where under each iteration a specific record from the body of the internal table is read and
placed on its work area. Please note processing on the record can only be possible once the record is
successfully read from the body to its work area.
Syntax:
Loop at < it > into < wa >.
[where f1 = < cond1 > and/or
F2 = < cond2 > and/or
F3 = < cond3 > and/or
.
.
.
fn = < condn > ]
.
ABAP Code..
.
End Loop.
Modify statement:
This statement will modify a specific record present within the body of the internal table.
Syntax:
Modify < it > from < wa >
[Index < n >]
[Transporting f1, f2.fn].
Transporting clause helps a record to modify only the fields which are presented in the transporting
clause.
Syntax:
6

Delete Statement:
This statement deletes a record or a group of records from the body of the internal table.
Program:
Report ZZY_DEMO_Internal_tables
TYPES: BEGIN OF ty_emp,
empno TYPE zzy_empmast-empno,
empname TYPE zzy_empmast-empname,
END OF ty_emp.

TYPES: ty_t_emp TYPE STANDARD TABLE OF ty_emp.

DATA: it_emp TYPE ty_t_emp,
wa_emp TYPE ty_emp.

wa_emp-empno = '1'.
wa_emp-empname = 'SANJAY'.
APPEND wa_emp TO it_emp.

wa_emp-empno = '2'.
wa_emp-empname = 'RAMESH'.
APPEND wa_emp TO it_emp.

wa_emp-empno = '3'.
wa_emp-empname = 'NIHAR'.
APPEND wa_emp TO it_emp.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

7

wa_emp-empno = '4'.
wa_emp-empname = 'SURAJ'.
INSERT wa_emp INTO it_emp INDEX 2.

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '1'.
wa_emp-empname = 'SURESH'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '2'.
wa_emp-empname = 'VENKY'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '3'.
wa_emp-empname = 'RAJU'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '4'.
wa_emp-empname = 'PRAVEEN'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

8

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

DELETE it_emp WHERE empno = '4'.

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.
Types: begin of ty_emp,
Same program with additional characteristics:
REPORT zzy_demo_internal_tables.

TYPES: BEGIN OF ty_emp,
empno TYPE zzy_empmast-empno,
empname TYPE zzy_empmast-empname,
END OF ty_emp.

TYPES: ty_t_emp TYPE STANDARD TABLE OF ty_emp.

DATA: it_emp TYPE ty_t_emp,
wa_emp TYPE ty_emp.

wa_emp-empno = '1'.
wa_emp-empname = 'SANJAY'.
APPEND wa_emp TO it_emp.

wa_emp-empno = '2'.
wa_emp-empname = 'RAMESH'.
APPEND wa_emp TO it_emp.

wa_emp-empno = '3'.
9

wa_emp-empname = 'NIHAR'.
APPEND wa_emp TO it_emp.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

wa_emp-empno = '4'.
wa_emp-empname = 'SURAJ'.
INSERT wa_emp INTO it_emp INDEX 2.

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '1'.
wa_emp-empname = 'SURESH'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '2'.
wa_emp-empname = 'VENKY'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '3'.
wa_emp-empname = 'RAJU'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

LOOP AT it_emp INTO wa_emp WHERE empno = '4'.
wa_emp-empname = 'PRAVEEN'.
MODIFY it_emp FROM wa_emp TRANSPORTING empname.
ENDLOOP.

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.

DELETE it_emp WHERE empno = '4'.

10

SKIP 2.

LOOP AT it_emp INTO wa_emp.
WRITE: /5(5) wa_emp-empno,
13 wa_emp-empname.
ENDLOOP.
Select Query Option:
Select is an important query statement using which a set of records can be fetched from a database
table and directly insert it into a target container. If the container is an internal table, system does
not require the services of a work area to insert records fetched using a select statement.
In SAP ABAP, there are four different types of select query available namely
1. Select into table
2. Select single
3. Select up to n rows
4. Select inner join

1. Select into table:

Using this type of select query, a bunch of records can be picked up from a database table
and directly insert it into the body of an internal table.

Syntax:

Select * / f1f2.fn
From < DB > into table < it >

[where f1 = < cond1 > and/or
f2 = < cond2 > and/or
.
.
.
fn = < condn > ]
Criteria for designing an efficient select:
The order of the fields in the database table, the order of the fields in the internal table, the
order of the fields in the select field list should all be identical.
The order of the fields in the where clause of the select ideally should be identical to any one
of the indexes of the table.
Never use select * option until and unless it is explicitly required. Using a select * option
increases the data fetch time of the select statement.
Never use a negation operator in the where clause of a select to negate records at the
database level.
Program to fetch the data for an internal table in a program from a database table:
Go to SE38 and create the program ZZY_select_table_demo.
11

REPORT zzy_select_table_demo.

**Structure Declarations
*TYPES: BEGIN OF ty_emp,
* empno TYPE zzy_empmast-empno,
* empname TYPE zzy_empmast-empname,
* END OF ty_emp.
**Table Type Declarations
*TYPES: ty_t_emp TYPE STANDARD TABLE OF ty_emp.
*
**Internal Table and Work Area Declarations
*DATA: it_emp TYPE ty_t_emp,
* wa_emp TYPE ty_emp.

INCLUDE zzy_select_table_demo_top.

SELECT empno "Employee Number
empname "Employee Name
FROM zzy_empmast INTO TABLE it_emp.
* WHERE empno = '00001'. CTRL + < (Commenting a whole line); CTRL + > (Un Commenting a
whole line)

WRITE: /2(5) 'EMPNO' COLOR 6,
10 'EMPNAME' COLOR 6.
SKIP 1.
LOOP AT it_emp INTO wa_emp.
WRITE: /2(5) wa_emp-empno,
10 wa_emp-empname.
ENDLOOP.
Types of ABAP Programs:
1. Executable Program:

These are the types of programs which can be independently executed either via F8 option
or via a transaction code if the program contains selection screens.

2. Include Program:

This type of program cannot be executed independently. Instead, they are inserted inside
another main program to see the effect of its code. It is one of the important modularization
techniques in ABAP.

Modularization technique can be used in various programs

Syntax:

Include < program name >.
12


Program:

Go to SE38 and create a program ZZY_select_table_demo_top.

TYPES: BEGIN OF ty_emp,
empno TYPE zzy_empmast-empno,
empname TYPE zzy_empmast-empname,
END OF ty_emp.
*Table Type Declarations
TYPES: ty_t_emp TYPE STANDARD TABLE OF ty_emp.

*Internal Table and Work Area Declarations
DATA: it_emp TYPE ty_t_emp,
wa_emp TYPE ty_emp.

3. Function Group:

Function group is a special type of program which acts as a container for functional modules
and screens. Since they are program containers, a function group cannot be executed.

4. Module Pool Program:
This is a special type of program used for building custom SAP applications involving several
screens and flow logics. This type of program can only be executed through a transaction
code.
5. Sub-routine Pool:
This is similar to include program which contains a pool of sub-routine definitions and quote
can only be written inside the sub-routine definitions.
ABAP Events:
Events are pre-defined conditions which when met executes the series of code written under it.
Most of the ABAP events have a static order and a couple of exceptional events are ordered based
on run time conditions.
Following are the events which are majorly used in the classical simple report.
1. Load-of-program:
This is regarded as a systems event which gets called at the very beginning when the program is
executed under this event, the high level code gets converted into machine level code and then
the same is loaded on the systems memory.
2. Initialization event:
Post load of program event, the next event that triggers if found in the program is the
initialization event. Under this event, we tend to include a series of code which has to be
executed at the very beginning before the actual processing status.

13

3. Start-of-selection:
This event is triggered post all the selection screen event executions which ideally should contain
the central processing of the program. This is also regarded as ABAP default event as a program
not containing any event will bring all its code except the data declaration part under this event.
4. End-of-selection:
This is the last event which triggers after all the event execution and typically under this event,
the output portion of the program is coded.
Classical report conditional events:
1. Top-of-page:
This is one of the conditional events which is used to design headers on classical reports. This
event is triggered for a specific page whenever system encounters for the first time either a
write or skip or ULine statement.
2. End-of-page:
This event is used to print footer on a specific report page. To make this event work, we need to
provide an information to the system as to how many number of lines will be reserved per page
using the command line-count x(y) at the very beginning in the report statement.
Line-count x(y):
Report Page


X


Once the system finishes printing, the (x-y)
th
line, on a specific page, this event is triggered to
generate the footer of the page.
Program:
Go to SE38 and create a program ZZY_sales_item_report.

System Variables:
There are a series of 180 to 200 variables which can be directly used by the developer for carrying
out certain functionalities. Please note, to use these variables the developer does not require a
declaration and initialization as the same would be done by the system based on the context of its
usage. All these system variables can be found as fields to a standard SAP ABAP structure known as
SYST.
A system variable can be accessed in a program using the below notation.
Syst- < variable >.


x-y


Y
14

Or
Sy- < variable >.
Below are the list of system variables which are quite frequently used in an ABAP program:
S. No System Variable
Name
Description
1 Sy_mandt It returns the current logon client number.
2 Sy_uname It returns the current user name using which user has logged on to
SAP.
3 Sy_datum
(YYYYMMDD)
It returns the current server date.
4 Sy_uzeit
(HHMMSS)
This returns the current server time.
5 Sy_repid/sy_cprog It returns the current main program name.
6 Sy_index It returns the iteration counter value when used inside an iterative
statement. Please note this command is incompatible with Loop-End
Loop statement.
7 Sy_tabix This returns the current index number of the record which is
currently read from the internal table.
8 Sy_dbcnt
(database count)
This returns the count of the number of records returned by a
database table into an ABAP program when queried using an SQL
query.
9 Sy_pagno It returns the current list page number.
10 Sy_tfill This returns the number of records present inside the body of the
internal table provided a statement to count the number of records is
used.
11 Sy_batch Its value would be blank if a program is executed in the fore-ground,
else it would be X if the program is executed in background.
12 Sy_langu It returns the current logon language.
13 Sy_dynnr This returns the current screen number.
14 Sy_tcode It returns the current transaction code.
15 Sy_datlo, Sy_timlo Local date for the current user and the local time for the current user.
16 Sy_subrc This system variable returns 0 if its immediate preceding operation is
completely successful, but returns a non-zero value (2, 4, 8) if the
immediately preceding operation is not completely successful.









15

You might also like