You are on page 1of 113

Advanced Techniques in ABAP Programming

Course Objectives

In this Course, the participant will learn:


• ABAP Workbench Environment and Tools
• An Introduction to ABAP Programming
• Advanced techniques
TechniquesininABAP
ABAPProgramming
Programming
• BDCs and Interfacing in the SAP Environment
• Enhancements & Modifications to SAP R/3
• Designing and Using SAP Forms (SAP Script)
• Dialog Programming
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call
Call Subroutines
Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
In this section, we will…

• Describe Subroutines
• Define and Call Subroutines
• Explain how subroutines are used
• Define Parameters
• Define Global and Local Data
• Define Local Field Values
Subroutines: What and Why?

PROGRAM RSDEM001. PROGRAM RSDEM004.

Calculate tax Call calculate_tax

Call calculate_tax

Calculate tax Subroutine


Calculate_tax
Defining and Calling Subroutines
Syntax:
• Defining a Subroutine:
FORM <form_name> [tables <tab1> <tab2>…]
[using <var1> <var2> value(<var3>)…]
[changing <cvar1>…].
ENDFORM.

• Calling a Subroutine:
PERFORM <form_name> [tables <tab1> <tab2>…]
[using <var1> value(<var3>)…]
[changing <cvar1>…].
Example:
PERFORM CALCULATE_TAX USING FL1.

FORM CALCULATE_TAX USING F1.


CLEAR ITAB.
MOVE: F1 TO ITAB-FL1.
APPEND ITAB.
ENDFORM.
Using Parameters to Pass Data

Pass By Reference
USING Pass By Value

TABLES Pass By Reference

CHANGING Pass By Value & Result


Parameters Example

Actual Parameters

PERFORM CALCULATE_TAX USING FL1 FL2.

FORM CALCULATE_TAX USING F1 F2.


CLEAR ITAB.
MOVE F1 TO ITAB-FL1.
APPEND ITAB.
ENDFORM. Formal Parameters

In this example, parameters are passed by


reference. This is the most common, and most
cost effective, method of parameter passing.
Passing Parameters
a1 a2 a3 a4
PROGRAM <name>. X Y
TABLES: ....
DATA: ....
.
. 1 1 2 2
.
PERFORM <name> USING
<a1> <a2>
<a3> <a4>.
.
.
. X Y
FORM <name> USING
VALUE(<f1>) f1 f2
VALUE(<f2>)
<f3>
<statements> <f4>.
1 Pass by value

ENDFORM. 2 Pass by reference


Internal Tables as Passed
Parameters

REPORT B170D094
DATA: BEGIN OF TAB,
F1 LIKE TABNA-COUNTRY,
F2 LIKE TABNA-NAME1,
END OF TAB.
DATA: X.
PERFORM SUB1 TABLES TAB
USING X.

FORM SUB1 TABLES T USING Z.


LOOP AT T.
WRITE: / T.
ENDLOOP.
MOVE . . . TO T.
APPEND T.
ENDFORM.
Global and Local Data
REPORT B170D091
TABLES: . . .
DATA: . . .
Global Data
X1 Y1
Call Subroutine X1 Y1
Subroutines
Passed Parameters X Y
DATA: … local data
statements
Passes Parameters
Data: … local data X1
statements
Persistent Local Variables:
STATICS

REPORT B170D092.
DATA: RESULT TYPE I.

PERFORM RANDOM_NUMBER CHANGING RESULT.

FORM RANDOM_NUMBER CHANGING P_RESULT TYPE I.


STATICS: L_STATE TYPE I.

L_STATE = ( L_STATE * 113 + 34 ) MOD 256.


P_RESULT = L_STATE.
ENDFORM.
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
In this section, we will…

• Define Function Modules


• Define Function Groups
• Describe Interfaces
• Explain Exception Processing
• Define Global Data / Local Memory
• Explain Remote Function Call (RFC)
• Search the Function Library
Introduction to Function Modules

¾ High Reusability
STRING_CONCENATE/ READ_CALENDAR/
CURRENCY_CONVERSION/
¾ Clear import and ACCOUNT_CHECK/ ...
¾ export parameters

¾ Search
¾ Create
Application ¾ Change
¾ Document

¾ Test ¾ Exception
Environment handling

¾ Remote Function Call


Function Groups
Tools

ABAP Workbench

Development

Function Builder
Calling a Function Module

Function Module How to call a


Maintenance Function Module

FM_02 CALL FUNCTION


Interface 'FM_02'
Import EXPORTING ...
Export IMPORTING ...
Tables TABLES ...
Exceptions (EXCEPTIONS ...)
Program
Documentation
Administration
Interfaces

IMPORT

EXPORT

TABLES
Exception Processing
Table Parameters/Exceptions STRING_SPLIT

