Anda di halaman 1dari 76

ABAP Training

Simple Reports
ABAP Training Reports 2

What Is a Report

Executable program with a three-stage function:


Data input  data processing  data output.
Reports: reading and processing of data using data from database
tables, without actually changing it.
ABAP Training Reports 3

Report Programming - Overview

 Task of a report
 Evaluate and display data from the database

 Reports are stand alone programs


 Reports are controlled by events
 Program events are always triggered externally

 React to events by

 Programming the corresponding processing block

 Ignore the event by not programming the corresponding block


ABAP Training Reports 4

Report Programming - Overview

 Reports can use logical databases


 Special ABAP programs which define

 Database tables that can be used by the program

 Relationship between these tables (via primary key - foreign

key links)
 Database accesses

 Selection screen options

 SAP provides logical databases for each application area

 Users can also define logical databases


ABAP Training Reports 5

Report Programming - Overview

 Report processing is controlled by events


 Processing the selection screen

 Presenting the selection screen, validating user inputs

 Reading the database

 Logical database events

 Evaluating data and creating lists

 Outputting the list

 Interactive list events are triggered by user mouse clicks

 If the program contains no event keywords


 Entire program belongs to the standard event which is triggered

after selection screen processing


ABAP Training Reports 6

Purpose of Report

 Reports are used by management as a tool to monitor the day


to day activity in the organization.
 Basically reports are used to provide data to the user in a
desired format.
ABAP Training Reports 7

Structure of Report

 Report statement and report driven list headings


 Processing blocks
 Control level statements
 Subroutines
 Includes
ABAP Training Reports 8

Concept of Flow Control in ABAP

 Internal control
 Branching (IF, CASE)

 Looping (DO, WHILE)

 External control
 Events

 Runtime events

 User events

 System does not necessarily process the statement blocks in

the same order as they appear in the source code


ABAP Training Reports 9

Concept of Flow Control in ABAP

event keyword external control


event begin
processing block
event end
internal control

event keyword external control

processing block
internal control

…...
ABAP Training Reports 10

Internal Control Statements

 If IF statements can be
IF logexpr1.
nested to any level.
Processing1.
ELSEIF logexpr2.
All IF statements must
Processing2. be terminated with
… ENDIF in the same
Else. processing block.
processingN.
ENDIF.
ABAP Training Reports 11

Internal Control Statements

 Case  F is a variable name


CASE f.  F11..fnn may be variables or
WHEN f11 or f12 …or f1n. literals
Processing f1.  CASE statements can be
WHEN f21 or f22 …or f2n. nested
Processing f2.  No further WHENs can
follow WHEN OTHERS
When ….
When others.
Processing others.
ENDCASE.
ABAP Training Reports 12

Internal Control Statements

 Iteration
 Do … ENDDO.

 Loop … ENDLOOP.

 While … ENDWHILE.

 Select … ENDSELECT.

 Breaking out of a loop


 Exit, stop, reject

 Selective loop processing


 Continue, check
ABAP Training Reports 13

Do ... Enddo

 Variations
 Do ... ENDDO.

 DO n TIMES ... ENDDO.

 Additions
 VARYING f FROM f1 NEXT f2.

 Basic form, DO...Enddo must contain a loop termination


statement
 Exit, reject, stop

 Can process/skip current loop pass


 Check, continue
ABAP Training Reports 14

Do ... Enddo
Sum = 0.
Do
Sum = 0.
Sum = sum + sy-index.
Do 10 times
Counter = counter + 1.
Sum = sum + sy-index.
Write sum.
Write sum.
If counter > 10.
Enddo.
Exit.
endif.
Enddo.
ABAP Training Reports 15

Exit

 Always leaves the current unit and the program continues after that
unit
 Context sensitive
 Only leaves inner structure if structures are nested

 Can be used in
 Unconditional loops (DO...Enddo)

 Conditional loops (WHILE...Endwhile)

 Subroutine (FORM...ENDFORM)

 Function (FUNCTION...ENDFUNCTION)

 Reading entries of a database table (SELECT...ENDSELECT)

 Reading lines of an internal table (LOOP...Endloop)


