Sunteți pe pagina 1din 29

Chapter 11 Reporting

Overview
Logical databases
Events
Interactive reporting
User messages
ABAP/4 Query

SAP AG
Chapter 11 Objectives

Distinguishing between an ABAP/4 Report and


an ABAP/4 query
Understanding logical databases
Understanding the interaction between logical
databases and reports
Events in ABAP/4 program s
Understanding interactive reporting
How to write a simple interactive report
How to output dialog m essages
How to create an ABAP/4 query

SAP AG
Overview of Reporting

REPORT

QUERY

SAP AG

An ABAP/4 report is a program which reads data from the database, sorts the data according to certain
criteria and outputs the data in form of a list on the screen.
To generate a list, you can use create a query with ABAP/4 Query or write your own ABAP/4 report.
ABAP/4 Query is a user-friendly tool which allows you to define the layout of a list without any
programming knowledge.
When programming an ABAP/4 report, you can use all the facilities available in the programming
language to evaluate complex data.
Queries and reports both use logical databases to retrieve data.
Reading Data

ABAP/4 Dictionary

1 2
REPORT ... . logical database
TABLES: ... . TABLES: ... .
SELECT... . SELECT... .
SELECT ... . SELECT ... .
... .
processing
ENDSELECT.
ENDSELECT. ENDSELECT.
ENDSELECT.

REPORT REPORT
TABLES: TABLES:
GET GET
processing processing
GET
processing
SAP AG

Basically, there are two ways you can read data:


1. Program the read in the relevant program using the SELECT statement (report-driven data
retrieval).For
performance reasons, it is better to use SELECT ... FOR ALL ENTRIES IN <itab> WHERE
<cond>
than nested SELECT statements.
2. Use a special read program - known as a logical database - to read the required data and make it
available
to your program in the correct sequence. In this case, you only have to program the statements
which
process the data.
You can use report-driven data retrieval and logical databases side by side in the same report.
In a report, the connection between the report processing and the logical database lies in the event key
word GET. This event is triggered by the logical database after the database has read a particular data
record and stored it in the report’s work area. The report itself does not read the record because it is
already available for processing.
Report and Data Program

SPFLI
REPORT SAPDBF1S
. DEFINING DATABASE F1S. Report RSBBB01B.
. ...
.
SELECT * FROM SPFLI... .
. .
. .
.
ENDSELECT. GET SPFLI.
... W rite: SPFLI-CARRID,
...

SAP AG

When you start a report which is assigned to a logical database, the ABAP/4 runtime system instructs
the logical database to evaluate the user’s selection criteria. The logical database reads a data record
and, in the appropriate GET event, makes the table entry read with SELECT available to the report.
The data record is processed in the GET event and control then returns to the logical database.
Events I
Events used with
logical databases

INITIALIZATION.
AT SELECTION-SCREEN O UTPUT.
OUTPUT.
AT SELECTION-SCREEN.
START-OF-SELECTION.
GET .... .
END-OF-SELECTION.

Events used when


creating lists

TOP-O F-PAGE.
TOP-OF-PAGE.
END-OF-PAGE.

SAP AG

ABAP/4 is an event-driven programming language. Reports which are linked to logical databases are
not processed sequentially. Besides the GET event, the report can use a number of other event key
words to perform processing. These events are triggered by the ABAP/4 processor which determines
the sequence of events. If processing is scheduled in the logical database for these events, it is
executed before the event in the report is triggered. The processing block defined for a event key word
is executed procedurally and concludes when the next event occurs.
The START-OF-SELECTION event is executed before the logical database starts reading the data.
This event is set by default.
Events II

Events used in
interactive reporting

AT LINE-SELECTION.
AT PFxx.
AT USER-COMM AND.
USER-COMMAND.

TOP-OF-PAGE DURING LINE-SELECTION.

SAP AG

You can define interactive events in ABAP/4 reports. These events are triggered when the user presses
a particular function key or pushbutton. They allow for the creation of secondary lists which display
detailed information.
Interactive Reporting: Overview

Secondary Windows
lists

Interactive
Reporting

Calling a Calling a report


transaction

SAP AG

Interactive reporting allows you to create secondary lists and windows. To call these and display them
on the screen, you use function keys. You can thus distribute all the information returned by the report
across several screens, gradually outputting more and more detailed information.
Using function keys, the user can also branch to:
a transaction
another report
Line Selection I