Exceptions
NO_DATA
CALL FUNCTION ‘STRING_SPLIT’
DATA_NOT_VALID EXPORTING DELIMITER = ‘-’
STRING_TOO_LONG STRING = TEXT
... IMPORTING HEAD = HEAD
TAIL = TAIL
EXCEPTIONS NO_DATA = 01
STRING_TOO_LONG = 02
OTHERS = 03.
FUNCTION STRING_SPLIT. CASE SY-SUBRC.
... WHEN 1. .... .
IF ... WHEN 2. .... .
RAISE NO_DATA. WHEN 3. .... .
ENDIF ENDCASE.
An Example of a Function Module

Import
Parameters

Export
Parameters
Global Data / Local Memory
Global Data

L<gr> TOP
FUNCTION-POOL <gr>.
DATA: X.
TABLES: ... .

Function module Subroutines

L<gr> U01 L<gr> F01


FUNCTION ... . FORM SUB1 USING ... .
DATA:... . DATA:... .
MOVE X TO ... . MOVE ... TO X.
ENDFUNCTION. ENDFORM.
Remote Function Call

R/3 R/2
Presentation System
server
Mainframe

RFC with
C-interface RFC RFC RFC

AP AP
R/3 R/3
System System
RFC

DB DB
System
Searching the Function Builder

Tools

ABAP Workbench

Development

Function Builder ABAP Repository Information System

Standard selections
Function module
Short description
Function group
Development class
Creating a Function Module
ABAP Function Builder: Initial Screen
Tools

ABAP Workbench Function module Z_FUNC_DEMO1

Development
Create

SE37
Function Builder Administration
Import/Export
Import/Export Parameters
Parameters
Table Parameters/Exceptions
Documentation
Source code
Creating a Function Module – Step 1 of 7

Initial Parameters
Creating a Function Module – Step 2 of 7

General Attributes
Creating a Function Module – Step 3 of 7

Import and Changing Interface Parameters


Creating a Function Module – Step 4 of 7

Export Interface Parameters


Creating a Function Module – Step 5 of 7

Tables Interface Parameters


Creating a Function Module – Step 6 of 7

Exceptions
Creating a Function Module – Step 7 of 7

Source Code and Activation


Example of Calling a Function
Module in a Program
Function ‘ZCONVERT_TEMPERATURE
* Local interface:
* IMPORTING VALUE (TEMP_UNIT) TYPE C
* VALUE (DEGREES) TYPE P
* EXPORTING REFERENCE (CONV_DEGREES) TYPE P
IF TEMP_UNIT = ‘F’.
CONV_DEGREES = ( DEGREES - 32 ) * 5 / 9.
ELSE.
CONV_DEGREES = DEGREES * 9 / 5 + 32.
ENDIF.
ENDFUNCTION.

This function can be called as follows:


DATA DEG TYPE P.
CALL FUNCTION ‘ZCONVERT_TEMPERATURE’
EXPORTING TEMP_UNIT = ‘F’
DEGREES = 100
IMPORTING CONV_DEGREES = DEG.
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
Selection Criteria
• Selection Criteria for
for ABAP
ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
In this section, we will…

• Describe Selection Screens


• Define Parameters
• Define Select Options
• Customize the selection screen
• Describe Selecting from Tables using Logical
Databases
• Define the Check and Reject Statement
Selection Screen

SPFLI Carrier ID to

Flight Selection
Flight Type
Search string
General selections
Client ID to

Date of flights to

REPORT <name>.
TABLES: SPFLI, SFLIGHT.
PARAMETERS: FLTYPE
GET SPFLI.
CHECK CARRID.
Declaring Fields with PARAMETERS
Syntax:
PARAMETERS <name>[(<length>)] [TYPE <data type>]
[DEFAULT<default>] [AS CHECKBOX].

Example:
REPORT ZTESTRPT1.
TABLES: SFLIGHT.
PARAMETERS: p_CARRID LIKE SFLIGHT-CARRID,
p_CONNID LIKE SFLIGHT-CONNID,
p_FLDATE LIKE SFLIGHT-FLDATE
DEFAULT SY-DATUM,
p_RESV_FLG AS CHECKBOX.
SELECT *
FROM SFLIGHT
WHERE CARRID = p_CARRID
AND CONNID = p_CONNID
AND FLDATE = p_FLDATE

ENDSELECT.
The SELECT-OPTIONS Statement
Syntax: SELECT-OPTIONS <selection name> FOR <field>.
REPORT <name>.
TABLES: SPFLI, LFA1.
SELECT-OPTIONS:
CONNECTID FOR SPFLI-CONNID
DEFAULT '1984' TO ' 3517 '.

POSTCODE
Internal SIGN OPTION LOW HIGH
table I BT 1984 3517

Program selections