ABAP Training Reports 16

Stop
 Cancels all data selection
 No further tables are read
 Followed immediately by the END-OF-SELECTION event
IF logexp.
Stop.
ENDIF.
ABAP Training Reports 17

Reject
 Used in logical database processing
 Stops processing the current database table line and resumes with the
next line of the table on the same hierarchy level
IF logexp.
Reject.
ENDIF.
ABAP Training Reports 18

Continue

 Terminates the current loop pass and returns processing to the


beginning of the next loop pass (if there is one)
 Excludes lines where logexp is true from the list

IF logexp.
Continue.
ENDIF.
ABAP Training Reports 19

Check

 CHECK logexp
 Evaluates the subsequent logical expression

 Expression is true

 Processing continues with the next statement

 Expression is false

 Terminates the current loop pass and goes back to the start

of the next loop pass (if there is one)


 Negative CHECK in a subroutine terminates the subroutine
 Negative CHECK not in loop or subroutine terminates the event
ABAP Training Reports 20

While … Endwhile

 Variations
 WHILE logexp ... ENDWHILE.

 Additions
 VARYING f FROM f1 NEXT f2.

 Performs loop body statements while logexp is true


 Pretest loop

 Can contain other nested loop structures


ABAP Training Reports 21

Select … ENDSELECT

 Used for processing lines of data returned directly from a


database table
 Variations
SELECT < * | field list > [target]
FROM <table>
[ Where … ]
[ Group by … ]
[ Order by … ]
ENDSELECT.
ABAP Training Reports 22

LOOP AT <itab>… ENDLOOP


 Used for processing internal tables
 Variations
 LOOP at itab. … Endloop.
 LOOP at itab INTO wa. … Endloop.
 Additions
 From n1

 To n2

 Where logexp
ABAP Training Reports 23

The ABAP Processor

 ABAP program is a collection of processing blocks which are


executed in response to specific events
 Processing blocks do not have to occur in any specific order
 At program start time the system starts a process, (the ABAP

processor)
 Calls these modules
 Controls the external program flow

In response to event keywords written in the program


ABAP Training Reports 24

Events
INITIALIZATION

AT SELECTION-SCREEN OUTPUT

AT SELECTION-SCREEN
TOP-OF-PAGE

END-OF-PAGE
START-OF-SELECTION

GET Events

END-OF-SELECTION
} TOP-OF-PAGE DURING
LINE-SELECTION
AT LINE-SELECTION
AT USER-COMMAND

Interactive Events
} AT PF<nn>
ABAP Training Reports 25

Events & Their Keywords

 Runtime events
 Selection screen events

 Initialization

 At selection-screen

 Events after selection criteria has been processed

 Start-of-selection

 End-of-selection
ABAP Training Reports 26

Events & Their Keywords

 During list processing


 Top-of-page
first line of new page
 End-of-page
last line of current page
 During display of (interactive)
list
 At line-selection user double clicks on a report line
 At user-command user clicks on command button or presses
 AT pf<nn>
function key
ABAP Training Reports 27

Run-Time Events

 Initialization
 Processed before the presentation of the selection screen

 Can be used to initialise values in the selection screen or to

assign values to any parameters that appear on the selection


screen
ABAP Training Reports 28

Run-Time Events

 At selection-screen
 Processing block is started after the user has specified all

the criteria in the selection screen


 If an error message is displayed from this processing block

the system displays the selection screen again and all input
fields can be changed
ABAP Training Reports 29

Run-Time Events

 Start-of-selection
 Processing block that is executed after processing the

selection screen and before accessing database tables using


a logical database
 Use this processing block to set the value of internal fields

 All statements that are not attached to an event keyword or

in a subroutine are processed in this event


ABAP Training Reports 30

Run-Time Events

 End-of-selection
 Processing block executed after the system has read and

processed all database table records


ABAP Training Reports 31

Top Of Page

