Académique Documents
Professionnel Documents
Culture Documents
1
Première séance :
- Présentation : Introduction à l’ABAP I
- Atelier :
o Prise en main de l’outil / Hello World
o Variables ABAP
o Le dictionnaire de donnée
o Interrogation de la base de donnée et tables internes
o Manipulation chaine de caractère
Deuxième séance :
- Présentation : Introduction à l’ABAP II
- Atelier :
o Utilisation du Perform
o Séparation de code
o Optimisation du code pour plus de performance
Sixième séance :
- Présentation : Smartform
- Atelier :
o Introduction / Overview
o Eléments du formulaire (Attribut, interface,
fenêtre, template, table,…)
o Mise en forme / Smartstyle
o Programme d’impression / Module de Fonction
o Exemple pratique
ABAP Programming Language
4
ABAP Workbench
Repository Information
ABAP Editor
System
5
Reporting
Program
• A series of ABAP statements
Report:
• An ABAP program whose output is a
list
Module Pool:
• A dialog program which is a collection
of screens
List:
• The output generated by an ABAP
report program
7
Reporting and ABAP Development
Reporting
ABAP
8 Development
Programming Environment -
The ABAP Editor
9
Programming Environment
Online Debugging
10
Basic Functions of the ABAP Editor
Find and
Repeat Find Standard toolbar
Syntax Check
Undo
ABAP Help
Toggles from
display to
change mode
Cut, copy and paste
to and from a buffer
11
The PROGRAM Menu Option
12
The EDIT Menu Option
13
The GOTO Menu Option
14
The UTILITIES Menu Option
15
The BLOCK/BUFFER Menu Option
16
The SETTINGS Menu Option
Preferred settings:
· PC mode with
line numbering
· With
compression logic
· Key word large
17
Basic ABAP Program Syntax
18
Chaining Statements in ABAP
MOVE: 1 TO COUNT,
‘President’ TO TITLE.
19
Comments in ABAP
A double quotation mark
(“) anywhere on a line
makes everything that
follows a comment.
20
ABAP Program Structure
21
ABAP Program Attributes
Type:
1 - Executable Program
I - Include Program
M - Module Pool
F - Function Group
S - Subroutine Pool
Application:
S - Basis
U - EDM (Enterprise Data Mode
V - Sales and Distribution
Y - Customer Head Office
Z - Customer Branch
22
* - For all Applications
Running an ABAP Program
An Alternative
Method:
From the System
Menu, choose
From the ABAP Editor: Services® Reporting
With the program to launch
displayed, the program
Program®Execute
One Final Method:
From the ABAP Editor: Initial Screen,
type the program name, then click on
the Execute push-button
23
The ABAP “WRITE” Statement
WRITE ‘********************’.
SKIP 2.
WRITE: ‘The date today is:’, SY-DATUM,
‘The current time is:’, SY-UZEIT.
SKIP 2.
WRITE ‘********************’. Notice that there is an
automatic space between the
fields written to the list.
<program title> 1
----------------------------------------------------------------------------------
-
********************
The date today is: 12/30/1996 The current time is: 12:32:06
********************
24
ABAP Format Specifications
Format Options
27
“NEW-PAGE” Statement
WRITE ‘***** City of Philadelphia *****’.
SKIP.
WRITE: / ‘Ticket Date’, 15 SY-DATUM.
WRITE: / ‘Ticket Time’, 15 SY-UZEIT.
SKIP.
WRITE ‘***** Department of Public Safety *****’.
NEW-PAGE.
WRITE: ‘Comments:’.
<program title> 1
--------------------------------------------------------------------------------------
***** City of Philadelphia *****
28
<program title> 2
---------------------------------------------------------------------------------------
Comments:
Writing Icons and Symbols
INCLUDE <ICON>.
INCLUDE <SYMBOL>.
Execute programs
“SHOWICON” and
“SHOWSYMB” to
display the system’s
icons and symbols.
29
Maintaining Page Headers
30
Numbered Texts (Text Symbols)
I: Integer D: Date
P: Packed # T: Time
32
Declaring Variables
33
Initial Values
C: (blank) N: zeroes
I: zero D: 00000000
P: zero T: 000000
F: zeroes X: 00
The “CLEAR” statement sets a field back to its initial value, not
its default value.
34
Assigning Default Values
35
Declaring “Like” Fields
36
Declaring Constants
37
User-Defined Data Types
38
Output Characteristic for Data
Types
Standard Length Justification
39
Output for Numeric Fields
DATA: FLOAT TYPE FVALUE
‘98.7654321E2’,
PACK TYPE PVALUE 12,
INT TYPE I VALUE 32.
WRITE: / FLOAT,
/ FLOAT EXPONENT 1 DECIMALS 3,
/ FLOAT EXPONENT 0 DECIMALS 2,
/ PACK,
/ PACK DECIMALS 1,
9.876543210000000E+03
/ INT DECIMALS 2.
987.654E+01
9876.54
These fields are not aligned 12
because of the different 12.0
standard output lengths of 32.00
40 the numeric type fields.
Assigning Values to Fields
Operators Functions
+ - * / ** SQRT, EXP, LOG,
DIV and MOD SIN, COS, STRLEN, . . .
COUNTER = COUNTER + 1.
SALARY = BASE * BONUS_PERCENT.
LENGTH = STRLEN( NAME ).
ANSWER = ( 10 + SQRT( NUM1 ) ) / ( NUM2 - 10 ).
42
Sub-Fields in ABAP
DOB = ‘19621230’.
TODAY= SY-DATUM.
44
Declaring Fields with PARAMETERS
ADD 10 TO NUM.
WRITE: / NUM,
‘----’,
NAME.
“Selection Screen”
32 ---- AARON
45
Selection Texts
46
Working with Field Symbols in
ABAP
DATA: NUM TYPE I VALUE 12.
FIELD-SYMBOLS: <F1>,
<F2> TYPE I, A field symbol is a
<F3> LIKE NUM. “pointer” that assumes a
field’s address, not a
ASSIGN: NUM TO <F1>, field’s value.
NUM TO <F2>,
NUM TO <F3>.
<F1> = 32.
FIELD-SYMBOLS <FSYMBOL>.
ASSIGN TEXT_LINE+2(5) TO <FSYMBOL>.
* this assigns 5 characters of text_line starting at
* position 3 to the field string.
IF SY-SUBRC = 0.
WRITE: / ‘The contents of field’, FIELD, <FSYMBOL>.
ELSE.
WRITE: / ‘Failure assigning field’, FIELD, ‘to field symbol’.
ENDIF.
50
Example Using the SELECT
Statement
TABLES: KNA1.
SELECT *
FROM KNA1.
WRITE: / KNA1-KUNNR, KNA1-NAME1.
ENDSELECT.
51
The SELECT Statement with a
WHERE Clause
TABLES: KNA1.
SELECT *
FROM KNA1
WHERE KTOKD = ‘0001’.
WRITE: / KNA1-KUNNR, KNA1-NAME1.
ENDSELECT.
52
SY-SUBRC
TABLES: KNA1.
SELECT *
FROM KNA1
WHERE KTOKD = ‘0001’.
WRITE: / KNA1-KUNNR, KNA1-NAME1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / ‘No records found.’.
ENDIF.
53
The INTO Clause
TABLES: KNA1.
SELECT KUNNR
NAME1
FROM KNA1
INTO (VL_KUNNR,
VL_NAME1).
WRITE : / VL_KUNNR , VL_NAME1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / ‘No records found.’.
ENDIF.
54
INTO CORRESPONDING-FIELDS
TABLES: KNA1.
SELECT KUNNR
NAME1
FROM KNA1
INTO CORRESPONDING-FIELDS
OF WT_KNA1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / ‘No records found.’.
ELSE.
LOOP at WT_KNA1.
WRITE WT_KNA1-IKUNNR.
ENDLOOP.
ENDIF.
55
Basic Flow Control in ABAP
56
The IF Statement
IF X =5.
WRITE :/ ‘The value of X is
5’.
ELSEIF X =6.
WRITE :/ ‘The value of X is
6’.
ELSE .
WRITE :/ ‘X is neither 5 nor
6’.
ENDIF.
57
Logical Expressions
Logical Expressions use :
RELATIONAL OPERATORS
LOGICAL OPERATORS
STRING COMPARISON
OPERATORS
58
Relational Operators
Comparison Syntax
Is Equal to =, EQ
Is not equal to < >, ><, NE
Greater than >, GT
Greater than or equal to > =, = >, GE
Less than <, LT
Less than or equal to <=, =<, LE
59
Logical Operators
AND
OR
NOT
If not ( X = 0 )
or not ( Y = 1 and
Z = X or X = 3
and ( Z = 5 )
61
String Comparison Operators
Comparison Syntax
Contains only CO
Contains any CA
Contains string CS
Contains pattern CP
Contains not only CN
Contains not any NA
Contains no string NS
Contains no pattern NP
62
The CASE Statement
e n .
h ’
W AVE
‘S en
CASE Wh TA’.
SY-UCOMM. ‘S R
When
‘SRTD’.
Whe
n
Wh ‘GET
D’.
‘PI e n
CK
Don’t forget ’.
those periods!
63
The DO Loop
J =4.
DO.
DO J TIMES.
WRITE :/ ‘Hello
WRITE :/ ‘Hello
world!’.
world!’.
ENDDO.
ENDDO.
64
The WHILE Loop
If expression evaluates to
If expression evaluates to FALSE, code in loop is
TRUE, code in loop is NOT executed, and
executed. control moves to after
ENDWHILE.
65
Nested Loops and Control
Structures
DO 2 TIMES.
WRITE :/ SY-INDEX.
DO 3 TIMES.
WRITE : / ‘ ‘, SY-INDEX.
ENDDO.
ENDDO.
66
The ON CHANGE OF Statement
68
The EXIT Statement
IF SY-SUBRC <>0.
EXIT.
ENDIF.
69
The CONTINUE Statement
DO 10 TIMES.
IF SY-INDEX >4.
CONTINUE .
ENDIF.
WRITE :/ SY-INDEX.
ENDDO.
70
Data Structures
LN FN City ST.
LN FN City ST.
71
Declaring a Structure - Method #1
Is this statement
1 REPORT YN1C0008.
2
necessary for the Basic Syntax:
3 TABLES: TABNA. code? DATA: BEGIN OF <name>
4 DATA: BEGIN OF ADDRESS,
5 FLAG TYPE C, <field1> . . .
6 ID LIKE TABNA-ID,
7 NAME1 LIKE TABNA-NAME1, <field2> . . .
8 CITY LIKE TABNA-CITY,
9 END OF ADDRESS.
...
10 MOVE ‘X’ TO ADDRESS-FLAG. END OF <name>.
11 MOVE ‘0001’ TO ADDRESS-ID.
12 MOVE ‘Smith’ TO ADDRESS-NAME1.
13 MOVE ‘Philadelphia’ TO ADDRESS-CITY.
14
15 WRITE ADDRESS.
16 Address Structure
17 Flag ID Name1 City
18
19
20
21
22
23
72
Declaring a Structure - Method #2
REPORT Yxxxxxxx. Basic Syntax:
TYPES: BEGIN OF ADDR,
TYPES: BEGIN OF <name1>,
FLAG,
<field1> . . . ,
ID LIKE EMPLOYEE-ID,
NAME1 LIKE EMPLOYEE-NAME1,
<field2> . . . ,
CITY LIKE EMPLOYEE-CITY, ... ,
END OF ADDR. END OF <name1>.
DATA: ADDRESS TYPE ADDR. DATA: <name2> TYPE
<name1>.
MOVE: ‘X’ TO ADDRESS-FLAG,
‘00001’ TO ADDRESS-ID,
Address Structure
‘Smith’ TO ADDRESS-NAME1,
Flag ID Name1 City
‘Philadelphia’ TO ADDRESS-CITY.
WRITE ADDRESS.
73
Populating a Structure with
Field-by-Field Transport
REPORT Y170DM37.
EMPLOYEE
TABLES: EMPLOYEE. ID Name1 City
DATA: BEGIN OF ADDRESS, 000000001 Electronics Inc. Waldorf
FLAG, MOVE-CORRESPONDING EMPLOYEE
ID LIKE EMPLOYEE-ID,
Address TO ADDRESS.
Flag ID Name City
NAME LIKE EMPLOYEE-NAME1,
000000001 Waldorf
CITY LIKE EMPLOYEE-CITY,
END OF ADDRESS. Clear <f1>.
74
Internal Table Types
• Standard
• Sorted
• Hashed
75
Creating an Internal Table
with Header Line
REPORT Y170DM38. The TYPES statement defines the
TABLES: EMPLOYEE. structure and data type for the
TYPES: BEGIN OF EMP, internal table.
ID LIKE EMPLOYEE-ID, The DATA statement with an
NAME1 LIKE EMPLOYEE-NAME1, INITIAL SIZE creates the actual
COUNTRY LIKE EMPLOYEE-COUNTRY, internal table capable of storing
END OF EMP. data. Because of the WITH
HEADER LINE addition, this
internal table is created with a
DATA: EMPTAB TYPE STANDARD TABLE
header line.
OF EMP INITIAL SIZE 10 WITH HEADER
LINE. ID NAME1 COUNTRY
Header Line
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB.
ENDSELECT.
76
Internal Table Keys
• Implicit Key
– All character fields
• Explicit Key
– User-defined
• e.g. … WITH [ UNIQUE/NON-UNIQUE ] KEY
FIELD1 FIELD2 ...
77
Size of an Internal Table
78
Loading an Internal Table with a
Header Line
REPORT Y170DM42. With both versions of the
TABLES: EMPLOYEE. APPEND statement, memory
space for ten records is allocated
TYPES: BEGIN OF EMP,
when the first record is written to
COUNTRY LIKE EMPLOYEE-COUNTRY, the internal table.
ID LIKE EMPLOYEE-ID,
SALARY LIKE EMPLOYEE-SALARY,
END OF EMP. Example 1
DATA: EMPTAB TYPE STANDARD TABLE
More than ten entries can be
OF EMP INITIAL SIZE 10 WITH HEADER saved in the internal table.
LINE.
79
Loading an Internal Table with a
Header Line
80
Internal Table with Header Line
EMPLOYEE
ID NAME1
COUNTRY
B Header Line
81
Internal Table with Header Line
1 EMPLOYEE
ID NAME1
COUNTRY
Header Line
82
Internal Table with Header Line
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
83
Internal Table with Header Line
1 EMPLOYEE
2
ID NAME1 COUNTRY
00000001 Baker Distributors USA Header Line
5 ID NAME1
COUNTRY
00000002 Diversified Indust... USA Header Line
86
APPEND <work area> to <EMPTAB>.
Internal Table without a Header
Line WHY???
Performance Issues
87
Internal Table without a Header
Line
EMPLOYEE
ID NAME1
COUNTRY
B Work Area
88
Internal Table without a Header
Line
1 EMPLOYEE
ID NAME1
COUNTRY
3 00000001 Baker Distributors USA 1 This work
area is not
2 attached to the
3. body of the
internal table.
.
.
10
89
Transferring ABAP Dictionary
Table Structures
REPORT Y170DM41.
TABLES: EMPLOYEE.
DATA: EMPTAB LIKE STANDARD TABLE OF
EMPLOYEE INITIAL SIZE 10 WITH HEADER LINE.
The internal table EMPTAB
will have the exact same
SELECT * FROM EMPLOYEE.
structure as the dictionary
MOVE EMPLOYEE TO EMPTAB. table EMPLOYEE.
APPEND EMPTAB.
ENDSELECT.
90
Automatic Field Conversion
91
Mass Reading from Database Tables
into Internal Tables
92
Processing an Internal Table
REPORT Y170DM45.
TABLES: EMPLOYEE.
This LOOP AT <EMPTAB>
TYPES: BEGIN OF EMP, statement allows for a logical
COUNTRY LIKE EMPLOYEE-COUNTRY, expression in a WHERE clause to
NAME1 LIKE EMPLOYEE-NAME1, limit the processing of the internal
SALES LIKE EMPLOYEE-SALES,
table.
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP INITIAL SIZE
10 WITH HEADER LINE.
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT. If no internal table entries
LOOP AT EMPTAB WHERE COUNTRY BETWEEN ‘A’ AND ‘D’. qualify under the logical
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1,
expression, the statement
EMPTAB-SALES. within the loop is not
ENDLOOP. executed and SY-SUBRC is
IF SY-SUBRC NE 0. WRITE: / ‘NO ENTRIES’. ENDIF. set to 4.
93
System Field SY-TABIX
REPORT Y170DM46.
TABLES: EMPLOYEE.
94
Accumulating Data within an
Internal Table
REPORT Y170DM43.
TABLES: EMPLOYEE. COLLECT <EMPTAB>.
TYPES: BEGIN OF EMP, Country Sales
COUNTRY LIKE EMPLOYEE-COUNTRY, Header
D 400,000
SALES LIKE EMPLOYEE-SALES, Line
USA 1,000,000
END OF EMP.
DATA: EMPTAB TYPE STANDARD TABLE OF EMP GB 500,000
INITIAL SIZE 10 WITH HEADER LINE. D 7,800,000
96
Control Level Processing
• AT FIRST
• AT NEW < field >
• AT END < field >
• AT LAST
97
Reading a Single Table Entry
98
Maintaining Internal Tables
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO
EMPTAB.
APPEND EMPTAB. INSERT <EMPTAB> INDEX <i>.
ENDSELECT. MODIFY <EMPTAB> INDEX <i>.
DELETE <EMPTAB> INDEX <i>.
READ TABLE EMPTAB INDEX 1.
MOVE ‘ABC’ TO EMPTAB-NAME1.
MODIFY EMPTAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
WRITE / ‘Attempt to modify failed.’.
ELSE.
Check SY-SUBRC after
WRITE: / EMPTAB-COUNTRY, EMPTAB-
NAME1. every attempt to change
ENDIF. an internal table entry.
INSERT EMPTAB INDEX 1.
DELETE EMPTAB INDEX SY-TABIX.
99
Working with an Internal Table
without a Header Line
101
Information about an Internal Table
REPORT Y170DM49.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP,
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
LINE_COUNT TYPE I,
INITIAL_COUNT TYPE I. DESCRIBE TABLE <internal table>
SELECT * FROM EMPLOYEE. LINES <var1>
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
OCCURS <var2>.
ENDSELECT.
DESCRIBE TABLE EMPTAB
LINES LINE_COUNT
OCCURS INITIAL_COUNT.
WRITE: / ‘ lines:’, LINE_COUNT,
/ ‘occurs:’, INITIAL SIZE_COUNT.
screen output
102
Calling the SAP Table Editor
REPORT Y170DM50.
TABLES: EMPLOYEE.
TYPES: BEGIN OF EMP,
COUNTRY LIKE EMPLOYEE-COUNTRY,
NAME1 LIKE EMPLOYEE-NAME1,
END OF EMP,
DATA: EMPTAB TYPE STANDARD TABLE OF EMP
INITIAL SIZE 10 WITH HEADER LINE,
SELECT * FROM EMPLOYEE.
MOVE-CORRESPONDING EMPLOYEE TO EMPTAB.
APPEND EMPTAB.
ENDSELECT.
EDITOR-CALL FOR EMPTAB.
CHECK SY-SUBRC EQ 0.
LOOP AT EMPTAB WHERE NAME1 EQ ‘Maurice Cheeks’.
WRITE: / EMPTAB-COUNTRY, EMPTAB-NAME1. screen output
ENDLOOP.
IF SY-SUBRC NE 0. WRITE: / ‘No records.’. ENDIF.
103
What Are Subroutines
Subroutine
Calculate_tax
104
Modularization
External FunctionModule
Call Call
REPORT YDEMOSB1. ABAP Function Builder
Function module
Form sub ZZCALC
105
Actual / Formal Parameters
Main 99 1
Program a1 a2 a3
Call Calculate_tax
Transfer values of fields a1 a2
Get value of field a3
Actual parameters a1 a2 a3
Parameter transfer
Pass by Value a1
b ro u tine
Su
Pass by Value and Result
f1
a1
Subro
utine
Pass by Reference a1 f1
b ro u tine
Su
f1
107
Declaring and Calling a Subroutine -
Pass by Value(1) & Reference(2)
REPORT Y170D091.
TABLES: ....
DATA: .…
.
.
.
PERFORM <name> USING
<a1> <a2>
<a3> <a4>.
.
.
.
FORM <name> USING
VALUE(<f1>) Pass by value
VALUE(<f2>)
Pass <f3>
by reference
<f4>.
ENDFORM.
108
Declaring and Calling a Subroutine -
Pass by Value and Result(3)
REPORT YSDEM000.
TABLES: ... .
DATA: ... .
.
.
.
PERFORM <name> USING
<a1> <a2>
CHANGING<a3>.
.
.
.
FORM <name> USING
VALUE(<f1>)
VALUE(<f2>)
CHANGING VALUE(<f3>).
.
.
Pass by value
.
f 3 =“A”.
.
Pass by value and result
.
.
ENDFORM.
109
Global versus Local Data
report y170dm86.
Local Data
* ------------------------------------------------------------------------------------------------------
* FORM SUB1 *
* ---------------------------------------------------------------------------------------------------------------------------
form sub1.
data: begin of tab occurs 10, “ <----------- local data
id like tabna-id,
name1 like tabna-name1,
end of tab.
local: num . “ <----------- local data
. . . <statements>
endform.
110
Local Data in Subroutines
Statement Value Upon Entering Value Upon Value Upon Entering
between Subroutine First Time Returning to Subroutine Next
FORM… Main Program Time
ENDFORM
DATA Whatever the local If declared Re-initializes based
DATA statement globally, returns on local DATA
initializes to old value, statement
otherwise not
recognized
LOCAL Last set value from Last set value Last set value from
main program from main main program
program
111
Passing Structures as Parameters
ENDFORM.
112
Passing Internal Tables as
Parameters
REPORT Y170D093.
DATA : BEGIN OF TAB OCCURS 10,
F1 LIKE TABNA-COUNTRY,
F2 LIKE TABNA-NAME1,
.
.
.
END OF TAB.
DATA : X.
.
.
.
PERFORM SUB1 TABLES TAB
. USING X.
.
.
FORM SUB1 TABLES FRED STRUCTURE TAB
USING Z.
LOOP AT FRED.
WRITE: / FRED-F1
FRED-F2.
ENDLOOP.
ENDFORM .
113
External Subroutines
Function Builder
FM Group : FIBU
FM_01 ...
Program 1 FM_02 ... Program 2
FM Group : XYZ
CALL FM_03 ... CALL
FUNCTION FM_04 ... FUNCTION
‘FM_0’ ‘FM_02’
... Program 3 ...
CALL FUNCTION
‘FM_02’
115
...
Function Module Parts
FM -02
Section I … Administration FM Group :
Section II … Interface
FIBU
FM_01 ...
Chapter 1 … Import
Chapter 2 … Changing
FM_02 ...
Chapter 3 … Export
Chapter 4 … Tables
Chapter 5 … Exceptions
116
Creating a Function Group
117
Administration
118
Interfaces
Program XYZ.
Call function
‘Y_DEMO_FUNC’
Import
Changing
Export
Tables
Exceptions Function Module
Y_DEMO_FUNC
119
Import/Export Parameter Interface
‘LIKE’
Pass parameter
‘TYPE’ by reference
120
Table Parameters/Exceptions
Interface & Documentation
Parameter name Flag parameter
as optional
‘LIKE’ ‘TYPE’
121
Function Module Source Code
function y_demo_function_module.
*”--------------------------------------------------------------------------------------------------
*” Local interface:
*” IMPORTING
*” REFERENCE(FIELD1) LIKE TABNA-COUNTRY
*” EXPORTING
*” VALUE(FIELD3) LIKE TABNA-NAME1
*” TABLES
*” TAB STRUCTURE TABNA
*” CHANGING
*” VALUE(FIELD2) LIKE TABNA-ID
*” EXCEPTIONS
*” INVALID_DATA
*” SYSTEM_ERROR
*” -------------------------------------------------------------------------------------------------
endfunction.
122
Exceptions
INVALID_OPERATOR
Function Module
...
IF...
RAISE INVALID_OPERATOR.
* MESSAGE. . . RAISING
INVALID_OPERATOR.
ENDIF.
...
123
Example - Raising Exceptions
case operator.
when ’+’.
result = operand1 + operand2.
when ’-’.
result = operand1 - operand2.
when ’/’.
if operand2 <> 0.
result = operand1 / operand2.
else.
raise division_by_zero.
endif.
when ’* ’
result = operand1* operand2.
when others.
raise invalid_operator.
endcase.
124
Calling a Function Module
report ymodemo message-id yj.
parameters: operand1 type i,
operator,
operand2 like operand1.
data: result type p decimals 2.
call function ’Y_CALCULATOR’
exporting
operand1 = operand1
operator = operator
operand2 = operand2
importing
result = result
exceptions
invalid_operator = 1
division_by_zero = 2
others = 3.
125
Calling a Function Module
(Continued)
exceptions
invalid_operator = 1
division_by_zero = 2
others = 3.
case sy-subrc.
when 1.
message e001.
when 2.
message e005.
when 3.
message e007.
endcase.
126
Program Organisation
SAPL <gr>
L<gr>TOP
*System-defined include files
FUNCTION-POOL <gr>
INCLUDE L<gr>TOP. Message-ID ZZ.
INCLUDE L<gr>UXX DATA: “Global DATA
*User-defined include files
L<gr>UXX
INCLUDE L<gr> U01.
L<gr>U01 INCLUDE L<gr> U02.
FUNCTION FA. .
.
L<gr>U02 .
FUNCTION FB.
127
Subroutine Includes for Function
Groups
Main Program
L <gr> TOP
FUNCTION-POOL <gr>.
DATA: X
TABLES: . . .
Program Subroutines
FUNCTION . . . L <gr> F01
DATA: . . . FORM SUB1 USING...
MOVE X TO . . . DATA: . . .
ENDFUNCTION. MOVE. . . TO X.
ENDFORM.
129
Test Environment
Export Parameters
130
Remote Function Call (RFC)
Program
ABAP \/4
CALL FUNCTION. . .
DESTINATION. . .
EXPORTING. . . Function
Function Module
Module
IMPORTING. . .
TABLES. . . FUNCTION REMOTE_CALL
EXCEPTIONS. . . ...
RAISE ERROR.
...
ENDFUNCTION.
External program
RFCLid
External Program. . .
RfcOpen (. . .)
RfcCallReceive (. . .)
RfcClose (. . .)
131
Modularization:
The Include Technique
132
SQL (SELECT)
ABAP Programs
ABAP
Reports
133
WHERE Clause: Single Access
*& *
* & ------------------------------------------------------------------------------------------
*
In the return-code field
report y170dm64. (SY-SUBRC),
0 = successful and
4 = entry does not exist.
tables: tabna.
if sy-subrc = 0.
write: tabna-country, tabna-id,
tabna-name1.
endif.
134
Syntax: Restricted Loop Processing
ENDSELECT. EQ =
GE >= =>
LE <= =<
NE <> >< Relational
GT > Operators
LT <
135
Syntax: Between Values,
Templates and Lists
136
WHERE Clause: Loop Processing
with Restricted Selection
report y170md65.
tables: tabna.
* The result of the select statement is limited by the conditions of
* the where clause.
The result set is limited by the
select * from tabna
where country eq ‘USA’
conditions in the
and id between ‘0000001’ WHERE clause.
and ‘0000002’ .
write: / tabna-country, tabna-id.
tabna-name1.
endselect.
if sy-subrc ne 0.
write: / ‘The conditions cannot be satisfied’.
endif.
137
LIKE: Example of a Template (%_)
*& *
*& *
*&-----------------------------------------------------------------------------------------------*
report y170dm66.
The ‘_’ is an any character
tables: t001.
positional parameter.
* Using a template during selection.-----------------------------------------------
The ‘%’ allows for any string
of any length.
select* from t001
where bukrs like ‘__01’
and butxt like ‘SAP%’.
write: / t001-bukrs, t001-butxt.
endselect.
if sy-subrc ne 0.
write: / ‘The conditions cannot be satisfied’.
endif.
*
138
IN: Example of a Comparison
Value List
tables: tabna.
if sy-subrc ne 0.
write: / ‘The conditions cannot be satisfied’.
endif.
139
IN: Example of a Range
tables: lfa1.
data: begin of tab occurs 10. This internal table is the same one
sign (1), that is created automatically when
option (2),
a selection screen is processed. The
low like lfa1-lifnr,
high like lfa1-lifnr, RANGES statement builds the
end of tab. structure of the table
move:‘I’ to tab-sign, automatically.
‘BT’ to tab-option, RANGES <name> FOR <field>.
‘0000000001’ to tab-low,
‘9999999999’ to tab-high.
append tab. DATA: BEGIN OF <name>
select from lfa1 OCCURS <n>,
tables: lfa1.
where lifnr in tab. SIGN(1),
Ranges: tab for lfa1-lifnr.
write: / lfa1-lifnr, lfa1-name1. OPTION(2),
move:‘I’ to tab-sign,
endselect. LOW LIKE <field>,
‘BT’ to tab-option,
HIGH LIKE <field>,
‘0000000001’ toEND
tab-low,
OF <name>.
‘9999999999’ to tab-high.
append tab.
select* from lfa1
where lifnr in tab.
write: / lfa1-lifnr, lfa1-name1.
140 endselect.
The ORDER BY Clause
SELECT * FROM <table>
WHERE <condition>
½*&-----------------------------------------------------------------------------------------------
*
*& Report Y170DM71 ORDER * BY <table field 1>
*& <table* field 2>
*&-----------------------------------------------------------------------------------------------
<table* field 3>
*& : *
*& *
:
*&-----------------------------------------------------------------------------------------------
*
<table field n>.
report y170dm71.
PRIMARY KEY.
tables: lfa1.
ENDSELECT.
* Notice the order by clause below.----------------------------------------------
*
select* from lfa1
order by name1.
write: / lfa1-lifnr, lfa1-name1.
endselect.
141
The SELECT DISTINCT Statement
REPORT Y170DM72.
TABLES: TABNA.
REPORT Y170DM73.
TABLES: TABNA.
DATA: TOTAL TYPE I,
MAX_SALES LIKE TABNA-SALES,
AVG_SALES LIKE TABNA-SALES,
MIN_SALES LIKE TABNA-SALES.
143
The Dynamic WHERE Clause
The parameters
REPORT Y170DM74. entered make up the
TABLES: TABNA. contents of the
PARAMETERS:
WHERE clause. The
WHERECL1(72) DEFAULT ‘COUNTRY = ‘ ‘USA’ ’ ’,
WHERECL2(3) DEFAULT ‘OR’, user has the option of
WHERECL3(72) DEFAULT ‘COUNTRY = ‘ ‘GB’ ’ ’.changing the
TYPE: BEGIN OF ITAB_RECORD, TEXT(72), conditions and
END OF ITAB_RECORD. dynamically effecting
DATA: WHERE_ITAB TYPE STANDARD TABLE OF
ITAB_RECORD INITIAL SIZE 3 WITH HEADER which way the
LINE. program will execute.
An internal table is created
WHERE_ITAB-TEXT = WHERECL1.
APPEND WHERE_ITAB.
which holds the WHERE
WHERE_ITAB-TEXT = WHERECL2. clause until it is used in the
APPEND WHERE_ITAB. SELECT statement.
WHERE_ITAB-TEXT = WHERECL3.
APPEND WHERE_ITAB.
APPEND WHERE_ITAB.
LOOP AT ITAB.
WRITE: / ITAB-COUNTRY, ITAB-ID.
ENDLOOP.
146
ENDSELECT.
Specifying the Table Name at
Runtime
REPORT Y170DM76.
DATA: TABLE_LINE(240).
WRITE: / TABLE_LINE.
ENDSELECT.
147
Joins: Why We Should Use Them
• Joins are more efficient than logical databases and nested selects.
• They access multiple tables with one select statement.
148
Inner Joins
SCARR SFLIGHT
149
Inner Joins Syntax
SELECT <table1~field1 table1~field2
table2~field3. . . >
INTO (<target >)
FROM <table1 > INNER JOIN <table2 >
150
The Driving table
SELECT scarr~carrname sflight~carrid sflight~connid
sflight~fldate
INTO (carrid, connid, date, carrname)
FROM scarr INNER JOIN sflight
ON scarr~carrid = sflight~carrid.
WRITE: / carrid, connid, date, carrname.
ENDSELECT.
151
Left Outer Joins
SCARR SFLIGHT
152
Left Outer Join Syntax
SELECT <table1~field1 table1~field2
table2~field3. . . >
INTO (<target >)
FROM <table1 > LEFT OUTER JOIN <table2 >
153
Open SQL Syntax Restrictions
The syntax for joins have been given certain restrictions in order to insure that they
produce the same results for all SAP supported databases.
154
Redundancy
LFA1 BSIK
155
Joins Accessing More than Two
Tables
SELECT <table1~field1 table1~field2 table2~field3. . .
>
INTO (<target >)
FROM (<table1 > INNER JOIN <table2 >
ON <table1~keyfield1 > = <table2~keyfield1 >
AND <table1~keyfield2 > = <table2~keyfield2 >
AND . . .)
INNER JOIN <table3 >
ON <table1~keyfield > = <table3~keyfield >
AND . . .
WHERE . . .
ENDSELECT.
156
Aliases
SELECT A~carrname
B~carrid
B~connid
B~fldate
INTO (carrid, connid, date, carrname)
FROM scarr AS A INNER JOIN sflight AS B
ON scarr~carrid = sflight~carrid.
WRITE: / carrid, connid, date, carrname.
ENDSELECT.
157
Subquery
SCARR SFLIGHT
158
Subquery Syntax/Example
159
Having Clause
160
Having Clause Syntax/Example
161
Authority Objects in SAP
162
The AUTHORITY-CHECK Statement
164
Nested SELECT Loops
TABLES: KNA1, KNB1, BSID.
SELECT * FROM KNA1.
WRITE: / KNA1-KUNNR, KNA1-NAME1.
SELECT * FROM KNB1
WHERE KUNNR = KNA1-KUNNR.
WRITE: / ' ', KNB1-BUKRS.
SELECT * FROM BSID
WHERE KUNNR = KNB1-KUNNR
AND BUKRS = KNB1-BUKRS.
WRITE: / ' ', BSID-BELNR, BSID-BUDAT, BSID-DMBTR.
ENDSELECT.
ENDSELECT.
ULINE.
ENDSELECT.
165
Logical Database Programs
CT
SELE ECT
SEL ECT
SEL LECT
E
ENDS CT
D S ELE
EN
S EL ECT
END
Security
Checks
166
Event-Driven Programming
START-OF-SELECTION.
GET <table>.
GET <table> LATE.
END-OF-SELECTION.
167
Attaching Code to an Event
Event
168
The START-OF-SELECTION Event
169
The GET Event
170
The GET LATE Event
171
Behaviour of a Logical
Database Program
EL ECT
S CT
SELE CT
SELE LECT
E
ENDS ECT
N D S EL
E CT
E L E
Security ENDS
Checks
172
Selection Screens with Logical
Database Programs
173
The END-OF-SELECTION Event
174
Selection Screens
175
The SELECT-OPTIONS Statement
???
176
Selection Tables
I BT 3 10
I EQ 1
E GE 7
177
Multi-Row Selection Tables
Click Here
To get Here
178
Creating User-Friendly Selection
Texts
Maintain Text
Elements
Selection Screen
179
Creating Parameters as Radio
Buttons
TABLES: KNA1
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME
TITLE TEXT-001.
PARAMETERS: ONE_TIME RADIOBUTTON GROUP GP1,
REGULAR RADIOBUTTON GROUP GP1,
BOTH RADIOBUTTON GROUP GP1
DEFAULT ‘X’.
SELECTION-SCREEN END OF BLOCK B1.
IF ONE_TIME = ‘X’.
PERFORM WRITE_ONE_TIME_CUST.
ELSEIF REGULAR = ‘X’.
PERFORM WRITE_REG_CUST.
ELSE.
PERFORM WRITE_CUST.
ENDIF.
180
Attaching Matchcodes to
Parameters
PARAMETERS: CUSTNO LIKE KNA1-KUNNR
MATCHCODE OBJECT DEBI.
181
Formatting the Selection Screen
SELECTION-SCREEN <option>.
183
Error Message at the Selection
Screen
184
Table T100: Messages
185
Table T100: Messages
Example
MESSAGE E001 WITH KNA1-KUNNR KNA1-BUKRS.
Message Types
A Abend
E Error
I Information
S Success
W Warning
186
Creating Message IDs and
Messages
1. Double-click on the newly
entered Message ID.
187
The Online Debugging Tool
188
The Online Debugging Display
Execution control
buttons. Click to display
current statement.
189
Online Debugging Display Modes
Use these buttons
to page through
the code
Online debugging
navigation buttons
190
Displaying Field Contents
Buttons for
hexadecimal display
Area for watch variables. These are user chosen variables that can
be monitored as they change value during execution.
191
Displaying the Contents of an
Internal Table
Contents of
internal table
192
Replacing Field Values at Runtime
193
Changing Internal Tables at Runtime
Debugger
195
Editing a Row of an Internal Table
196
Adding a Row to an Internal Table
197
Debugging Strategies
198
Using Breakpoints
199
Setting Static Breakpoints
CASE WORLD_SERIES.
WHEN COLORADO_ROCKIES.
WRITE ‘WORLD’S GREATEST TEAM’.
WHEN OTHERS.
WRITE ‘MAYBE NEXT YEAR’.
BREAK-POINT. Setting a static
breakpoint
ENDCASE.
200
Setting Dynamic Breakpoints
Use menu
path to add
break point
201
Displaying Dynamic Breakpoints
202
Setting Breakpoints at Keywords or
Events
203
Setting Watchpoints
Create watchpoint
buttons
Create
watchpoint
screen
204
Watch points overview screen
Button to
display watch
point overview
screen
Watch point
change/delete
Logical Operators
between watch points
205
Deleting and Deactivating
Breakpoints
206
Deleting and Deactivating
Dynamic Breakpoints
207
Debugging Without Breakpoints
208
Switching to the ABAP Editor from
within the Debugger
@#!##?*&% No Problem!
Debugging Editing
209
Releasing the Database During
Debugging
210
211