CONNECT ID 1984 3517


Value Sets

Connection id

from to Incl. Excl.


0010 0400

1900 3600
ABAP
ABAP
3799
Connection id

SIGN OPTION LOW HIGH


Selection Texts

REPORT <name>.
TABLES: SPFLI, AIRPROM.
SELECT-OPTIONS:
FLTIME FOR SPFLI-FLTIME
DEFAULT '06:01:00' TO 10:00:00',
AIRPFROM FOR SPFLI-AIRPFROM.
.
.

Text Elements

Text elements/selection texts .


.
<field name> <text> .
Flight Time 06:01 10:00
FLTIME Flight time
Airport From
AIRPFROM Airport from
Customizing the Selection Screen
Syntax:
• Selection Screen Block
SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN END OF LINE.

• Selection Screen Formatting Statements


SELECTION-SCREEN SKIP <n>.
SELECTION-SCREEN POSITION <pos>.
SELECTION-SCREEN ULINE.
SELECTION-SCREEN COMMENT <g> TEXT-<xxx>.
Customizing the Selection Screen:
Example

REPORT <name>. Text-001


Text-001
Please
Please enter
enter carrier
carrier ID
ID
TABLES: SPFLI, SFLIGHT, SBOOK.

SELECTION-SCREEN BEGIN OF LINE. col.10


col.10 col.50
col.50
SELECTION-SCREEN COMMENT 10 TEXT-001.
SELECTION-SCREEN POSITION 50.
PARAMETERS: CARRID LIKE SPFLI-CARRID
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN ULINE /10(50).

.
.

Please enter carrier ID


CALL SELECTION-SCREEN

FEATURES
• Additional selection screen during processing.
• Selection screen within another selection screen
• Submit a report using selection screen.

APPLICATIONS
Reduces number of fields on the standard selection screen.
Starting reports with different selection screens.
Pop-up window.
Initial screen of a regular screen painter transaction.
CALL SELECTION-SCREEN
Example

Report…
Report…
...
...
SELECTION-SCREEN: BEGIN
SELECTION-SCREEN: BEGIN OF
OF BLOCK
BLOCK b1
b1 WITH
WITH FRAME
FRAME TITLE
TITLE <var1>.
<var1>.
SELECT-OPTIONS: carrier
SELECT-OPTIONS: carrier for
for sflight-carrid.
sflight-carrid.
SELECTION-SCREEN: END
SELECTION-SCREEN: END OF
OF BLOCK
BLOCK b1.
b1.

SELECTION-SCREEN: BEGIN
SELECTION-SCREEN: BEGIN OFOF SCREEN
SCREEN 2000
2000 ASAS WINDOW,
WINDOW,
INCLUDE BLOCKS
INCLUDE BLOCKS b1,
b1,
BEGIN OF
BEGIN OF BLOCK
BLOCK b2
b2 WITH
WITH FRAME
FRAME TITLE
TITLE a1.
a1.
SELECT-OPTIONS:
SELECT-OPTIONS: scust FOR
scust FOR sbook-customid.
sbook-customid.
SELECTION-SCREEN:
SELECTION-SCREEN: END OF
END OF BLOCK
BLOCK b2,
b2,
END OF
END OF SCREEN
SCREEN 2000.
2000.
...
...
AT LINE-SELECTION.
AT LINE-SELECTION.
CALL SELECTION-SCREEN
CALL SELECTION-SCREEN 2000
2000 STARTING
STARTING ATAT 55 5,
5,
The CHECK Statement

CHECK <selection name>. .


.
.
CHECK <logical expression>. Carrier id 7100 7500

Price
REPORT <name>.
TABLES: SPFLI, SFLIGHT.
SELECT-OPTIONS:
CARRIERID FOR SPFLI-CARRID
SPFLI
PARAMETERS: PRICE LIKE SFLIGHT-
PRICE.
Work area SPFLI
GET SPFLI.
D 71000
CHECK SPFLI-CARRIER NE 'BA'.
LAND1 PSTLZ ORT01
GET SFLIGHT. Internal Table CARRIERID
CHECK: CARRIERID I BT 7100 7500
.
.
.
GET SBOOK .
The REJECT Statement

REJECT <name>.

REPORT ZZ70D132.
TABLES: SPFLI, SBOOK. k
SPFLI s bac SPFLI
. e
G o PFL
I
. S
. to
GET SBOOK.
IF ... .
SFLIGHT SFLIGHT
REJECT ’SPFLI'.
ENDIF.
.
.
. SBOOK
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting
Reporting using
UsingABAP
ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
In this section, we will…

• Explain variations of the WRITE statement