REPORT RSAAA11A.
Basic list
TABLES: SPFLI. CAR ID from to departure
SELECT-OPTIONS: SCARRID ... . LH 0400 Frankfurt New York 10:10:00
LH 0402 Frankfurt New York 13:30:00
START-OF-SELECTION. ...
SELECT * FROM SPFLI LH 3577 Rome Frankfurt 07:05:00
WHERE ... .
WRITE: /SPFLI-CARRID,
SPFLI-CONNID ... .
HIDE: SPFLI-CARRID, SPFLI-CONNID. Secondary list
ENDSELECT.

AT LINE-SELECTION. You have chosen the connection


SKIP TO LINE 12. LH 0402.
WRITE: TEXT-001, SPFLI-CARRID,
SPFLI-CONNID, TEXT-002.
*D: TEXT-001: 'You have chosen'
*D: TEXT-002: 'the connection.'

SAP AG

To enable the user to choose a line in a list and display detailed information for this line in a
secondary list, you can use the AT LINE-SELECTION event.
This event is processed when the user selects a valid line in the list with the cursor and activates the
function key assigned to the PICK function in the interface definition. If there is no interface defined,
the event is triggered by pressing F2 or by double-clicking.
The HIDE Area
HIDE <field>.

HIDE area
Carr. ID From To Depart
LH 0400 Frankfurt New York 10:10:00 Line SPFLI-CARRID SPFLI-CONNID
LH 0402 Frankfurt New York 13:30:00
... 1 LH 0400
LH 3577 Rom e Frankfurt 07:05:00
2 LH 0402
...
11 LH 3577
Choose

REPORT RSAAA11A
...

START-OF-SELECTION.
SELECT * FROM SPFLI WHERE ... .
WRITE: SPFLI-CARRID, SPFLI-CONNID, ... .
HIDE: SPFLI-CARRID, SPFLI-CONNID.
ENDSELECT.
SAP AG

By default, the data of the basic list is not available when you are in the secondary list.
However, the HIDE statement allows you to store field contents for a particular list line in a special
area. For each field, the system stores the field name and the value.
The HIDE command must immediately follow the WRITE statement since the information is stored
for each output line. You can also use the HIDE area to store fields which do not appear in the list.
Line Selection II

Hide area

CAR ID from to Depart Line SPFLI-CARRID SPFLI-CONNID


LH 0400 Frankfurt New York 10:10:00
LH 0402 Frankfurt New York 13:30:00 1 LH 0400
...
LH 3577 Rom e Frankfurt 07:05:00
2 LH 0402
...
11 LH 3577
Choose

LH 0402 Frankfurt New York 13:30:00 LH 0402 ?


SY-LISEL SPFLI-CARRID SPFLI-CONNID SPFLI-DEPTIME

REPORT RSAAA11A.

AT LINE-SELECTION.
WRITE: TEXT-001, SPFLI-CARRID,
SPFLI-CONNID, TEXT-002.

SAP AG

As soon as the user selects a line by double-clicking or positioning the cursor, and triggers an
interactive event (in this case AT LINE-SELECTION) by pressing a function key or pushbutton, the
system passes the values stored for this line in the HIDE area to the corresponding work area.
The contents of the selected line are also stored in the system field SY-LISEL.
Valid Line Selection
header lines Hide area

"SPFLI-CARRID" LH
"SPFLI-CONNID" 0400
"SPFLI-CARRID" LH
footer lines "SPFLI-CONNID" 3577

REPORT RSAAA11A.
START-OF-SELECTION.
SELECT * FROM SPFLI WHERE ... .
WRITE: SPFLI-CARRID, SPFLI-CONNID, ... .
HIDE: SPFLI-CARRID, SPFLI-CONNID.
ENDSELECT.
END-OF-SELECTION.
CLEAR SPFLI-CARRID.
AT LINE-SELECTION.
CHECK NOT SPFLI-CARRID IS INITIAL.
...
CLEAR SPFLI-CARRID.

SAP AG

It is up to you to program that the system does not generate a secondary list at invalid line selection.
There are several possibilities to recognize invalid line selection from the program.
In above programming example, each field whose contents is stored in the HIDE area is set to the
initial value. If the user selects an invalid line, the system does not find a corresponding value in the
HIDE area. With a negative result of the CHECK statement, the current processing block is
terminated. The system does not generate a secondary list.
User Messages: Overview

REPORT RSDEMO00 MESSAGE-ID AT.


.
.
.
MESSAGE E027.

Table T100
S ID No. Message text

