3 SAP ABAP Reports Part 1
3 SAP ABAP Reports Part 1
3 SAP ABAP Reports Part 1
2
Data and Types Statement
TYPES Statement
The TYPES statement introduces user-defined data types. As with standard data types,
you can use them when creating data objects and when assigning types to formal
parameters and field symbols. User-defined data types are an essential component of the
ABAP/4 type concept. Data types do not occupy the memory. It defines the technical
attributes of an object.
DATA Statement
DATA statement is used to define the data objects. Objects are instances of types, and do
occupy their own memory space. Data objects are the physical units with which ABAP
statements work at runtime. Each ABAP data object has a set of technical attributes, which
are fully defined at all times when an ABAP program is running. The technical attributes of
a data object are its length, number of decimal places, and data type. ABAP statements
work with the contents of data objects and interpret them according to their data type.
3
DATA Definitions
• DATA Statement
– -All variables used within the ABAP/4 program must be declared with DATA
statements
• Example:
4
TYPES Statement
TABLES Statement
– TABLES <dbtab>.
• This statement creates a structure with the same data type and the same name as a
database table, a view, or a structure from the ABAP Dictionary.
• Creates a Workspace or buffer for the table records. This buffer can then be accessed
following data retrieval. The buffer name is the same as the table name.
• Before Release 4.0, it was necessary to declare a database table <dbtab> to an ABAP
program using the statement TABLES <dbtab> before you could access the table using
Open SQL statements. This restriction no longer applies to Open SQL statements.
• Example :
– TABLES : KNA1. “Customer Master
– TABLES : LFA1, MARA. “Vendor & Material Master
6
Selection Screens
• The Selection screen is the part of a report program that you can design for interactive
input of field values and selection criteria.
• The selection screen is always processed directly after a report program is started.
• The user can enter field values and selection criteria on this screen.
• In an ABAP/4 program, you use the following statements to design selection screens:
7
Selection Screen Elements
• Parameters cannot have data type F. The data type F is not supported in the Selection
Screen
• To suppress the display use NO-DISPLAY option
– PARAMTER P_TELNO NO-DISPLAY.
• To make a parameter a required input field, the OBLIGATORY option of the
PARAMETERS statement is used.
• REPORT ztraining.PARAMETERS: value TYPE i DEFAULT 100,name LIKE sy-uname
DEFAULT sy-uname ,date LIKE sy-datum DEFAULT sy-datum.
8
PARAMETERS
PARAMETERS Variations
PARAMETERS <p> ...... DEFAULT <f> ......
PARAMETERS <p> ...... NO-DISPLAY ......
PARAMETERS <p> ...... LOWER CASE ......
PARAMETERS <p> ...... OBLIGATORY ......
PARAMETERS <p> ...... AS CHECKBOX ......
PARAMETERS <p> ...... RADIOBUTTON GROUP <radi>......
PARAMETERS <p> ...... MEMORY ID <pid>......
PARAMETERS <p> ...... MATCHCODE OBJECT <obj> ......
PARAMETERS <p> ...... MODIF ID <key> ......
PARAMETERS <p> TYPE <type> ... VALUE CHECK ...
PARAMETERS <p> LIKE (field)….
PARAMETERS <p> ... VISIBLE LENGTH <len> ...
9
PARAMETERS contd.
Example :
Output Screen :
10
PARAMETERS contd.
PARAMETERS <p> ...... LOWER CASE ......
Example :
PARAMETERS: FIELD1(10),
FIELD2(10) LOWER CASE.
WRITE: FIELD1, FIELD2.
Output Screen :
11
PARAMETERS contd.
Example :
Output Screen :
12
Selection Screen
• To define a checkbox for parameter input, the option AS CHECKBOX of the
PARAMETERS statement is used.
• Syntax : PARAMETERS <p>...... AS CHECKBOX.
• To define groups of radio buttons for parameter input, the RADIOBUTTON GROUP option
of the PARAMETERS statement is used.
• Syntax : PARAMETERS <p>...... RADIOBUTTON GROUP <radi>.
• Example:
• PARAMETERS: yes AS CHECKBOX,
no AS CHECKBOX DEFAULT'X'
13
PARAMETERS
Example :
Output Screen :
14
PARAMETERS contd.
Example :
Output Screen :
15
PARAMETERS contd.
Example :
Output Screen :
16
PARAMETERS contd.
17
PARAMETERS contd.
Example :
Output Screen :
18
PARAMETERS contd.
Example :
DATA NAME(20) TYPE C.
INITIALIZATION.
NAME = 'SPFLI-CARRID'.
19
PARAMETERS contd.
START-OF-SELECTION.
WRITE: / 'P1:', p1,
/ 'P2:', p2,
/ 'P3:', p3.
20
Selection Criteria
• If you use the SELECT-OPTIONS statement, the report user can enter the selection
criteria on the selection screen.
• During its definition, you normally attach a selection criterion to a specific column of a
database table that must be declared in the program.
• If you want to program complex selections, selection criteria, stored in special internal
tables, are preferable to the PARAMETERS statement because they relieve you from
coding them in lengthy WHERE conditions.
21
SELECT-OPTIONS
• The field <f> cannot have data type F. The data type F is not supported on the
selection screen.
• Their main purpose is to transfer the selection criteria directly to database tables
using the WHERE clause of Open SQL statements.
22
Selection Table
• It is an internal table(with name <seltab>) with a header line. Its line structure is a field
string of four components.
– SIGN :
– OPTION :
• If HIGH is empty, you can use EQ, NE, GT, LE, LT,CP, and NP.
• Type is the same as the column type of the database table, to which the selection
criterion is attached.
23
SELECT-OPTIONS Variations
24
SELECT-OPTIONS
Example :
Output Screen :
25
SELECT-OPTIONS contd.
Example :
Output Screen :
26
SELECT-OPTIONS contd.
Example :
REPORT demo_sel_screen_select_no_int1.
Output Screen :
27
Using Selection Tables
28
Formatting Selection Screen
• Blank Lines
• To produce blank lines on the selection screen, use the SKIP option with the
SELECTION-SCREEN statement.
29
Program Selections
• SELECT-OPTIONS Statement
• SELECT-OPTIONS <Name> FOR <Table field>
NO EXTENSION
OBLIGATORY
LOWER CASE
• SELECT-OPTIONS allows specification of multiple values and ranges. This can only be
declared for fields within tables defined in the TABLES statement.
• Example
• SELECT-OPTIONS: s_kunnr FOR kna1-kunnr.
30
Specifying Blank Lines
31
Elements on a Single Line
• To position a set of parameters or comments on a single line on the selection screen, the
elements are declared in a block enclosed by the following two statements:
SELECTION-SCREEN BEGIN OF LINE
.…
SELECTION-SCREEN END OF LINE.
• Example
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(10) text-001. “Text Symbol for Title
PARAMETERS: p1(3), p2(5), p3(1).
SELECTION-SCREEN END OF LINE
32
Positioning in the Selection Screen
• To position the next parameter or comment on the selection screen, the POSITION option
is used.
• SELECTION-SCREEN POSITION <pos>.
• To create a logical block of elements on the selection screen, mark the beginning of the
block with the BEGIN OF BLOCK option of the SELECTION-SCREEN statement, then
define the individual elements and mark the end of the block with the END OF BLOCK
option as shown below:
33
Blocking Selection Screen
34
Formatting Selection Screen
• You can create up to five pushbuttons in the application toolbar on the selection screen.
These buttons are also automatically connected to function keys.
• <i> must be between 1 and 5. You must specify the text to appear on
• When the user clicks this button, FC0<i> is entered in the field SSCRFIELDS-UCOMM,
which can be checked during the event AT SELECTION-SCREEN.
35
Formatting Selection Screen contd.
TABLES sscrfields.
PARAMETERS: p_carrid TYPE s_carr_id,
p_cityfr TYPE s_from_cit.
SELECTION-SCREEN: FUNCTION KEY 1,
FUNCTION KEY 2.
INITIALIZATION.
sscrfields-functxt_01 = 'LH'.
sscrfields-functxt_02 = 'UA'.
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN'FC01'.
p_carrid = 'LH'.
p_cityfr = 'Frankfurt'.
WHEN 'FC02'.
p_carrid = 'UA'.
p_cityfr = 'Chicago'.
ENDCASE.
36
Formatting Selection Screen contd.
• The parameters [/]<pos(len)>, <name>, and the MODIF ID option are the same as
described for the COMMENT option in Comments.
• For <ucom>, you must specify a code of up to four characters. You must declare
SSCRFIELDS by using a TABLES statement.
37
Formatting Selection Screen contd.
TABLES SSCRFIELDS. INITIALIZATION.
38
Formatting Selection Screen contd.
39
RANGES statement
• Use ranges in WHERE clause and logical expression as select-options are used
– <f> IN <rangetab>
40
Reports
Creating an ABAP/4 Program
Any customer-developed program should begin with “Y ”or “Z ”as a first character.
A statement is a sequence of words that ends with a period “.”.
A word in a statement always begins with an ABAP/4 keyword. A literal is enclosed by
single quotation marks.
*at the first column denotes the entire line is commented
“can be inserted at any place in line, after this double quotes, everything will be treated as
comments.
Transaction Code : SE38.
Menu Path: Tools> ABAP Workbench> ABAP Editor
41
Types of programs
Type 1
• run on its own
• Can be started it in the R/3 system without a transaction code
• Can be executed in background
42
Types of programs contd.
• non-executable • non-executable
• can have its own screens • cannot have its own screens.
• non-executable • non-executable
43
Sample Program
REPORT ZFIRSPRG.
WRITE ‘This is the First Sample ABAP Program’.
WRITE /‘This is in Second Line.’.
WRITE:/ ‘This is in Third Line.’,
‘I am also in Third Line’.
Note
/ -Line feed
: -Chain declaration.
44
REPORT Statement
• REPORT Statement
– MESSAGE-ID - Allows the use of the Message statement without explicitly specifying
the message id.
– NO STANDARD PAGE HEADING - Builds a header for the list displayed from your
report by default.
• Example :
45
WRITE Statement
46
Reports
47
Events in ABAP
ABAP is a event driven language. The different events in an ABAP report are
• Initialization
• At Selection Screen
• At Selection Screen Output
• Start of` Selection
• End of Selection
• Top of Page
• End of Page
• At Line Selection
• At User Command
48
Events in ABAP contd.
Initialization
• Example
CLEAR s_blart.
s_blart-sign = ‘I’.
s_blart-option = ‘EQ’.
s_blart-low = ‘WA’.
APPEND s_blart.
CLEAR s_blart.
49
Events in ABAP contd.
AT SELECTION-SCREEN
• This event is processed after user presses “Enter”on the selection screen.
• Its main purpose is to verify user input prior to program execution. Used during validating
the data entered by the user
• AT SELECTION-SCREEN OUTPUT
– This event is processed before display of the selection screen.
– Example
AT SELECTION SCREEN.
IF p_wrbtr < 1.
MESSAGE e999 WITH ‘Greater than 1.’
ENDIF.
50
Events in ABAP contd.
START-OF-SELECTION.
• This event begins the main processing of the program. The event is triggered upon the
user pressing “Execute” on the selection screen.
END-OF-SELECTION
• This event is triggered following the execution of the last statement in the START-OF-
SELECTION.
51
Events in ABAP contd.
TOP-OF-PAGE.
This event is triggered by the first WRITE statement to display data on a new page; it is not
triggered by the NEW-PAGE statement, but the first WRITE statement following a NEW-
PAGE.
END-OF-PAGE.
AT LINE-SELECTION.
This event is activated from the displayed list when the user selects “Choose” or double-
clicks on a line.
AT USER-COMMAND.
This event gets activated when the user executes a function defined within the menu for the
displayed list.
52
Events in ABAP Runtime Environment
53
Control Statements
• IF…ELSE…ENDIF
• CASE….ENDCASE
• LOOP….ENDLOOP
• DO…..ENDDO
• WHILE….ENDWHILE
54
IF Statement
55
IF …
• IF <logical expression>
– <logical expression> is one of the following :
• F1 <operand> F2
– Any logical or relational operators can be used.
• F1 BETWEEN F2 AND F3
– Field F1 is checked for the value between F2 and F3
• F1 IS INITIAL
– Field F1 is Initial I.e. Value is equals Zero
• F1 IN <selection>
– <selection> is an internal table of Select-Options.
• NOT ( F1 IS INITIAL)
– Field F1 is not Initial I.e. Value is not equals Zero
56
OPERANDS
• = , EQ Equal to.
• <>, ><, NE Not Equal to.
• >, GT Greater than.
• <, LT Less than.
• >=, =>, GE Greater than or equal to
• <=, =<, LE Less than or equal to
• CO Contains only. Left side contains only characters from right side.
• CN Contains not only. Equivalent to NOT ( c1 CO c2 ).
• CA Contains any. Left side contains at least one character from the right side.
• NA Contains not any. Equivalent to NOT ( c1 CA c2 ).
• CS Contains String. Left side contains the full string in right side.
• NS Contains no string. Equivalent to NOT ( c1 CS c2).
• CP Contains pattern. Similar to LIKE in WHERE clause. “*”matches any multiple characters,
the “+”matches a single character. Use the “#”to indicate the character immediately following
to be matched literally; For eg., to find an actual “*”in position 1, use “#*…”
• NP Contains no pattern. Equivalent to NOT ( c1 CP c2 ).
57
CASE Statement
Example
CASE<var>.
WHEN<val1>. DATA: txt1 VALUE 'X',
txt2 VALUE 'Y',
<statement block> txt3 VALUE 'Z',
WHEN <val2>. strng VALUE 'A'.
58
LOOP Statement
LOOP AT <itab>
FROM <n1>
TO <n2>
WHERE <logical expr>
ENDLOOP.
• <itab> - Internal Table within the program
• <n1> - If specified, the LOOP begins with record number n1.
• <n2> - If specified, the LOOP ends with record number n2.
• WHERE - Comparison to be performed before processing the statements.
• System fields: sy-index, sy-tabix.
59
LOOP…
Nested Loops are also possible
LOOP AT ITAB1.
…
LOOP AT ITAB2.
….
ENDLOOP.
…
ENDLOOP.
Within the loop, the statements CHECK and EXIT can also be used; a
failed CHECK statement skips the processing of the current record and
returns to the top of the LOOP. EXIT resumes processing with the
statement immediately following the ENDLOOP.
60
Do Loop
61
WHILE Loop
62
LOOP
To terminate the processing of a loop, one of the following keywords is used.
• CONTINUE
• CHECK
• EXIT
NEW-PAGE.
–This will trigger a new page to be displayed.
–This can be used to skip from the current page.
63
Joins
• Joins are more efficient than logical database and nested selects.
• Types of Joins
– Inner Joins
– Outer Joins
64
Inner Joins
• Inner Joins allow access to multiple tables with a single selectstatement by creating a
temporary table based on the conditions in the ON Statement. Multiple tables are joined
based on the key fieldsspecified by the join condition.
• Inner Joins are equivalent to views created in the Dictionary.
• Syntax for inner join
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…
WHERE….
ENDSELECT.
• Example :
SELECT scarr~carrname sflight~carrid sflight~connid sflight~fldate
INTO (carrname,carrid,connid, date)
FROM scarr INNER JOIN sflight
ON scarr~carrid = sflight~carrid.
WRITE:/ carrname, carrid, connid, date.
ENDSELECT.
65
Left Outer Joins
• Like inner joins, left outer joins create a temporary table based on the conditions specified
in the ON clause
• Unlike inner joins:
– Based on conditions expressed in the ON statement, fields in the driving (left-hand)
table that do not correspond to fields in the right-hand table are still added to
temporary table. There they are populated with initial values
• Syntax :
SELECT <table1~field1 table1~field2 table2~field3….>
into (<target>)
FROM <table1> LEFT OUTER JOIN <table2>
ON <table1~keyfield1> =<table2~keyfield1>
AND <table1~keyfield2> = <table2~keyfield2>
AND…
WHERE….
ENDSELECT.
66
System Fields
• General
• SY-UNAME - Logon name of the user
• SY-DATUM - Current date
• SY-UZEIT - Current time
• SY-TABIX - Current line of an internal table
• SY-INDEX - Number of loop passes
• Lists
• SY-TITLE - Report title from text elements
• SY-LINCT - Number of lines from the REPORT statement
• SY-LINSZ - Line width from the REPORT statement
• SY-SROWS - Number of lines in the current window
• SY-SCOLS - Number of columns in the current window
• SY-PAGNO - Number of current page
• SY-LINNO - Number of current line
• SY-COLNO - Number of current column
67
Thank You!