• Explain printing Icons, Lines and Symbols
• Explain other formatting options
• Describe the different events in ABAP Reports
• GUI Status and Title for reports
• Interactive (Drilldown) Reporting and its
advantages
• Define Open SQL Statements
• Explain File Handling in ABAP
WRITE Statement
• Syntax:
WRITE [AT] [/P(L)] v1[+O(SL)]
[UNDER v2 | NO-GAP]
[USING EDIT MASK m | USING NO EDIT MASK]
[mm/dd/yy | dd/mm/yy | mm/dd/yyyy |
dd/mm/yyyy | mmddyy | ddmmyy | yymmdd]
[NO-ZERO]
[NO-SIGN]
[DECIMALS n]
[ROUND n]
[LEFT-JUSTIFIED | CENTERED | RIGHT-JUSTIFIED]
[COLOR col]
WRITE Statement – Printing Symbols

• Symbols are two-color pictures like Square,


Circle, Folder, Document etc…
• Most symbols take one character space in the
output list
• Include the following statement at the top of the
program:
INCLUDE <symbol>.
WRITE Statement – Printing Icons & Lines

• Icons are similar to Symbols, but they are multi-


colored
• Most icons occupy two character spaces in the
output list
• Include the following statement in the program:
INCLUDE <icon>.
• Horizontal and Vertical Lines can also be printed
• Include the following statement for printing lines:
INCLUDE <list>.
WRITE Statement – Printing Icons & Lines

• Icons are similar to Symbols, but they are multi-


colored
• Most icons occupy two character spaces in the
output list
• Include the following statement in the program:
INCLUDE <icon>.
• Horizontal and Vertical Lines can also be printed
• Include the following statement for printing lines:
INCLUDE <list>.
Formatting Options – Using Colors

• Format Statement
– Syntax:
FORMAT <option1> [ON|OFF] <option2> [ON|OFF]….
• Colors can be printed in List using:
– FORMAT COLOR <color_code> [ON|OFF]
[INTENSIFIED [ON|OFF] ] [INVERSE [ON|OFF] ].
– WRITE <field> COLOR <color_code> [ON|OFF].
• Generally, Background colors are changed by
Color Codes, but foreground can be changed by
setting parameter INVERSE ON.
Other Formatting Options

• Uline
• New-line
• New-page
• Skip
• Back
• Position
• Set blank lines
Events in ABAP Runtime Environment
Events in ABAP Report Programming

List Processor Events


Report Processor Events Secondary List events
• INITIALIZATION • AT LINE SELECTION
• AT SELECTION SCREEN • AT PFn (obsolete)
• START-OF-SELECTION • AT USER-COMMAND
• GET
• END-OF-SELECTION
Basic List events
• TOP-OF-PAGE
• END-OF-PAGE
Interactive Reporting - Overview

Secondary lists Windows

Interactive Reporting

Calling a Calling a report


transaction
Events in Interactive Reporting
Report RSDEMO00
*Basic list
START-OF-SELECTION.

GET…

END-OF-SELECTION

TOP-OF-PAGE/END-OF-PAGE

*Interactive Reporting
AT PFnn.

AT LINE –SELECTION.

AT USER-COMMAND.

TOP-OF-PAGE DURING LINE-SELECTION


Page Headings

Text elements

Basic list

Report RSBBB06C
.
.
.
TOP-OF-PAGE DURING LINE-SELECTION.
Choose

TOP-OF-PAGE

Secondary list
Secondary Lists and the System Field SY-LSIND

REPORT RSBBB06A
GET SFLIGHT
Basic list
SY-LSIND:0
.
.
. Choose

AT LINE-SELECTION.
CASE SY-LSIND.
1.Secondary list
WHEN ‘1’ SY-LSIND:1
.
. Choose
.

WHEN ‘2’.
. 2.Secondary list 3.Secondary
. list
.
WHEN ‘3’. SY-LSIND:2
SY-LSIND = Choose
SY-LSIND = 1.
.
.
Line Selection I: System Field SY-LISEL

Carrid Connid Flight date

AA 0169 12121994
AA 0192 26081994
AT LINE-SELECTION.
LH 0108 25011995
MOVE: SY-LISEL(3) TO SFLIGHT-CARRID,
SY-LISEL+6(4) TO SFLIGHT-CONNID,
Choose SY-LISEL+14(8) TO SFLIGHT-FLDATE.
SELECT * FROM SBOOK
WHERE CARRID = SFLIGHT-CARRID
AND CONNID = SFLIGHT-CONNID
AND FLDATE = SFLIGHT-FLDATE.

LH 0108 25011995

SY-LISEL
WRITE : SBOOK-PRICE,……….
Line Selection II

HIDE <Field>.

HIDE area
Carrid Connid Flight date
“SFLIGHT-CARRID” AA
AA 0169 12121994
“SFLIGHT-CONNID” 0169
LH 0454 17111994 “SFLIGHT-FLDATE 12121994
“SFLIGHT-CARRID” LH
“SFLIGHT-CONNID” 0454
“SFLIGHT-FLDATE” 17111994
Choose