D AT 001 Record &1 &2 does not exist


. .. ... ...
D AT 011 Error when updating
. .. ... ...
D AT 027 Not authorized

E027 Not authorized

SAP AG

If the user has entered inconsistent values or has selected values without having the appropriate
authorization, you output a dialog message with the key word MESSAGE.
Dialog messages are stored in table T100. They are classified by language, two-character ID and three-
character name.
Messages can contain up to four variables (&1, &2, &3 and &4). If you want to output & in a message
it does not represent a variable, you must enter it twice. For example: '
This is a message with an &&'
.
In the long text of the message, you can use &v1&, &v2&, &v3& and &v4& instead of the
corresponding variables.
You can create your own messages using an ID which starts either with Y (head office) or Z (branch).
Syntax of the MESSAGE Statement

REPORT <reportname> MESSAGE-ID <message-id>.

.
.
.
Annn
Xnnn
MESSAGE Ennn WITH <field1> <field2> <field3> <field4>.
W nnn
Innn
Snnn

SAP AG

You output messages with the MESSAGE statement. Here, the language used for table T100 is the
logon language. You specifiy the message ID after the MESSAGE-ID parameter of the REPORT
statement. This message ID applies to the entire report. You specify the message number in the
MESSAGE statement.
Immediately before the three-digit number, you specify the message type which determines how the
user is meant to react to the message (see next page).
After the WITH parameter, you enter the values for the variables (up to four). You can also specify
fields or literals here. The i-th field replaces the variable &i in the message. If the variables in the
message are only represented by & or $, these placeholders are supplied with the values of the fields in
the MESSAGE statement according to their position.
Unlike the message ID in the REPORT statement, you can enter a message ID in the MESSAGE
statement yourself by specifying the ID immediately after the message number in parentheses.
However, this ID applies only to the current message. For example: MESSAGE E004(UD).
If you want to send a dynamic message, you can use the following syntax:

MESSAGE ID <mid> TYPE <mtype> NUMBER <mno.> WITH <field1> <field2> <field3>
<field4>.
The system fields SY-MSGID, SY-MSGTY and SY-MSGNO contain the message ID, the message
type and the message number respectively, while the system fields SY-MSGV1 through SY-MSGV4
contain the fields for the placeholders.
Classification of Dialog Messages
Abend Error W arning Inform ation Success

Function Function Function Function Function


call call call call call

New input possible


New input required
Restart
Selection Selection Selection Selection Selection
screen screen screen screen screen

A message
E message W m es sage I message
X message

List List List with


S message

SAP AG

There are six message types:


A Abend Processing terminates and the user has to restart the transaction.
X Exit As message type A, but with short dump MESSAGE_TYPE_X
E Error Processing is interrupted and the user must enter a new value.
W Warning Processing is interrupted and the user can either enter a new value (effect as
with E
messages) or just press ENTER to confirm the values (effect as with I messages).
I Information Processing is interrupted and the system resumes processing after the user has
pressed
ENTER.
S Success Information is displayed on the next screen.
To make the selection screen ready for input again when E and W messages occur, you output these
messages in the AT SELECTION-SCREEN event. In one of the subsequent events (e.g. START-OF-
SELECTION, GET or END-OF-SELECTION), these message types behave like A messages.
Example of Dialog Message
REPORT RSAAA11B MESSAGE-ID AT.
TABLES SPFLI.
PARAMETERS: P_CARRID LIKE SPFLI-CARRID.

AT SELECTION-SCREEN.
AUTHORITY-CHECK OBJEKT 'S_CARRID'
ID 'CARRID' FIELD P_CARRID
ID 'ACTVT' FIELD '03'.
IF SY-SUBRC NE 0.
MESSAGE E045 WITH P_CARRID.
ENDIF.

START-OF-SELECTION.
SELECT * FROM SPFLI WHERE CARRID = P_CARRID.
...
ENDSELECT.

SAP AG

If the user specifies an airline carrier as a parameter, an authorization check is performed in the event
AT SELECTION-SCREEN (in contrast to Chapter 9). As a result, the input has to be corrected before
the reports starts the data selection process.
Advantages:
There is no negative authorization check for any the records read from the database.
Authorization checks only have to be performed once for each airline carrier.
Users learn whether or not they are authorized at the selection stage.
Conventional Reporting vs. ABAP/4 Query
Logical database list
e.g. F1S
Report
(ABAP/4)

Reporting
ABAP/4 Query

report
report generator
generator
functional
functional area
area

