ABAP Programming Overview
ABAP Programming Overview
SAP
Application
Server
DB Server
SAP SYSTEM (3 Tier Architecture)
SAP GUI SAP GUI
Presentation Layer
(Windows based)
SAP Instance
Application Layer
Dispatcher M
(Windows Server/UNIX)
Request SAP Buffer
Queue
(Shared Mem)
D D B V S E
G
Oracle
Database Layer
Informix
(Windows Server/UNIX)
DB2
Database Server
MS SQL Server
MaxDB
ABAP Programming Overview
ABAP Overview
MOVE … IF ...
DATA ...
WHILE...
Advanced
Business
Application
Programming
ABAP Feature
Operating/Database system-independent
programming
ABAP contains a subset of SQL called Open SQL
for comfortable database access for various
database
ABAP Programming
ABAP Report
Dialog Programming(Transaction)
ABAP Program : Report
Report Program
: attribute type 1
(executable)
Reading
Data
Database
Reading data
Types of ABAP Report
1. Report Listing
2 2. Drill-down Report
3. Control-break Report
4. ALV Report
ABAP Program : Dialog Program
Dialog Program
: attribute type M
(Module Pool)
Reading
Data
Writing
Database
Constants <Field-symbols>
Variable
Variable
REPORT ZTEST.
DATA firstname TYPE STRING.
firstname = ‘John’.
Predefined ABAP Data Types
Type Description Initial Value Length
C Character Space 1 – 65535
I Integer 0 4 bytes
* Syntax
DATA var[(length)] [Type type] [Decimals number].
* Data Declaration
DATA: tmp(10) TYPE C,
tmp1 TYPE I,
tmp2(8) TYPE P DECIMALS 2 VALUE ‘1.50’.
DATA: tmp3(5) TYPE N,
tmp4.
Defining Variable with DATA Statement
* Data Declaration
DATA customerno LIKE customers-id.
DATA matnr LIKE mara-matnr.
* Data Declaration
TYPES tname(30) TYPE c.
DATA: customer_name TYPE tname,
firstname TYPE tname.
Value Assignment
* Value assignment
DATA: name1(30),
first_num TYPE I,
next_num TYPE I.
MOVE ‘XXXX’ TO name1.
MOVE 5 TO first_num.
COMPUTE next_num = first_num + 5.
name1 = ‘SAP’.
ADD 1 TO next_num.
Value Assignment
* Value assignment
DATA: tmp1 TYPE i,
tmp2 TYPE i.
tmp1 = tmp2 = 10.
ABAP Practice
Structure
* Syntax
DATA BEGIN OF <structure name>.
DATA field1.
DATA field2.
…
…
DATA END OF <structure name>.
Structure
wa
* Include Structure
DATA BEGIN OF wa.
INCLUDE STRUCTURE customers.
DATA tel(7).
DATA END OF wa.
Defining Structure
* LIKE option
DATA wa LIKE customers.
wa-id = 1.
wa-name = ‘John’.
WRITE: wa-id, wa-name.
ABAP Practice
Constants
* Constant variable
CONSTANTS max_no TYPE I VALUE 999.
DATA counter TYPE I VALUE max_no.
WRITE: max_no, counter.
Constants Using Example
* Constant variable
CONSTANTS ctext(11) TYPE C VALUE ‘Hello World’.
WRITE ctext.
WRITE ctext.
WRITE ctext.
WRITE ctext.
WRITE ctext.
System Fields
The system fields (structure syst) are filled by
the runtime environment. You can use them
to query the system status in an ABAP
program
You should access them only for reading
sy-datum = Current date of application server
syst-datum sy-uzeit = Current time of application server
sy-datlo = Current date of SAP GUI
sy-timlo = Current time of SAP GUI
sy-mandt = Current client logon
sy-subrc = Return value of ABAP statement
ABAP System Fields : Structure SYST (SE11)
DATE
* Fixed Length 8
* Include Representation ‘YYYYMMDD’
DATA today TYPE D.
today = sy-datum.
WRITE today.
today = ‘19991231’.
WRITE today.
TIME
* Fixed Length 6
* Format ‘HHMMSS’
DATA times TYPE T.
times = sy-uzeit.
WRITE times.
HHMMSS
MOVE Statement
Example:
DATA tmp type i value 9.
tmp = 10.
CLEAR tmp.
DATA wa like customers.
…
CLEAR wa.
ABAP Report : Program Structure
Report ztest.
*Data objects declaration
data ...
data begin of ...
*Program Logic(Data objects processing)
…
write ….
ABAP Chapter 2
Report Statement
Write & Format Statement
Flow Control in ABAP
Manipulating Character Data
Report Driven : Page Report (List Header)
List Processing
Report
Header
Report
Listing
(Body)
Report Statement
* Syntax
REPORT <report name>
[NO STANDARD PAGE HEADING]
[LINE-SIZE no of columns]
[LINE-COUNT no of lines[(no of footer)]].
Text Element
Title and Headers
List Header This is test program by Prapoj
Column Header
Column Column
#1 #2
Creating Lists
ABAP statement that create list
WRITE
SKIP
ULINE
The complete report list will appears
automatically at the end of the
processing block
List Buffer
Memory Space
TaskHandler
ABAP Processor
List Buffer
WRITE,SKIP,ULINE
Dynpro Processor
DB Interface
WRITE Statement
* Write data
WRITE ‘Hello World’.
WRITE: ‘OK’, ‘Test data’.
WRITE: /15(10) ‘ABCDEFGHIJKLMNOPQ’.
WRITE /20 ‘Test data’.
Breaking to a New Line
* Write data
WRITE: / ‘First Line’, ‘Data 1’,
/ ‘Second Line’, ‘Data 2’,
/(20) ‘Third Line’, ‘Data 3’,
/35 ‘Fourth Line’, ‘Data 4’.
sy-colno
Text Symbol
Report ztest.
Write: Text-001,
Text-002.
Text Element
Text Symbols
Text Symbol Text
001 Text 1
002 Text 2
Text Symbol
write: / Text-001.
write: / Text-001.
write: / Text-001.
write: / Text-001.
write: / Text-001.
Column Position
* Write Syntax
WRITE var [NO-ZERO]
[NO-SIGN]
[NO-GROUPING]
[NO-GAP]
[DECIMALS no of decimals]
Suppressing Blanks(NO-ZERO)
* No Zero
DATA: number(10) TYPE N VALUE 23.
WRITE: number, number NO-ZERO.
Suppressing Number(+ / -) Sign
* No Sign
DATA: v_integer TYPE I VALUE -1.
WRITE: v_integer, v_integer NO-SIGN.
NO-GROUPING
* No grouping
DATA: v_integer TYPE I VALUE 120000.
WRITE: v_integer, v_integer NO-GROUPING.
NO-GAP
* No gap
WRITE: ‘Hello’ NO-GAP, ‘World’.
DECIMALS
* Decimals
DATA: v_pack TYPE P DECIMALS 4
VALUE ‘1234.5678’.
WRITE: v_pack, v_pack DECIMALS 2.
Formatting Options
*Skip Statement
SKIP.
WRITE: ‘Hello World’, sy-linno.
SKIP.
WRITE: ‘Test 1’.
SKIP 5.
WRITE: ‘Test 2’.
SKIP TO LINE 20.
WRITE ‘This is line 20’.
Inserting Horizontal Lines(ULINE)
* Uline
WRITE: ‘Hello World’.
WRITE: /5(35) sy-uline, sy-vline.
ULINE /5(35).
ULINE.
WRITE: / ‘This is an underline’.
ULINE /(18).
Frame
uline: /(45).
write: /1 sy-vline, 'Column #1',
15 sy-vline, 'Column #2',
30 sy-vline, 'Column #3',
45 sy-vline.
uline: /(45).
Exercise I
sy-datum sy-uzeit
FORMAT Statement
FORMAT [INTENSIFIED]
[INTENSIFIED OFF]
[COLOR <color>]
[COLOR OFF]
[HOTSPOT ON]
[HOTSPOT OFF]
[RESET]
FORMAT Statement
FORMAT COLOR 1.
WRITE: / ‘Hello World’, ‘Test’ COLOR 7.
FORMAT COLOR OFF.
FORMAT COLOR
FORMAT COLOR col_heading. “color 1
FORMAT COLOR col_normal. “color 2
FORMAT COLOR col_total. “color 3
FORMAT COLOR col_key. “color 4
FORMAT COLOR col_positive. “color 5
FORMAT COLOR col_negative. “color 6
FORMAT COLOR col_group. “color 7
FORMAT COLOR col_background. “color off
Exercise I
Include Program
You can create a program with program type include program
in the program attribute
Include program do not have to have an introductory statement
During the syntax check and during program generation by the
ABAP compiler, the INCLUDE statement is replaced by the
source text of the defined include program
REPORT ztest1.
Include Program : ZINCLUDE1
INCLUDE zinclude1. Data tmp(10).
… Data tmp1 type i.
Data tmp2 type p. REPORT ztest2.
Data tmp3.
INCLUDE zinclude1.
…
Symbols and Icons
* Display Icon or Symbol in List
INCLUDE <LIST>.
WRITE: / ‘Phone :’, SYM_PHONE AS SYMBOL.
WRITE: / ‘Alarm :’, ICON_ALARM AS ICON.
WRITE: / ‘Green Light :’,
ICON_GREEN_LIGHT AS ICON HOTSPOT.
FORMAT HOTSPOT ON.
WRITE: / ‘Hello ABAP’, ’Hi!’.
FORMAT HOTSPOT OFF.
Flow Control in ABAP
Flow Control in ABAP
IF <Condition>.
<Statement Block>
ELSEIF <Condition>.
<Statement Block>
ELSEIF <Condition>.
<Statement Block>
ELSE.
<Statement Block>
ENDIF.
IF Statement
IF sy-mandt = ‘100’.
WRITE: / ‘This is Production Client’.
ELSEIF sy-mandt = ‘800’.
WRITE: / ‘This is Development Client’.
ELSE.
WRITE: / ‘This is Test Client’.
ENDIF.
CASE Statement
CASE <field>.
WHEN <value1>.
<Statement Block>
WHEN <value2>.
<Statement Block>
...
WHEN OTHERS.
<Statement Block>
ENDCASE.
CASE Statement
CASE sy-mandt.
WHEN ‘100’.
WRITE: / ‘Production Client’.
WHEN ‘800’.
WRITE: / ‘Development Client’.
WHEN OTHERS.
WRITE: / ‘Test Client’.
ENDCASE.
DO Statement
DO.
WRITE sy-index.
IF sy-index = 3.
EXIT.
ENDIF.
WRITE: sy-index.
ENDDO.
CONTINUE Statement
DO 5 TIMES.
IF sy-index = 3.
CONTINUE.
ENDIF.
WRITE: sy-index.
ENDDO.
CHECK Statement
DO 4 TIMES.
CHECK sy-index BETWEEN 2 AND 3.
WRITE: sy-index.
ENDDO.
WHILE Statement
Example :
9 / 2 = 4.5
9 DIV 2 = 4.0
9 MOD 2 = 1
SQRT( 2 ) = 1.41
2 ** 4 = 16
Character String Operator
T
if ‘AABB’ co ‘AB’. F
if ‘ABCD’ co ‘ABC’.
T
if ‘AXCZ’ ca ‘AB’.
F
if ‘ABCD’ ca ‘XYZ’.
T
if ‘ABCD’ cp ‘+B*’.
Manipulating Character Data
Manipulating Character Data
* Translate
DATA tmp(5) VALUE ‘abc ‘.
TRANSLATE tmp TO UPPER CASE.
TRANSLATE tmp TO LOWER CASE.
TRANSLATE tmp USING ‘ 0’.
TRANSLATE tmp USING ‘ 0aA’.
REPLACE
* Replace
DATA tmp(20) VALUE ‘I was a boy’.
REPLACE ‘was’ WITH ‘am’ INTO tmp.
IF sy-subrc = 0.
write ‘Replace OK’.
ELSE.
write ‘Cannot find data to be replaced’.
ENDIF.
Removing Spaces(CONDENSE)
* Condense
DATA: tmp(20) VALUE ‘I am a boy’.
CONDENSE tmp. I am a boy
CONDENSE tmp NO-GAPS.
Iamaboy
Concatenation String(CONCATENATE)
* Concatenate
DATA: tmp1(2) VALUE ‘AB’,
ABCDE
tmp2(3) VALUE ‘CDE’,
tmp3(10).
CONCATENATE tmp1 tmp2 INTO tmp3.
CONCATENATE tmp1 tmp2 INTO tmp3
SEPARATED BY ‘ ‘.
AB CDE
Split
* Split
DATA: name(30) value ‘David, John, Peter’,
one(10), two(10), three(30).
split name at ‘,’ into one two three.
Working with Date Variables
* Date
DATA today TYPE D.
today = sy-datum.
WRITE: today, sy-
datum+0(4)
‘Year :’ , today+0(4),
‘Month :’, today+4(2),
‘Day :’ , today+6(2).
WRITE … TO …
DATA: today TYPE D, tmp(10).
today = sy-datum.
tmp = today.
WRITE tmp.
WRITE today TO tmp.
WRITE tmp.
CLEAR today.
WRITE today NO-ZERO TO tmp.
WRITE tmp.
Invalid Date
TOP-OF-PAGE.
FORMAT COLOR 1.
WRITE: / 'Report Header'.
ULINE.
START-OF-SELECTION.
DO 100 TIMES.
WRITE: / sy-index.
ENDDO.
ABAP Program Structure
Report ztest.
*Data declaration
data ...
data begin of ...
*Top-of-Page event
top-of-page.
*End-of-Page event
end-of-page.
*Start-of-selection
Start-of-selection.
ABAP Practice
Exercise II
sy-datum sy-uzeit
sy-uname
sy-repid
ABAP Chapter 3
Open SQL
Internal Table
SAP System : 3 Tier Client/Server
SAP GUI SAP GUI SAP GUI Presentation
Server
SAP
Application
Server
DB Server
SAP SYSTEM (3 Tier Architecture)
SAP GUI SAP GUI
Presentation Layer
(Windows based)
SAP Instance
Application Layer
Dispatcher
M
(Windows Request SAP Buffer
Server/UNIX) Queue
(Shared Mem)
D D B V S E G
Oracle
Database Layer
Informix
(Windows Database Server DB2
Server/UNIX) MS SQL Server
SAP DB/MaxDB
SAP System : Dialog Processing
SAP GUI
Report zpsm1.
Request Tables customers.
List Select single * from
1 Generate
10
customers where id =
1.
Application Server
Send Request
Screen(List) Write: / customers-
name.
Store request
3 Dispatcher
to queue Send 2 SAP Buffer
Search for
Request List9 7
Check
free5WPProgram in Program
Queue Send request Execute
4 Program Buffer Table
to WP D D D … D ABAP
… stateme
8 6
nt
Database Server SQL Load&Gen
Request Program
Dialog Work Process Architecture
Dialog Work Process Local Memory
Memory Space
TaskHandler
ABAP Processor
List buffer
DYNPRO Processor
DB Interface
Result Set Memory
Database Server
Open SQL
SELECT ...
INSERT ...
UPDATE ...
DELETE ...
DB Interface
SAP Application Server
Result Set
~ 32 KB in length
Database Server
Data Data
Data Data
Data
Example Tables in DB
customers
spfli id name city
SQ 0110 20010226 75
Select Overview
Select <result> Which Columns?
From <table> Which Table?
Into <destination> Where to place?
Where <condition> Which Lines?
Select Statement
Select multiple records from database
SELECT * FROM customers.
…
ENDSELECT.
ABAP Processor
List buffer
DYNPRO Processor
DB Interface
Result Set
Database
SELECT Statement Working Steps
1. Transform open SQL to DB SQL and return result
set into result set work area
SELECT *
Reading Selected Column
Data: id like customers-id,
name like customers-name,
city like customers-city.
Select id name city
into (id, name, city)
from customers.
write: / id, name, city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Reading Selected Column
Data: begin of wa,
id like customers-id,
name like customers-name,
city like customers-city,
end of wa.
Select id name city
into wa
from customers.
write: / wa-id, wa-name , wa-city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Select Column : Example I
Reading Selected Column
Tables customers.
Select id name city
into (customers-id, customers-name, customers-city)
from customers.
write: / customers-id, customers-name, customers-city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Select Column : Example II
Corresponding Fields of...
Tables: customers.
Select id name city
into corresponding fields of customers
from customers.
Write: / customers-id, customers-name,
customers-city.
Endselect.
Select Statement : Special Topics
DB Count : SY-DBCNT
Tables customers.
Select * from customers.
write: / sy-dbcnt, customers-id, customers-name.
endselect.
if sy-subrc <> 0.
write: / ‘No Data found’.
else.
write: / sy-dbcnt, ‘Record found’.
endif.
SELECT … ORDER BY ...
Tables: spfli.
Select * from spfli
Order by cityfrom.
Write: / spfli-carrid, spfli-connid,
spfli-cityfrom.
Endselect.
SELECT … With Template
Tables customers.
Select * From customers
Where name Like ‘_r%’.
Write: / customers-id,customers-name.
Endselect.
Aggregate Functions
Data: maxdat like sflight-distance,
mindat like sflight-distance,
counter type I.
Select COUNT( * ) MIN( distance ) MAX( distance )
into (counter ,mindat, maxdat)
from spfli.
Write: / ‘Count :’ , counter,
/ ‘Min :’ , mindat,
/ ‘Max :’ , maxdat.
SELECT *
SELECT *
sflight
spfli
carrid connid fldate price
carrid connid cityfrom cityto distance
LH 0400 20010101 150
LH 0400 NY BK 100
LH 0400 20010110 145
LH 0402 BK NY 540
LH 0400 20010228 130
SQ 0110 SQ BK 250
SQ 0110 20010226 75
Tables Join
Question: Select carrid, connid and cityto from spfli
and fldate,price from sflight where carrid = ‘LH’
USR02
BNAME
USR21
PERSNUMBER
ADDRNUMBER
ADCP
ABAP : Outer Join
Open SQL – Outer Join
REPORT ZOUTERJOIN .
TABLES: ZCUSTOMERS,ZSALES.
SELECT A~NAME B~PROD_ID
INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID)
FROM ZCUSTOMERS AS A LEFT OUTER JOIN ZSALES AS B
ON A~ID = B~CUST_ID.
WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID.
ENDSELECT.
Single Result
Table
A~NAME B~PROD_ID
John A1
John A2
Peter
David X1
Micheal
Exercise
List customers name who do not buy any
product from company
Internal Table
Data Objects in ABAP
Memory Space
Variable Structure
Constants <Field-symbols>
INTERNAL TABLE
Flight (Structure)
Carrid Connid Date Price
Internal Table
Flight (Internal Table) Header Line
Carrid Connid Date Price
Structure
Data: Begin of flight,
carrid like sflight-carrid,
connid like sflight-connid,
date like sflight-fldate,
price like sflight-price.
Data: End of flight.
flight-carrid = ‘LH’.
Write: / flight-carrid.
INTERNAL TABLE
Data: begin of tab occurs 10,
carrid like sflight-carrid,
connid like sflight-connid,
fldate like sflight-fldate,
price like sflight-price.
Data end of tab.
USING ABAP DICTIONARY STRUCTURE
Tables sflight.
Data flight like sflight occurs 0 with header line.
Select * from sflight.
Move sflight to flight.
Append flight.
Endselect.
Standard Key of Internal Table
tab
Sort flight.
Sort flight by price fldate.
Sort flight by price ascending
fldate descending.
SORTING INTERNAL TABLE
Data tab like spfli occurs 0 with header line.
Select * from spfli into table tab.
Sort tab by cityfrom.
…
Loop at tab.
write: / tab-carrid, tab-connid,tab-cityfrom.
Endloop.
PROCESSING INTERNAL TABLE
...
Loop at flight.
Write: / flight-carrid, flight-connid.
Endloop.
...
Delete flight index 5.
Delete flight where carrid = ‘LH’.
flight-carrid = ‘XX’.
flight-price = 100.
…
Insert flight index 1.
DELETING INTERNAL TABLE
DATA flight LIKE sflight occurs 0 with header line.
Clear flight.
Refresh flight.
Free flight.
Total Record of Internal Table
INSERT
UPDATE
Database
MODIFY
DELETE
Insert (Table)
Tables customers.
customers-id = ‘999’.
customers-name = ‘Test’.
Insert customers.
if sy-subrc <> 0.
write: / ‘Data Already Exists’.
endif.
Update Statement
Tables customers.
Select single * from customers where id = 1.
If sy-subrc = 0.
customers-name = ‘John’.
update customers.
Endif. Update customers
set name = ‘John’
where id = 1.
Update Statement
Data wa like customers.
wa-id = ‘1’.
wa-name = ‘Test No 1’.
wa-city = ‘Bangkok’.
update customers from wa.
If sy-subrc <> 0.
write: / ‘Data not found’.
Endif.
Modify Statement
Tables customers.
customers-id = ‘1’.
customers-name = ‘Test No 1’.
Modify customers.
Deleting Database Table Entries
Tables customers.
customers-id = ‘1’.
Delete customers.
USR02
BNAME
USR21
PERSNUMBER
ADDRNUMBER
ADCP
Exercise : User Master
usr02-
usr02- trdat
bname adcp-
tel_numbe
r
Basic list
Navigating Between Lists
ABAP Editor
Detail list 2
Cancel
Detail list 1
3 | David | ....
ABAP Processor
List buffer
DYNPRO Processor Basic List
John
Peter HIDE area of list level 1
line Field name Value
David
DB Interface 1
2
customers-name
customers-name
John
Peter
Result Set Memory 3 customers-name David
Database Server
Database
At Line Selection(Hide Statement)
Basic List Detail List
At Line-selection
1
SY-LILLI = 2
4
HIDE area of list level 1 Customers Structure
2
line Field name Value 2 | Peter | ....
… … …
2
2
customers-id
customers-name
00000002
Peter
3
3 customers-id 00000003
…
At Line Selection
Tables: spfli,sflight. Basic List (SPFLI)
Start-of-selection.
Select * from spfli.
write : / spfli-carrid, spfli-connid, spfli-cityto.
Hide : spfli-carrid, spfli-connid.
Endselect.
At Line-selection.
select * from sflight where carrid = spfli-carrid Detail List (SFLIGHT)
Detail List
zcustomers-id
zcustomers
zcustomers-name
zsales-cust_id
zsales-qty
zsales-prod_id
zsales
Hide Statement (Report Heading)
List Buffer
Tables customers.
Customers Name
Top-of-page. -----------------------------
write: / ‘Customers Name’. John
Peter
uline. David
Start-of-Selection.
Select * from customers.
write : / customers-name.
Hide customers-name.
HIDE area of list level 1
Endselect.
At line-selection. line Field name Value
3 customers-name John
Write: ‘You Choose :’ , customers-name. 4 customers-name Peter
5 customers-name David
At Line Selection(Hide Statement)
Basic List Detail List
Customers Name
-----------------------------
You choose : Peter
John 5
Peter
David
At Line-selection
1
SY-LILLI = 4
4
HIDE area of list level 1 Customers Structure
2
line Field name Value
3 | Peter | ....
3 customers-name John
4 customers-name Peter 3
5 customers-name David
Invalid Line Selection
…
TOP-OF-PAGE.
…
Hide: spfli-carrid, spfli-connid.
…
Endselect.
Clear: spfli-carrid,spfli-connid.
At Line-selection.
Select * From sflight
Where carrid = spfli-carrid and
connid = spfli-connid.
Write: / spfli-carrid, spfli-connid ,sflight-fldate.
Endselect.
Clear: spfli-carrid,spfli-connid.
Page Heading
Start-of-selection.
Write: ‘Basic List’.
At Line-selection.
Write: ‘Detail List’.
Top-of-page.
Write: ‘Header-Basic List’.
Top-of-page During Line-selection.
Write: ‘Header-Detail List’.
Detail List Page Heading
...
top-of-page during line-selection.
case sy-lsind.
when 1.
write: / ‘Detail List Header #1’.
when 2.
write: / ‘Detail List Header #2’.
endcase.
.....
.....
...
Column Selection
data: fieldname(30).
...
start-of-selection.
select * from spfli.
write: / spfli-carrid,15 spfli-connid, 25 spfli-cityto.
hide: spfli-carrid,spfli-connid.
endselect.
at line-selection.
case sy-lsind.
when 1.
get cursor field fieldname.
case fieldname.
when 'SPFLI-CARRID'.
select single * from scarr where carrid = spfli-carrid.
if sy-subrc = 0.
write: / spfli-carrid,scarr-carrname.
endif.
when 'SPFLI-CONNID'.
select * from sflight where carrid = spfli-carrid and
connid = spfli-connid.
...
Column Selection : Value
...
data: fieldname(30),fieldvalue(30).
...
start-of-selection.
select * from spfli.
write: / spfli-carrid,15 spfli-connid, 25 spfli-cityto.
hide: spfli-carrid,spfli-connid.
endselect.
at line-selection.
case sy-lsind.
when 1.
get cursor field fieldname value fieldvalue.
case fieldname.
when 'SPFLI-CARRID'.
select single * from scarr where carrid = spfli-carrid.
if sy-subrc = 0.
write: / spfli-carrid,scarr-carrname.
endif.
when 'SPFLI-CONNID'.
...
Creating List in Modal Dialog Box
...
at line-selection.
window starting at 10 10
ending at 65 20.
select * from sflight
where carrid = spfli-carrid and
connid = spfli-connid.
write: / sflight-carrid,sflight-connid,sflight-fldate.
endselect.
...
Exercise
Basic List
zsales
zsales-qty
zsales-cust_id zsales-prod_id
zsales-sale_id
zproducts
Detail List zsalereps
zproducts-on_hand
zproducts-p_id
zsalereps-sale_id zsalereps-name
Drill-Down 2 Levels Example
Exercise
SCARR
Drill-down Level 1
SPFLI
Drill-down Level 2
SFLIGHT
Drill-down Level 3
SBOOK
GUI Interface (User Interface)
GUI Interface
SET PF-STATUS …. => GUI Status
SET TITLEBAR …. => GUI Title
AT USER-COMMAND Event
REPORT ztest.
DATA exctab(10) occurs 0 with header line.
....
START-OF-SELECTION.
SET PF-STATUS ‘0100’.
...
exctab = ‘LIST’. APPEND exctab.
exctab = ‘TEST’. APPEND exctab.
...
AT USER-COMMAND.
CASE SY-UCOMM.
WHEN ‘LIST’.
SET PF-STATUS ‘0100’ excluding exctab.
GUI TITLE
...
SET TITLEBAR ‘0100’.
ABAP Practice
Selection Screen
Selection Screen
PARAMETERS Statement
SELECT-OPTIONS Statement
parameters
Parameters
PARAMETERS: tmp1(10) TYPE C,
tmp2(10) TYPE C lower case,
tmp3 LIKE sy-datum default sy-datum,
tmp4 TYPE D default ‘19991231’,
tmp5 TYPE i,
tmp6(5) TYPE N.
Parameters Example
Tables customers.
Parameters pid like customers-id.
START-OF-SELECTION.
select single * from customers where id = pid.
if sy-subrc = 0.
write: / customers-name.
else.
write: / ‘No data found’.
endif.
Selection-Text
By default , the System displays the name of the selection as text on
the Selection Screen
You Should use the Text element/Selection texts function to store a
text line for each Selection Criterion.
Check box
Tables customers.
Select-options sname for customers-name.
START-OF-SELECTION.
Select *
from customers
Where name in sname.
Internal Structure of Select-options
sname
Sign Option Low High
Internal Structure of Select-options
Field Value .
Sign I = Include
E = Exclude
Option BT = Between
CP = Contains Pattern
EQ = Equal
GT = Greater Than
LT = Less Than
GE = Grater Than or Equal
LE = Less Than or Equal
NE = Not Equal
Internal Structure of Select-options
sname
Sign Option Low High
I CP M*
I EQ Smith
I BT A John
SELECT-OPTIONS
sname
Sign Option Low High Select * from customers
Transform
where (name like ‘M%’) or
I CP M* (name = ‘Smith’) or
I EQ Smith (name between ‘A’ and ‘John’).
I BT A John
a1
Copy
f1
Memory Space(Subroutine)
Call by Value
Data: a1,a2.
a1 = ‘A’.
a2 = ‘A’.
PERFORM routine1 USING a1 a2.
.…...
FORM routine1 USING VALUE(f1) VALUE(f2).
f1 = ‘X’.
f2 = ‘X’.
ENDFORM.
Call by Reference
a3
Address Passing
f3
Memory Space(Subroutine)
Call by Reference
Data: a3.
a3 = ‘A’.
PERFORM routine2 USING a3.
.…...
FORM routine2 USING f3.
f3 = ‘X’.
ENDFORM.
Call by Value and Result
a4
Copy Copy
f4
Memory Space(Subroutine)
Call by Value and Result
Data: a4,a5.
a4 = ‘A’.
a5 = ‘A’.
PERFORM routine3 USING a4 a5.
.…...
FORM routine3 CHANGING VALUE(f4) f5. “f5 : call by reference
f4 = ‘X’.
f5 = ‘X’.
ENDFORM.
Passing Structure as Parameters
TABLES sflight.
SELECT * FROM sflight.
PERFORM subproc USING sflight.
ENDSELECT.
FORM subproc USING rec LIKE sflight.
WRITE: / rec-carrid.
ENDFORM.
Passing Internal Table as Parameters
REPORT RSAAA10F.
TABLES: sflight.
…..
PERFORM cal(RSAAA10B).
REPORT RSAAA10B.
TABLES sflight.
…..
FORM cal.
…..
ENDFORM.
EXIT Statement
DATA tmp TYPE I.
tmp = 4.
PERFORM a.
WRITE tmp.
FORM a.
EXIT.
tmp = 99.
ENDFORM.
STOP Statement
DATA tmp TYPE I.
START-OF-SELECTION.
tmp = 4.
PERFORM a.
WRITE tmp.
END-OF-SELECTION.
tmp = 0.
write tmp.
FORM a.
STOP. “go to END-OF-SELECTION
tmp = 99.
ENDFORM.
Function Module
Function Module
Function Group
Function Library
- Admin
- Import/Export Parameter
- Source Code
- Main Program
- Documentation
Function Group
When you create a function module, you must assign it to
function group
The function group is the main program in which a function
module is embedded
The function group is a program type F,and not executable
The entire function group is loaded in a program the first
time that you call a function module that belongs to it
Function Group
is a container for function modules
When a function module is called,the entire function group
is loaded into the session of the program
Function group is used to define global data for function
modules
A DATA statement in the global memory of a function group
is shared by all the function modules that belong to that
function group
Function Group : SE37
Function Group : SE80
Function Module
is a code that can be called from any ABAP
program,therefore making it a globally accessible object
ABAP program pass data to function module from import
parameters or internal tables
Function module receives data from a program,process the
information in its own code, and then sends back
information in the export parameters or internal tables
Function Module : SE37
Function Module
Function Module : Source Code
FUNCTION Z_FMTEST.
result = number1 ** number2.
ENDFUNCTION.
Program Example I
REPORT ztest.
PARAMETERS: no1 TYPE I,
no2 TYPE I.
DATA result TYPE I.
START-OF-SELECTION.
CALL FUNCTION ‘Z_FMTEST’
EXPORTING
number1 = no1
number2 = no2
IMPORTING
result = result.
write: / result.
Exercise : Function Module
ABAP Program
Function
Module
?
EXCEPTIONS
Function Module
Function Z_CAL01.
if number1 > 9 and number2 > 9.
raise invalidnumber.
else.
result = number1 ** number2.
endif.
ENDFUNCTION.
Example II : Exceptions
REPORT ztest.
PARAMETERS: no1 TYPE I,
no2 TYPE I.
DATA result TYPE I.
START-OF-SELECTION.
CALL FUNCTION ‘Z_CAL01’
EXPORTING
number1 = no1
number2 = no2
IMPORTING
result = result
EXCEPTIONS
invalidnumber = 1.
IF sy-subrc <> 0.
write: / ‘Please enter number < 10’.
ELSE.
write: / result.
ENDIF.
Exercise : Exceptions
ABAP Program
Function
Module
?
EXCEPTIONS VS AT SELECTION-SCREEN
REPORT ztest.
FUNCTION Z_CAL01.
Parameters: no1 type i,
if number1 > 9 and number2 > 9.
no2 type i.
raise invalidnumber.
At selection-screen
else.
result = number1 ** number2.
VS if no1 > 9 and no2 > 9.
message e000(38) with ‘Please enter no < 10’.
endif.
endif.
ENDFUNCTION.
START-OF-SELECTION.
CALL FUNCTION ‘Z_CAL01’.
…..
Optional
Function Module
ABAP Program
Structure in Function Module
Example : Structure
Example : Structure
Internal Table in Function Module
Example : Internal Table
Example : Internal Table
Function Group
Function Module : Z_FMTEST
•Syntax
Catch system-exceptions <error type> = <n>.
<ABAP statement – generate runtime error> .
Endcatch.
if sy-subrc = <n>.
...
endif.
CATCH Error Type
•Error class
•Catch system-exceptions conversion_errors = 1.
•Single error
•Catch system-exceptions convt_no_number = 1.
•All catchable runtime error
•Catch system-exceptions others = 1.
CATCH Statement
Report ztest.
Data num type I.
Catch system-exceptions conversion_errors = 1.”others
Move ‘abc’ to num. “runtime error: convt_no_number
Endcatch.
If sy-subrc = 1.
Write: / ‘Assign wrong data type to variable: num’.
Endif.
CATCH Statement
Report ztest.
Data num type I.
Catch system-exceptions others = 1.
Move ‘abc’ to num.
Endcatch.
If sy-subrc = 1.
Write: / ‘Assign wrong data type to variable: num’.
Endif.
CATCH Statement
Report ztest.
PARAMETERS: NUM1 TYPE I,
NUM2 TYPE I.
DATA RESULT TYPE I.
START-OF-SELECTION.
CATCH SYSTEM-EXCEPTIONS COMPUTE_INT_ZERODIVIDE = 1.
RESULT = NUM1 / NUM2.
ENDCATCH.
IF SY-SUBRC = 1.
WRITE: /'Divide by zero'.
ELSE.
WRITE: / RESULT.
ENDIF.
CATCH Statement
Report ztest.
PARAMETERS: NUM1 TYPE I,
NUM2 TYPE I.
DATA RESULT TYPE I.
START-OF-SELECTION.
CATCH SYSTEM-EXCEPTIONS OTHERS = 1.
RESULT = NUM1 / NUM2.
ENDCATCH.
IF SY-SUBRC = 1.
WRITE: /'Divide by zero'.
ELSE.
WRITE: / RESULT.
ENDIF.
CATCH in Function Module
Function Z_CAL. Function Z_CAL.
if number1 > 9 and number2 > 9. CATCH SYSTEM-EXCEPTIONS OTHERS = 1.
raise invalidnumber. RESULT = NUMBER1 ** NUMBER2.
ENDCATCH.
else.
IF SY-SUBRC = 1.
result = number1 ** number2. RAISE invalidnumber.
endif. ENDIF.
ENDFUNCTION. ENDFUNCTION.
ABAP Practice
ABAP Chapter 6
Message
Debugging
File Transfer
Type Group
Message in ABAP
User Messages
If user has entered inconsistent values,you output a dialog message
with MESSAGE statement
Dialog messages are stored in table T100 (Transaction : SE91)
report ztest.
….
AT SELECTION-SCREEN.
…
message e000(38) with ‘----’ ‘---’ ‘---’ ‘---’.
…
Message Type
Syntax
Message [ A<nnn> ](message class) with <field1> <field2> …
E, W, I, S
Messages Type - A(Abend)
Message A000(38)...
Program Start
Selection
Screen
A Message Exit
Messages Type - E(Error)
Message E000(38) ...
Program Start
Selection
New input
Screen
Require
E Message
Messages Type - W(Warning)
Message W000(38)...
Program Start
W Message
Enter
List
Messages Type - I(Information)
Message I000(38)...
Program Start
Selection
Screen
I Message
Enter
List
Messages Type - S(Success)
Message S000(38)...
Program Start
Selection
Screen
List
(Next Screen)
Dynamic Message
Report ztest1.
Parameters today like sy-datum.
At selection-screen.
if today <> sy-datum.
message e000(38) with ‘Please enter today :’ sy-datum.
endif.
Start-of-selection.
Write: / ‘Today is :’, today.
Debugging
Debugging Mode
Debugging Mode : Internal Table
Debugging Mode : Internal Table
Debugging Mode : Watchpoint
Watchpoint : SAP ECC 6.0
How to Set Debugging Mode
If you want to test transaction,enter /h in the command
field,press ENTER and execute the transaction
Set breakpoints in the program
Utilities->Breakpoints->Set
Uses BREAK-POINT statement
ABAP Practice
File Transfer
File Transfer (Application Server)
Read/Write File
Close File
File Transfer
* Opening a file
Open dataset filename for output in text mode
encoding default message msg_txt.
If sy-subrc <> 0.
Write: ‘File cannot be opened .Reason :’,msg_txt.
else.
File Transfer
* Opening a file
Open dataset filename for appending in text mode
encoding default message msg_txt.
If sy-subrc <> 0.
Write: ‘File cannot be opened .Reason :’,msg_txt.
else.
...
Reading Data from OS File
* Reading data from a file
Parameters filename(128) default ‘customersdata.txt’ lower case.
Data msg_txt(50).
Data all_customers like customers occurs 0 with header line.
Start-of-selection.
Open dataset filename for input in text mode
encoding default message msg_txt.
If sy-subrc <> 0.
Write: ‘File cannot be opened .Reason :’,msg_txt.
else.
Reading Data from OS File
Do.
Read dataset filename into all_customers.
if sy-subrc <> 0.
Exit.
endif.
Append all_customers.
Enddo.
Close dataset filename.
Endif.
Deleting OS File
* Upload data to PC
parameters filename like rlgrap-filename
default ‘c:\customers.txt’.
Data all_customers like customers occurs 0 with header line.
START-OF-SELECTION.
Upload Data from PC
CALL FUNCTION ‘UPLOAD’
Exporting
filename = filename
Tables
data_tab = all_customers
Exceptions
file_open_error = 1
…
others = 5.
Upload Data from PC
Case sy-subrc.
When 1.
Write: ‘Error when file opened’.
When 2.
Write: ‘Error during data transfer’.
…
When 0.
Insert customers from table all_customers.
…
Endcase.
Upload/Download Data in Background
usr02-trdat
usr02-bname
adcp-tel_number
Exercise IV : Drill-Down Report