REPORT RSBBB06C
.
.
GET SFLIGHT.
WRITE : SFLIGHT-CARRID, 18 SFLIGHT-CONNID,….
HIDE: SFLIGHT-CARRID,SFLIGHT-CONNID,SFLIGHT-FLDATE.
Line Selection III

HIDE <Field>.

HIDE area
Carrid Connid Flight date

“SFLIGHT-CARRID” AA
AA 0169 12121994
“SFLIGHT-CONNID” 0169
LH 0454 17111995
“SFLIGHT-FLDATE 12121994
“SFLIGHT-CARRID” LH
“SFLIGHT-CONNID” 0454
“SFLIGHT-FLDATE” 17111994

Choose

LH 0454 17111994 LH 0454 17111994


SFLIGHT-
SY-LISEL SFLIGHT CONNID SFLIGHT-
FLDATE
-CARRID
?
SFLIGHT-PRICE
REPORT RSBBB06C

AT LINE-SELECTION.
SELECT * FROM SBOOK WHERE…..

WRITE : SBOOK-CARRID ,SBOOK-CONNID,…..


Valid Line Selection
Header
Hide area

“SFLIGHT-CARRID” AA
“SFLIGHT-CARRID” LH

Footer

REPORT RSBBB06D.
GET SFLIGHT.
WRITE: SFLIGHT-CARRID,
HIDE: SFLIGHT-CARRID,…..
.
.
.
END-OF-SELECTION.
CLEAR SFLIGHT-CARRID.
AT LINE-SELECTION.
CHECK NOT SFLIGHT-CARRID IS INITIAL.
.
.
.
CLEAR SFLIGHT-CARRID.
System Fields
SY-STARO

SY-CUCOL
SY-CPAGE

SY-CUROW

SY-STACO

SY-LSIND =2
SY-LSIND =1
SY-LSIND=0
GUI Status and Title bar for reports

• Use the “SET PF-STATUS” statement to choose a


GUI Status dynamically for a report
– Syntax: SET PF-STATUS <pfstat>.
• The button the user presses is trapped in the
system variable SY-UCOMM and handled in the
event AT USER-COMMAND
• Use “SET TITLEBAR” statement to set a program
window title text dynamically
– Syntax: SET TITLEBAR <title>.
Open SQL
Open SQL – SELECT Statement
• Syntax:
SELECT [SINGLE] [DISTINCT] <result>
INTO <target> FROM <source>
[WHERE <condition>] [GROUP BY <fields>]
[HAVING <cond>] [ORDER BY <fields>].

• INTO clause:
... INTO [CORRESPONDING FIELDS OF] <wa>
| INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab>
| INTO (<f1>, <f 2>,...)

• FROM clause:
... FROM [<tab> [INNER]|LEFT [OUTER] JOIN] <dbtab> [AS <alias>]
[ON <cond>]
[CLIENT SPECIFIED]
[BYPASSING BUFFER]
[UP TO <n> ROWS]
Open SQL – Joins in SELECT

• Inner Joins
– Allow access to multiple tables with a single
SELECT statement
– Creates a temporary table based on condition in
the ON clause
– Multiple tables joined based on key fields specified
• Left Outer Joins
– Allow access to multiple tables
– Creates a temporary table
– BUT, field values in driving (left-hand) table that
DO NOT correspond to values in the right-hand
table are added to the temporary result table
Open SQL – INSERT Statement

• Inserts a row or several rows into a database


table
• Syntax:
INSERT <dbtab> FROM <wa>.

INSERT <dbtab> FROM TABLE <itab> [ACCEPTING DUPLICATE


KEYS].
• Possible to insert one or more rows from an
internal table
• Duplicate entries could be discarded by using the
clause ‘ACCEPTING DUPLICATE KEYS’
Open SQL – UPDATE Statement

• Updates a row or several rows in a database


table
• Syntax:
UPDATE <dbtab> SET <si> = <f>
[WHERE <cond>].

UPDATE <dbtab> FROM <wa>.

UPDATE <dbtab> FROM TABLE <itab>.


Open SQL – DELETE Statement

• Deletes one or more rows in a database table


• Syntax:
DELETE FROM <dbtab> WHERE <cond>.
DELETE <dbtab> FROM <wa>.
DELETE <dbtab> FROM TABLE <itab>.
File Handling – OPEN DATASET
• Opens a file for reading, writing or for appending
• Syntax:
OPEN DATASET <dsn> [FOR INPUT|OUTPUT|APPENDING]

[IN BINARY|TEXT MODE]

[AT POSITION <pos>]

[MESSAGE <mess>]