list
list definition
definition
(ABAP/4
(ABAP/4 Query)
Query)

Creating
Creating lists
lists with
with

Report:
Report: ABAP/4
ABAP/4 statements
statements
ABAP/4
ABAP/4 Query: Description
Query: Description of
of desired
desired result
result
SAP AG

When you create a list with a report, the data is usually retrieved via a logical database, processed by
the report and then output as a list.
When you create a query, you first assign a functional area which is basically a special view of a
logical database. You choose the fields you require from this functional area and define the formatting
and type of processing.
When the query is started, an internal report generator creates a report in accordance with the list
definition and then reads, processes and outputs the data.
ABAP/4 Query

Functional areas

User groups
Choose logical database

Group fields together


Assign users
to functional areas
Define auxiliary fields

Query

Define lists
Basic list
Statistic
Ranked list

Start queries

SAP AG

ABAP/4 Query allows you to generate non-standard ABAP/4 reports in R/3. It is primarily intended
for users with no programming knowledge in ABAP/4.
The queries are assigned to a user group. Any user belonging to this user group can access these
queries.
A functional area can be assigned to several user groups.
Several functional areas can be assigned to one user group.
The queries of a user group are shared by all the users in that user group.
A query is always created with reference to a particular functional area. This functional area must be
assigned to the user group.
In general, you can access all the queries assigned to your user group.
Chapter 11 Summary

Data can be read using logical databases and processed


by reports. Logical databases facilitate the selection
screen design process.
In interactive reporting, you can provide the user with
additional inform ation using secondary lists or screens.
To store data to be displayed in the secondary list, you
use the HIDE statement. ABAP/4 Query is used to
generate ABAP/4 reports.
ABAP/4 Query is intended for users with no progamming
knowledge.

SAP AG
Exercises Chapter 11: Reporting

1. Name of your report: ZBCA##111


##: Group number

Task:
Create a list of all flight connections for each airline carrier.
Output the following fields:
SPFLI-CARRID, SPFLI-CONNID, SPFLI-CITYFROM,
SPFLI-CITYTO, SPFLI-DEPTIME, SPFLI-ARRTIME.
Define a selection screen on which the user can enter
selections for the airline carrier (SPFLI-CARRID) and the
connection ID (SPFLI-CONNID). Read the data from table
SPFLI in accordance with this selection.
When the user selects a line, the system should display a
secondary showing the flights (table SFLIGHT) available for
the flight connection concerned (see example list). Output the
selected line as the header of the secondary list. Calculate and
output the number of vacant seats for each flight. Make sure
that the program can handle invalid line selection.
a) Example list

AA 0017 NEW YORK SAN FRANCISCO 13:30:00 16:31:00

Date Price Seats


max. occupied
free

29.10.1995 666,67 USD 660 10 650

11.11.1995 666,67 USD 660 20 640

16.11.1995 666,67 USD 660 38 622


19.11.1995 666,67 USD 660 38 622
29.11.1995 666,67 USD 660 10 622

19.12.1995 666,67 USD 660 20 640

21.12.1995 666,67 USD 660 38 622


2. Name of your report: ZBCA##K2
##: Group number
Development class: $TMP (local)

Task:
Copy your solution to Chapter 11, Exercise 1 or the example
solution and output an error message when no data is found
for the specified selection values.
Hint:
Read the table SPFLI in the event AT SELECTION-SCREEN
and store the records in an internal table to avoid repeated
database accesses.
Use message number 037 from the AT work area.
Solutions Chapter 11: Reporting

1. REPORT RSAAA111.

TABLES: SPFLI, SFLIGHT.

* Report-specific selection criterita for airline carrier and


* connection ID

SELECT-OPTIONS: SELCARR FOR SPFLI-CARRID,

SELCONN FOR SPFLI-CONNID.

* Auxiliary field: Number of free seats

DATA: SFREE LIKE SFLIGHT-SEATSMAX.

START-OF-SELECTION.

* Read data corresponding to selection criteria and output list

SELECT * FROM SPFLI

WHERE CARRID IN SELCARR AND CONNID IN SELCONN.

WRITE: / SPFLI-CARRID,
SPFLI-CONNID,
SPFLI-CITYFROM,
SPFLI-CITYTO,
SPFLI-DEPTIME,
SPFLI-ARRTIME.
HIDE: SPFLI-CARRID, SPFLI-CONNID.

ENDSELECT.
END-OF-SELECTION.

CLEAR SPFLI-CARRID. "initialization