Under this event we can code the header of the page which is to be printed
on every page of the report.
The TOP-OF-PAGE event occurs as soon as the system starts processing a
new page of a list. The system processes the statements following TOP-
OF-PAGE before outputting the first line on a new page
ABAP Training Reports 32

End Of Page

This event occurs to define a page footer,if, while processing a


list page, the system reaches the lines reserved for the page
footer, or if the RESERVE statement triggers a page break.
ABAP Training Reports 33

Control Level Statements

1. At new <f> - beginning of a group of lines with the same contents in


the field <f> and in the fields left of <f>
2. At end of <f> - end of a group of lines with the same contents in the
field <f> and in the fields left of <f>
3. At first – first line of internal table
4. At last – last line of internal table
In a loop which processes an internal table, you can use special
control structures for control break processing. All these structures
begin with AT and end with Endat. The sequence of statements which
lies between them is then executed if a control break occurs.
ABAP Training Reports 34

Control Level Statements

If you have sorted an extract dataset by the fields <f1>, <f2>,


..., the processing of the control levels should be written
between the other control statements as follows:
Loop.
At first.... ENDAT.
AT NEW <f1>....... ENDAT.
AT NEW <f2>....... ENDAT.
...
<Single line processing without control statement>
...
at end of <f2>.... ENDAT.
AT END OF <f1>.... ENDAT.
At last..... ENDAT.
Endloop.
ABAP Training Reports 35

Control Level Statements - SUM


The ABAP statement SUM can be used only within a loop.
When used in an AT – ENDAT block, the system calculates the totals for
the numeric fields of all lines in the current line group and writes them
to the corresponding fields of the work area.
E.G.
Loop at ITAB into line.
At end of col1.
Sum.
Write: / line-col1, line-col2.
ENDAT.
Endloop.
ABAP Training Reports 36

Report Statement

 REPORT keyword is used along with the name of the report as the
starting line of the report to introduce the program as a report the SAP
system.
 Some additions which are used with this statement. Are as follows -:
NO STANDARD PAGE HEADING – this will suppress the page heading
which is taken as a default heading by SAP.
LINE-COUNT – to determine the page length of an output list.
REPORT <rep> LINE-COUNT <len>[(<n>)].
ABAP Training Reports 37

Report Statement

<Len> shows the length of the total report length.


<N> will reserve n no of lines as space for the page footer.

LINE-SIZE – this specify the width of the report.


MESSAGE-ID – this contains the ID where messages to be used are
stored.
ABAP Training Reports 38

SELECT Overview

SELECT <result> which columns?

FROM <table> which table?

INTO <target> where to?

WHERE <condition> which entries?

GROUP BY <fields> how are they grouped?

ORDER BY <fields> how are they arranged?


ABAP Training Reports 39

Single or Multiple Entries

SELECT <result> FROM <table> ... ENDSELECT.


SELECT SINGLE <result> FROM <table> WHERE..

 Select accesses all records select * into wa


matching the selection criteria
from spfli
 Allows line by line

processing using where carrid = ‘LH’.


SELECT...ENDSELECT endselect.
 Select single returns first row
that matches the selection
criteria select single * into wa
from spfli
where carrid = ‘LH’.
ABAP Training Reports 40

SELECT Clause

Select *
SELECT <f1> <f2> ... <fn>

• SELECT *
• returns all columns of the database table
• SELECT <f1> <f2> ... <fn>
• <f> can be either fields or aggregate functions
• MIN, MAX, AVG, SUM, COUNT
• NB. No commas between fields in field list
ABAP Training Reports 41

SELECT <result> INTO ...


 <wa>
 <result> fields placed left to right into components of <wa>
 (<f1>, <f2>, ... <fn>)
 <result> fields placed left to right into fields <f1> <f2> ... <fn>
 TABLE <itab>
 selected data not processed line by line but all at once
 CORRESPONDING FIELDS OF <wa>
 <result> fields placed into fields in <wa> which have the corresponding
name
 CORRESPONDING FIELDS OF TABLE <itab>
 as above except selected data is placed in <itab> all at once