[FILTER <filt>].
• <dsn> can be a logical or physical file names
File Handling – READ DATASET

• Reads a file
• Syntax:
READ DATASET <dsn> INTO <f> [LENGTH <len>].

• <dsn> can be a logical or physical file names


• Logical file names can be created in customizing
File Handling – TRANSFER DATASET

• Syntax:

– Writing to a dataset
TRANSFER <f> TO <dsn> [LENGTH <len>].
– Closing a dataset
CLOSE DATASET <dsn>.
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
• Background Processing of Programs
In this section, we will…

• Explain the concept of ALV Reports


• The Data Declaration for ALV Reports
• Database Selection, Event Handling and Form
Handling in ALV reports
• Different Function Modules for displaying ALV
reports
The Concept of ALV Reports
The Data Declaration

Type
TypePool
PoolSLIS
SLISfor
for
ALV Reporting
ALV Reporting

ALV
ALVTables
Tablestotopass
pass
events
events and triggersto
and triggers to
ALV Function Modules
ALV Function Modules
Saving the VARIANT
Database selection, Events and Forms
Calling Function
REUSE_ALV_GRID_DISPLAY
Dynamic calling of form top_of_page
Call Function
REUSE_ALV_COMMENTARY_WRITE
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Interactive Reporting using ABAP
• ALV Reporting
Logical Databases
• Logical Databases and
and usage
usage
• Background Processing of Programs
In this section, we will…
• Explain Logical Databases
• Describe Report Driven Reading of Vendor
Master Data
• Describe Reading Data
• Compare Logical Databases v. Select
• Define the structure of a Logical Database
• Explain Event Keywords
• Describe the interaction of Logical Databases
and Reports
What are Logical Databases?
• Database within a database, but logically stored within existing
(physical) tables.

Physical Database

Logical Logical Logical


Database Database Database
Structure of a Logical Database

Logical Databases: Display Structure of KDF

LFA1

LFAS

LFBK Vendor master data


LFA1
LFB1

LFB5
LFAS LFBK LFB1 Documents
LFC1

LFC3 LFC3 BSIK


LFB5 LFC1

BSIK BKPF

BKPF
BSEG
BSEG
GSEG
GSEG
Reading Data

ABAP Dictionary
1 2
Logical Database Without Logical
Database
REPORT ... .
TABLES:SPFLI,SFLIGHT. REPORT ... .
GET SPFLI. TABLES: SPFLI,SFLIGHT.
SELECT SPFLI.
Processing SELECT SFLIGHT.
GET SFLIGHT.
.... Processing
Processing ....
ENDSELECT.
ENDSELECT.
Event Key Words: Overview

START-OF-SELECTION Introduces any initial processing to be done


prior to next event keyword (usually GET).

END-OF-SELECTION Introduces any statements that are to be


processed after all logical database records
have been read and processed.

GET <dbtab> Retrieves the table <dbtab> for


processing via the logical database
program SAPD<db><a>.

GET <dbtab> LATE Introduces statements that are only processed


once subordinate tables have been read and
processed. These statements are processed
before the next loop is performed.
Interaction of Logical Databases
and Report

Read program KDF


Logical
LFA1 REPORT SAPDBKDF... REPORT RSDEMO00.
database KDF
FORM PUT LFA1 .
SELECT * FROM LFA1... . TABLES: LFA1,
LFBK LFB1 PUT LFA1 . LFBK,
ENDSELECT . LFB5 .
ENDFORM . GET LFA1.
LFB5 LFC1 LFC3 BSIK
FORM PUT_LFBK .
SELECT * FROM LFBK... .
PUT LFBK. GET LFBK.
ENDSELECT.
ENDFORM.
FORM PUT_LFB1.
SELECT * FROM LFB1... .
PUT LFB1.
ENDSELECT.
ENDFORM.
FORM PUT_LFB5.
SELECT * FROM LFB5... .
PUT LFB5. GET LFB5.
ENDSELECT.
ENDFORM.
Overview: Events in the Context of
Logical Databases

START-OF-SELECTION

1 GET LFA1
1 LFA1 5 1 LFA1 5
2 GET LFB1
2 LFB1 4 2 LFB1 4
3 GET LFC1

4 GET LFB1 LATE


LFC1 LFC1 LFC1 LFC1 LFC1
3 3 3 3 3 5 GET LFA1 LATE

END-OF-SELECTION
Report-Driven Reading of Vendor
Master Data

REPORT ZZ70D121.
TABLES: LFA1, LFB1, LFC3.
SELECT * FROM LFA1
WHERE LIFNR ...
Processing LFA1

SELECT * FROM LFB1


WHERE LIFNR = LFA1-LIFNR.
Processing LFB1

SELECT * FROM LFC3