AT LINE-SELECTION.

* Interactive event, create secondary list

CHECK NOT SPFLI-CARRID IS INITIAL.

* Display secondary list only if line selection is valid

WRITE SY-LISEL.

WRITE: /5 TEXT-001, 29 TEXT-002, 49 TEXT-003,

/ TEXT-004 UNDER TEXT-003, 60 TEXT-005, 70 TEXT-006.

* D: TEXT-001: Date

* D: TEXT-002: Price

* D: TEXT-003: Seats

* D: TEXT-004: maximum

* D: TEXT-005: occupied TEXT-006: free


ULINE.

SELECT * FROM SFLIGHT WHERE CARRID EQ SPFLI-CARRID

AND CONNID EQ SPFLI-CONNID.

* Read data for secondary list using hidden key fields and output
* list

WRITE: / 5 SFLIGHT-FLDATE,

SFLIGHT-PRICE,

SFLIGHT-CURRENCY.

SFREE = SFLIGHT-SEATSMAX - SFLGIHT-SEATSOCC. "calculate free


seats

WRITE: SFLIGHT-SEATSMAX,

SFLIGHT-SEATSOCC,

SFREE.

ENDSELECT.

CLEAR SPFLI-CARRID. "initialization


2. REPORT RSAA116 MESSAGE-ID AT.
TABLES: SPFLI, SFLIGHT, SCARR.

* Report-specific selection criteria for airline carrier and


* connection ID

SELECT-OPTIONS: SELCARR FOR SPFLI-CARRID,

SELCONN FOR SPFLI-CONNID.

* Auxilary field: Number of free seats

DATA: SFREE LIKE SFLIGHT-SEATSMAX.

* Internal table to avoid reading from database a second time

DATA: BEGIN OF TAB_FLI OCCURS 1.

INCLUDE STRUCTURE SPFLI.

DATA END OF TAB_FLI.

AT SELECTION-SCREEN

* Function module to check authorization for airline carrier

CALL FUNCTION ´CARRID_AUTHORITY_CHECK´

TABLES

SELTAB_CARRID = SELCARR.
* Read data corresponding to selection criteria

SELECT * FROM SPFLI INTO TABLE_FLI

WHERE CARRID IN SELCARR AND CONNID IN SELCONN.

IF SY-SUBRC NE 0.

MESSAGE E037.

* There is no flight connection for this combination (carrier,


* destinations)

ENDIF.

START-OF-SELECTION.

LOOP AT TAB_FLI.

FORMAT COLOR COL_KEY. "color for key columns

WRITE: / TAB_FLI-CARRID,

TAB_FLI-CONNID.

FOR;AT COLOR COL_NORMAL. "color for list

WRITE: TAB_FLI-CITYFROM,

TAB_FLI-CITYTO,

TAB_FLI-DEPTIME,

TAB_FLI-ARRTIME.

HIDE: TAB_FLI-CARRID, TAB_FLI-CONNID. "hide information

ENDLOOP.

END-OF-SELECTION.

CLEAR TAB_FLI-CARRID. "initialization


AT LINE-SELECTION.

* Interactive event, create secondary list

CHECK NOT TAB_FLI-CARRID IS INITIAL.

SET PF-STATUS ´ ´.

* Display secondary list only if line selection is valid

FORMAT COLOR COL_HEADING. WRITE SY-LISEL.

WRITE: /5 TEXT-001, 29 TEXT-002, 49 TEXT-003.

WRITE: / TEXT-004 UNDER TEXT-003, 60 TEXT-005, 70 TEXT-006.

* D: text-001 Date

* D: text-002 Price

* D: text-003 Seats

* D: text-004 max.

* D: TEXT-005 OCCUPIED

* D: text-006 free

ULINE.

SELECT * FROM SFLIGHT WHERE CARRID EQ TAB_FLI-CARRID

AND CONNID EQ TAB_FLI-CONNID.

* Read data for seocdary list using hidden key fields and output
* list

FORMAT COLOR COL_NORMAL.

WRITE: /5 SFLIGHT-FLDATE,

SFLIGHT-PRICE,

SFIGHT-CURRENCY.

SFREE = SFLIGHT-SEATSMAX - SFLIGHT-SEATSOCC.

WRITE: SFLIGHT-SEATSMAX,

SFLIGHT-SEATSOCC,

SFREE.

ENSELECT.

CLEAR TAB_FLI-CARRID. "initialization

S-ar putea să vă placă și