ABAP Training Reports 42

WHERE

 <table field> <operator> <value>


 <table field> BETWEEN <value> AND <value2>
 <table field> LIKE <pattern>
 <table field> CONTAINS <pattern>
 <table field> IN ( <value1>, <value2> ... )
 <table field> IN <seltab>
ABAP Training Reports 43

<operator>

EQ =
GE >= =>
LE <= =<

NE <> ><
GT >
LT <
ABAP Training Reports 44

<pattern>
 special characters ‘_’ and ‘%’
 ‘_’
 represents a single character

 ‘%’
 represents any sequence of characters

Select * from SPFLI


Where Cityfrom Like ‘S%’.
ABAP Training Reports 45

GROUP BY <f1> <f2> ...


 combines groups of entries into single entries
 a group consists of entries that have identical <f1> <f2> ...
 SELECT list
 must contain <f1> <f2> ...

 fields other than <f1> <f2>... must be aggregate functions , eg.

SUM( LUGGWEIGHT )
ABAP Training Reports 46

ORDER BY

 determines sort order of selected result table


 ...ORDER BY PRIMARY KEY
 sorts entries by primary key of the database table
 ...ORDER BY <f1> [ASCENDING | DESCENDING]
<f2> [ASCENDING | DESCENDING] ...
 default sort order is ascending
Select Carrid Connid CityFrom Cityto
From SPFLI
Order by Carrid Connid CityTo Descending.
ABAP Training Reports 47

Message & Message Class

 Messages are grouped together into an object called


message class where individual messages are identified by
an unique number.
 Transaction code : SE91
ABAP Training Reports 48

Different Message Types


A Termination The message appears in a dialog box, and the program terminates. When the
user has confirmed the message, control returns to the next-highest area menu.

E Error Depending on the program context, an error dialog appears or the program
terminates.
I Information The message appears in a dialog box. Once the user has confirmed the
message, the program continues immediately after the MESSAGE statement.

S Status The program continues normally after the MESSAGE statement, and the
message is displayed in the status bar of the next screen.

W Warning Depending on the program context, an error dialog appears or the program
terminates.
X Exit No message is displayed, and the program terminates with a short dump.
Message type X allows you to force a program termination.
ABAP Training Reports 49

Messages in Reports

 messages are stored in table T100

 REPORT <reportname> MESSAGE-ID xx.


 xx represents an application area

 xx must not be enclosed in quotation marks


ABAP Training Reports 50

Messages in Reports
 MESSAGE xnnn.
 x is one of Displayed Outcome
 I Info window resumes processing
 W Warning status line terminates current list level
 E Error status line terminates current list level
 A Abend window termination after message
 X Exit status line immediate termination
 S Success status line resumes processing
 nnn is the message number

 system variables
 SY-MSGID message ID
 SY-MSGTY message type
 SY-MSGNO message number
ABAP Training Reports 51

Messages in Reports

MESSAGE xnnn [WITH <f1>…<f4>].

& characters serve as placeholders in a message


200 Level &1 not allowed here

WITH option allows contents of fields < fi > to replace the &i
according to the value of I
MESSAGE E200 WITH SY-LSIND.
ABAP Training Reports 52

Messages in Reports

MESSAGE ID <id> TYPE <x>


NUMBER <nnn> [WITH <f1> … <f4>].

allows a message to be specified dynamically at runtime


do not need the MESSAGE-ID xx option in the REPORT
statement
<id> is the message application area
<x> is the message type (I, W, E, A, X, S)
<nnn> is the message number
ABAP Training Reports 53

Write Statements

 Write : / Var1 As Checkbox.


Write : / Var2 As Symbol.

Write : / Var3 As Icon.

Write : / Var4 As Line.


ABAP Training Reports 54

Several Options In Write Statements

 NO-ZERO
 NO-SIGN
 DD/MM/YY
 LEFT-JUSTIFIED
 RIGHT-JUSTIFIED
 CENTERED
 USING EDIT MASK mask
 USING NO EDIT MASK
 DECIMALS d