WHERE LIFNR = LFA1-LIFNR
AND BUKRS = LFB1-BUKRS.
Processing LFC3

ENDSELECT.
ENDSELECT.
ENDSELECT.
The STOP Statement

REPORT ZZ70D124.
TABLES: LFA1.
DATA: COUNTER TYPE I.

GET LFA1.
WRITE: ‘GET LFA1’, 15 LFA1-LIFNR,
LFA1-NAME.
COUNTER = COUNTER + 1.
SKIP.
IF COUNTER > 1.
STOP.
ENDIF.

END-OF-SELECTION.
WRITE: / ‘END-OF-SELECTION’,
TEXT-001.
* TEXT-001: ‘That’s it buddy !’.
Logical Database vs. Select
GET SELECT
• The functional connection • Faster than logical
already exists databases
• Beginners can find the • More powerful than logical
database they are after databases -
easier
– select into
• The logic is reusable
• Select-Options are flexible – order by
• Authority check is made • More flexible
automatically • More specific coding is
easier work for the database.

BUT: BUT:
Logical databases are slower ; Authority check missing
Changes to a logical database ; Parameters for select-
affect all programs that use it options have to be
coded
Classic Scenario verses Function
Module LDB_PROCESS

Classic Scenario

LDB_PROCESS
Topic Objectives

In this Topic, the participant will learn:


• To Define and Call Subroutines
• To Maintain and Call Function Modules
• Selection Criteria for ABAP Report Programs
• Advanced Reporting using ABAP
• ALV Reporting
• Logical Databases and usage
Background Processing
• Background Processing of
of Programs
Programs
In this section, we will…
• Describe the Process Flow of Background
Jobs
• Define the Scheduling and Processing of
Background Jobs
• Describe the Job Results for a Job Log and
Spool Requests
• Define the Authorization Objects
• Describe the Automatic Generation of Jobs
• Define the Function Modules for
Generating Jobs
Background Processing

Characteristics
Execution
Execution ofof ABAP
ABAP programs
programs without
without
dialog
dialog (no
(no specific
specific job-control
job-control language
language
for
for operating
operating system
system required)
required)

Complete
Complete integration
integration in
in the
the SAP
SAP System
System

Parallel
Parallel background
background and
and online
online operation
operation

Ease
Ease of
of use
use

Distributed
Distributed processing
processing
Process Flow Overview

SCHEDULE
SCHEDULE PROCESS
PROCESS OVERVIEW
OVERVIEW

ABAP
PROGRAM.

Background
Background
Work
Work
process Job Log
VARIANT JOB process

Date & Time


Scheduling Background Jobs

Function
Function "Job
"Job definition"
definition"

Job name ............


.
.
. Steps
Steps

Print
Print data
data

Start
Start date
date

Period
Period

Save
Save
Processing Jobs – Job Status

Scheduled
Scheduled

Released
Released

Ready
Ready

Active
Active

Finished
Finished
Cancelled
Cancelled
Job Results – Job Log
Job
Job Log
Log

Date Time Msg. ID Message

Job Started
07.21.1998 14:00:20 00516
step001 started
07.21.1998 14:00:21 00550 (program ZTEST, variant
. . . ZVAR, . .
. . . .
. . . .
. . . .
. . . .
07.21.1998 14:00:56 00517 Job Finished
Job Results – Spool Request

Job
Job list
list
Job
Job name
name JOBA
User
User name
name Smith
Start
Start date
date 10/29/92 WRITE...

Spool
Passing Data To Memory
Job:
Step 1:

Global TABLES: LFA1.


Global REPORT…
ABAP
ABAP DATA FLAG, F1, BEGIN OF TAB OCCURS
Memory
Memory IF...
EXPORT FLAG TAB LFA1 F1 TO MEMORY.
ENDIF.
FLAG

Step 2:
F1
REPORT...
TAB DATA FLAG, F1, BEGIN OF TAB OCCURS.
IF SY-BATCH NE SPACE
IMPORT FLAG TAB LFA1 F1
FROM MEMORY.
IF SY-SUBRC NE SPACE
AND FLAG NE SPACE.
...
Authorizations and Background
Processing
Objects
Objects Fields
Fields Values
Values Description
Description
S_BTCH_JOB
S_BTCH_JOB Job
Job activities
activities DELE
DELE Delete
Delete job
job
PLAN
PLAN Schedule
Schedule jobjob
LIST
LIST Display
Display job
job list
list
PROT
PROT Display
Display job
job log
log
RELE
RELE Release
Release job
job
SHOW
SHOW Display
Display job
job

S_BTCH_NAM
S_BTCH_NAM User
User name
name <User
<User names>
names> User
User names
names that
that can
can
be
be specified
specified when
when
scheduling
scheduling aa job
job

S_PROGRAM
S_PROGRAM Authorization
Authorization <Auth.
<Auth. group>
group> Programs
Programs thatthat belong
belong
group
group to
to the
the authorization
authorization
groups
groups entered
entered here
here can
can
be
be specified
specified when
when
User
User action
action BTCSUBMIT
BTCSUBMIT scheduling
scheduling aa job.
job.
S_BTCH_ADM <Usernames> User names that can
Batch be specified to
Administrator ID maintain batch jobs
Generating Jobs Automatically

Create
Create job
job

Insert
Insert job
job steps
steps

Complete
Complete job
job
Function Modules for Generating
Jobs
JOB_OPEN
JOB_OPEN

Job name JOBNAME


Job number JOBCOUNT

JOB_SUBMIT
JOB_SUBMIT

Job name JOBNAME


Job number JOBCOUNT
Report name REPORT
Variant VARIANT

JOB_CLOSE
JOB_CLOSE

Job name JOBNAME


Job number JOBCOUNT
Function Module ‘JOB_OPEN’

CALL
CALL FUNCTION
FUNCTION ’JOB_OPEN’
’JOB_OPEN’

EXPORTING
EXPORTING
DELANFREP
DELANFREP == <delete
<delete internal
internal report>
report>
JOBNAME
JOBNAME == <job
<job name>
name>

IMPORTING
IMPORTING
JOBCOUNT
JOBCOUNT =<job
=<job number>
number>

EXCEPTIONS
EXCEPTIONS
CANT_CREATE_JOB
CANT_CREATE_JOB == 11
INVALID_JOB_DATA
INVALID_JOB_DATA == 22
JOBNAME_MISSING
JOBNAME_MISSING == 33
..
..
..
Function Module ‘JOB_SUBMIT’

CALL
CALL FUNCTION
FUNCTION ’JOB_SUBMIT’
’JOB_SUBMIT’

EXPORTING
EXPORTING
AUTHCKNAM
AUTHCKNAM == <user>
<user>
JOBCOUNT
JOBCOUNT == <job
<job number>
number>
JOBNAME
JOBNAME == <job
<job name>
name>
REPORT
REPORT == <report
<report name>
name>
VARIANT
VARIANT == <report
<report variant>
variant>

EXCEPTIONS
EXCEPTIONS
..
..
..
Function Module ‘JOB_CLOSE’

CALL
CALL FUNCTION
FUNCTION ’JOB_CLOSE’
’JOB_CLOSE’

EXPORTING
EXPORTING
JOBCOUNT
JOBCOUNT == <job
<job number>
number>
JOBNAME
JOBNAME == <job
<job name>
name>
SDLSTRTDT
SDLSTRTDT == <start
<start date>
date>
SDLSTRTTM
SDLSTRTTM == <start
<start time>
time>
STRTIMMED
STRTIMMED == <immediate
<immediate start>
start>
..
..
..

IMPORTING
IMPORTING
JOB_WAS_RELEASED
JOB_WAS_RELEASED == <job
<job released
released indicator>
indicator>

EXCEPTIONS
EXCEPTIONS
..
..
..
Example: Program to Generate a
Job
REPORT B180D025.
DATA: JOBCOUNT LIKE TBTCO-JOBCOUNT.
PARAMETERS: JOBNAME LIKE TBTCO-JOBNAME,
REPORT LIKE SY-REPID,
VARIANT LIKE RALDB-VARIANT,
BTCHUSER LIKE SY-UNAME.
START-OF-SELECTION.
CALL FUNCTION ’JOB_OPEN’
EXPORTING JOBNAME = JOBNAME
IMPORTING JOBCOUNT = JOBCOUNT.
CALL FUNCTION ’JOB_SUBMIT’
EXPORTING AUTHCKNAM = BTCHUSER
JOBCOUNT = JOBCOUNT
JOBNAME = JOBNAME
REPORT = REPORT
VARIANT = VARIANT.
WRITE: / JOBNAME, JOBCOUNT,
REPORT, VARIANT, BTCHUSER.
CALL FUNCTION ’JOB_CLOSE’
EXPORTING JOBCOUNT = JOBCOUNT
JOBNAME = JOBNAME.
Other Function Modules for
Generating a Job

BP_JOBLOG_SHOW
BP_JOBLOG_SHOW BP_JOBLOG_READ
BP_JOBLOG_READ
Display
Display job
job log
log Read
Read job
job log
log

BP_JOB_COPY
BP_JOB_COPY BP_JOB_DELETE
BP_JOB_DELETE
Copy
Copy job
job Delete
Delete job
job

BP_EVENT_RAISE
BP_EVENT_RAISE BP_CHECK_EVENTID
BP_CHECK_EVENTID
Trigger
Trigger event
event Check
Check event
event name
name

You might also like