ABAP Training Reports 55

Several Options In Write Statements


 No-gap

 No grouping

 CURRENCY w

 EXPONENT e

 ROUND r

 TIME ZONE tz
ABAP Training Reports 56

Formatting of Report

 Report can be printed using various statements like -:


Write
Write:[/][<pos>][(<len>)] <f>
E.G.
WRITE: 'one',
/ ' ',
/ 'two'.
ABAP Training Reports 57

Output Format of Predefined Types

c field length left-justified

n field length left-justified

i 11 right-justified

f 22 right-justified

p 2*fieldlength (+1) right-justified

d 8 left-justified
t 6 left-justified

x 2*field length left-justified


ABAP Training Reports 58

Positioning Output

WRITE AT [/] [pos] [(len)] <f> .

 ‘/’ Denotes new line


 <Pos>
 Horizontal position

 Always output at that position regardless of whether there is

enough space or other fields overwritten


 (<Len>)
 Output length

 If <len> is too short

 Numeric fields truncated (left), asterisk displayed

 All others truncated (right) with no indication


ABAP Training Reports 59

Some Examples of Pos (Len)

data: word(16) value '0123456789ABCDEF',


col type i value 5,
len type i value 10.
write /5(12) word. 0123456789AB
write at col(len) word. 0123456789
write /(3) 12345. *45
write: /5(8) word, 25(2)word. 01234567 01
ABAP Training Reports 60

Formatting Options – All Data Types

WRITE <f> <option1> <option2> ....

• LEFT-JUSTIFIED Output is left justified


• RIGHT-JUSTIFIED Output is right justified
• CENTERED Output is centered
• UNDER <g> Output starts under field <g>
• NO-GAP Blank after field <f> is omitted
• USING EDIT MASK <m> Specifies a format template <m>
• USING NO EDIT MASK Deactivates a template
• NO-ZERO All zero field replaced by blanks
• COLOR <c> Color of output specified by <c>
ABAP Training Reports 61

Formatting Options - Examples

data c(10) value ‘text’.


write: / ‘Left Just :’, c left-justified. Left Just : text
write: / ‘Right Just:’, c right-justified. Right Just: text
write: / ‘Centered :’, c centered. Centered : text
write: / ‘aaa ‘, ‘bbb ‘, ‘ccc ‘, ‘ddd’. aaa bbb ccc ddd
write: / c under ‘bbb ‘ text
write: / c using edit mask ‘_:_%_8_!’. t:e%x8t!
ABAP Training Reports 62

Formatting Options - Numeric Fields

• NO-SIGN Leading sign is not output


• DECIMALS <d> Output has <d> digits after decimal
• EXPONENT <e> Type f fields exponent defined in <e>
• ROUND <r> Type p fields are divided by 10**(r)
and then rounded
• CURRENCY <c> According to definition of <c> in table
TCURX
• UNIT <u> Number of decimal places is fixed
according to the definition of <u> in
table T006 for type p fields
ABAP Training Reports 63

Formatting Options - Date Fields

WRITE <f>
• DD/MM/YY
• MM/DD/YY
• DD/MM/YYYY
• MM/DD/YYYY
• DDMMYY
• MMDDYY
• YYMMDD
ABAP Training Reports 64

Formatting Options - Examples

Data X type p value ‘-12345.678’,


F type f value ‘-12345.678’,
D type d value ‘19980323’.
Write: / X decimals 2. 12,346.000-
Write: / F decimals 2. -1.235e+04
Write: / X exponent 2. 12,346-
Write: / F exponent 2. -123.4567800000000e+02
Write: / D DD/MM/YY 23/03/99
ABAP Training Reports 65

Format

FORMAT <option1> [ON|OFF] <option2> [ON|OFF] ...


 Formatting <optioni> applies to all subsequent output until turned off
using the OFF option

FORMAT <option1> = <var1> <option2> = <var2> ...


• <vari> interpreted as a number
• should be of type i
• <vari> = 0 has same effect as OFF option
• <vari> > 0 has same effect as ON option
• with COLOR option acts like corresponding colour
number
ABAP Training Reports 66

Format

 formatting options used in a WRITE statement overwrite the


corresponding settings of a previously issued FORMAT statement for
the current output
 for each new event the system resets all formatting options to their
default values
 all options have a default value of OFF except the INTENSIFIED

option
 FORMAT RESET
 sets all formatting options to OFF in one go
ABAP Training Reports 67

COLOURS IN LISTS

FORMAT COLOR = <c>


INTENSIFIED = <int>
INVERSE =<inv>
 COLOR sets colour of line background
 INVERSE influences the foreground colour
 INVERSE ON sets the foreground to the selected COLOR option
 INTENSIFIED determines the colour palette for the line background
ABAP Training Reports 68

LIST COLOUR OPTIONS


<n> <c> Colour Intended For
OFF or COL_BACKGROUND 0 depends on GUI background

1 or COL_HEADING 1 grey-blue headings

2 or COL_NORMAL 2 light grey list bodies

3 or COL_TOTAL 3 yellow totals

4 or COL_KEY 4 blue-green key columns

5 or COL_POSITIVE 5 green pos threshold value

6 or COL_NEGATIVE 6 red neg threshold value

7 or COL_GROUP 7 violet group levels


ABAP Training Reports 69

Color Examples

Data: I type i value 0, col(15).


While I < 8.
Case I.
When 0. Col = ‘COL_BACKGROUND’.
When 1. Col = ‘COL_HEADING.
When ...
endcase.
Format intensified color = I.
Write: / (4) I, at 7 sy-vline, col, sy-vline,
Col intensified OFF, sy-vline, col inverse.
I = i + 1.
Endwhile.
ABAP Training Reports 70

Output of Color: Example


ABAP Training Reports 71

On Screen Lines & Blank Lines

 Horizontal Lines
LINE [AT [/] [<pos>] [(len)] ].
WRITE [AT [/] [<pos>] [(len)] ] SY-ULINE.
 Vertical Lines
WRITE [AT [/] [<pos>] ] SY-VLINE.
WRITE [AT [/] [<pos>] ] ‘|’.
 Blank Lines
SKIP [<n>]
SKIP TO LINE <n>.
ABAP Training Reports 72

Creating Blank Lines

SET BLANK LINES [ON | OFF]


 OFF

 system suppresses blank lines created by WRITE

SKIP [<n>]
 if <n> greater than lines remaining on page

 produces page footer, throws to new page

 at the beginning of a new page

 ignored except if page created by NEW-PAGE or if page is

the first of a list level


 last output statement of last list page

 ignored
ABAP Training Reports 73

Page and Line Breaks


NEW-PAGE. this statement ends the current page. All other output appears on a new page.
But this statement does not trigger the END-OF-PAGE event.
...NO-TITLE new page without standard list header
...WITH-TITLE new page with standard list header
...NO-HEADING new page without column headers
...WITH-HEADING new page with column headers
...LINE-COUNT lin new page with lin lines per page
...LINE-SIZE col new page with col columns per line
NEW-LINE. this statement positions the next output in a new line
ABAP Training Reports 74

Formatting Options - RESERVE

RESERVE statement triggers a page break if less than <n> free lines are
left on the current list page between the last output and the page footer.
Before starting a new page, the system processes the END-OF-PAGE
event.
RESERVE only takes effect if output is written to the subsequent page
(the system will not generate an empty page).
ABAP Training Reports 75

System Fields for Lists

 SY-PAGNO
 Number of current page of current list

 SY-LINNI
 Number of current line of current list

 SY-COLNO
 Number of column where cursor is positioned in the current

list
ABAP Training Reports 76

System Fields for Lists

 SY-title
 Title that appears in the title bar of the display window

 Can be manipulated by

 Maintaining text elements

 Using SET TITLEBAR <titlestring>.

 SY-SROWS
 Current number of lines in display window

 SY-SCOLS
 Current number of columns in display window

Anda mungkin juga menyukai