Você está na página 1de 63

Day 2

Reports
Basic Functions of the ABAP Editor

Standard toolbar
Find and
Repeat Find

Syntax
SyntaxCheck
Check Execution (F8)
(Ctrl+F2) ABAP help (F1)

Display/change mode Where used list


Activation (Ctrl+Shift+F3)

.
(Ctrl+F3)
(Ctrl+F1)
DATA Definitions

• DATA Statement
DATA <Name> TYPE or LIKE VALUE DECIMALS

- All variables used within the ABAP/4 program must be declared with DATA
statements
- <Name> up to 30 characters in length, containing any characters other than
(, ), +, ., :
- <TYPE> Indicates the variable type

Example:
DATA: p_bukrs LIKE bkpf-bukrs.
DATA i_val TYPE i VALUE 99.
DATA Definitions

• TYPES Statement
TYPES <name> TYPE or LIKE DECIMALS
SAP allows the creation of new user defined data types. And this does not
create a variable, BUT just a new type that can be used in creating a variable.

Example :
TYPES : cc LIKE bkpf-bukrs
DATA : c_cc TYPE cc.
TYPES

• Field String Type


TYPES: BEGIN OF <type>... END OF <type>.
Example
TYPES: flight(25) TYPE C.

TYPES: BEGIN OF flightrec1_type,


flag TYPE C,
carrid LIKE spfli_carrid,
name TYPE flight,
sum_field TYPE sum_field_type,
END OF flightrec1_type.

TYPES: f_type TYPE flightrec1_type.


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.
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'
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.
Specifying Blank Lines
• To produce blank lines, the SKIP option is used.
Syntax
– SELECTION-SCREEN SKIP [<n>].
• To underline a line or part of a line, the ULINE option is
used.
Syntax
– SELECTION-SCREEN ULINE [[/]<pos(len)>]
• To write text on the selection screen, the COMMENT option
is used
Syntax
– SELECTION-SCREEN COMMENT [/]<pos(len)> <comm>
[FOR FIELD <f>]
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.
Positioning in the Selection Screen
• To position the next parameter or comment on the selection screen, the
POSITION option is used.
• Syntax
– SELECTION-SCREEN POSITION <pos>.
• For <pos>, you can specify a number, POS_LOW, or POS_HIGH.
• 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:
– SELECTION-SCREEN BEGIN OF BLOCK <block>
[WITH FRAME [TITLE <title>]]
[NO INTERVALS].

SELECTION-SCREEN END OF BLOCK <block>.
• Blocks can be nested.
Blocking Selection Screen

• Example
– SELECTION-SCREEN BEGIN OF BLOCK rad1 WITH FRAME TITLE text-002.
PARAMETERS vendor RADIOBUTTON GROUP gr1.
PARAMETERS customer RADIOBUTTON GROUP gr1.
PARAMETERS material RADIOBUTTON GROUP gr1.
SELECTION-SCREEN END OF BLOCK rad1.
Data Definitions
• Internal Tables
DATA : BEGIN OF <name> OCCURS x,
(variable definitions)
END OF <name>.

Internal Tables are defined as an extension of a structure, with the addition


of an OCCURS clause. Internal Tables can be created with or without
header lines.

Example 1 (with header line)


DATA : BEGIN OF t_wrk OCCURS 0 WITH HEADER LINE,
t_kunnr LIKE kna1-kunnr,
sw TYPE C,
END OF t_wrk.
Data Definitions

Example 2 (without header line)


DATA : BEGIN OF t_wrk OCCURS 0,
t_kunnr LIKE kna1-kunnr,
sw TYPE c,
END OF t_wrk.

• Internal Table records are added by a number of statements,


including INSERT & APPEND.
• Only one line can be referenced at a time in the program via the
header line.
• Lines to be referenced must be loaded into the header line via
statements such as READ and LOOP.
Data Definitions

• You can Include another structure into Internal table.

DATA : BEGIN OF t_tab1,


field1 LIKE bkpf-belnr,
field2 LIKE bseg-buzei,
END OF T_TAB1.

DATA : BEGIN OF t_tab2 OCCURS 10.


INCLUDE STRUCTURE t_tab1.
DATA : END OF t_tab2.

In this example, t_tab2 will contain the fields field1 & field2.
Program Level Statements
• CLEAR Statement
– CLEAR <var1>.
– Initializes the var1 to Zero

• REFRESH Statement
– REFRESH <var1>.
– Deletes and Initializes the var1 to Zero

• This has differences only in Internal Tables with header line and
without header line.

• CLEAR will initialize the header line, if the Internal table is with
header line otherwise it is same as REFRESH.
Data Definitions
• Appending Internal Table
DATA : BEGIN OF t_tab1 OCCURS 0,
field1 TYPE C,
field2 TYPE C,
END OF t_tab1.

t_tab1-field1 = ‘A’.
t_tab1-field2 = ‘B’.
APPEND t_tab1.
CLEAR t_tab1.

t_tab1-field1 = ‘C’.
t_tab1-field2 = ‘D’.
APPEND t_tab1.
CLEAR t_tab1.
Internal Tables

• Modifying Internal Table

MODIFY <Internal Table> <Options>

t_tab1-field1 = ‘Y’.
t_tab1-field2 = ‘Z’.

MODIFY t_tab1 INDEX 1.


This will modify the Internal Table of first row.
MODIFY t_tab1.
This will modify the entire Internal Table.
MODIFY t_tab1 INDEX sy-index.
This will modify the Internal Table where the current index
pointer is pointing.
Internal Tables
• Deleting Internal Table

DELETE <Internal Table> <Options>

DELETE t_tab1 INDEX 1.


This will delete the Internal Table of first row.
DELETE t_tab1 FROM 1 TO 4.
This will deletes from 1 to 4 lines in Internal Table.
DELETE t_tab1 INDEX sy-index.
This will delete the Internal Table where the current index
pointer is pointing.
DELETE t_tab1 WHERE t_tab1-field1 = ‘C’.
This will delete the satisfied records of the above condition.
Internal Tables
• Reading Internal Table

READ TABLE <itab> INDEX <idx>.


READ TABLE <itab> INTO <wa> INDEX <idx>.
READ TABLE <itab> WITH KEY <fld1= “string”> [BINARY SEARCH]

Example
READ TABLE itab INDEX 1.
This will read the first line of Internal Table
READ TABLE itab INTO wrk_tab INDEX sy-tabix.
This will copy the line into another work area where current index is pointer
is pointing
READ TABLE itab WITH KEY lifnr eq ‘V001’ BINARY SEARCH
This will read the internal table with specified key in binary search mode
Internal Tables

• The SORT Statement

SORT <Int. Tab.> BY <f1>..<fn> [<order>]

– The internal table <Int. Tab.> is sorted by its standard key if the BY option is
– not used.
– If BY option is used, it will be sorted by the order of the fields <f1>, <f2>,.. <fn>
– By default, it will sort by ASCENDING,
– To sort in the descending order we have to specify as DESCENDING.
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
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

Type M ( Module pool)


• Program cannot run on its own and can be called via
a transaction code
Types of programs…

Type I ( Include program )


• Contains the program code that can be used by
different programs
• It modularizes the source code which consists of
several different, logically related parts
• Readability is improved and thus easy
maintenance
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’.

/ - Line feed
: - Chain declaration.
REPORT Statement

• REPORT Statement
– LINE-SIZE - Specifies, in columns, the width of the list to be
displayed.
– LINE-COUNT - Specifies the no. of lines per page
– 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 :
REPORT ZTEST LINE-SIZE 250 LINE-COUNT 65.
WRITE / ‘Width of the Line Statement’.
WRITE Statement

• WRITE <Format> <Field> <Options>


– <Format> Output Format specification
• Being with a “/” to indicate a new line
• WRITE 9 … means on the current line, begin in column 9
• WRITE /03(5)… means begin a new line, begin in column 3, for a
length of 5
– <Field> Can be a data variable, text literal, or numbered text
– <Options> Specify a number of formatting options like NO-
ZERO, NO-SIGN, CURRENCY w, DECIMALS d, ROUND r,
DD/MM/YYYY, NO-GAP
• Example
Write / p_text NO-GAP
Reports…

REPORT ZTEST NO STANDARD PAGE HEADING


WRITE ‘1’,’2’,’3’ NO-GAP.
WRITE : / 3 ‘Column 1’,
15 ‘Column 2’, 25(7) ‘ ------’,
35 ‘Column 3’.
SKIP.
WRITE ‘End of Line’.

SKIP - Will leave a blank line.


Events in ABAP

• ABAP is a event driven language. The different events in


an ABAP report are
– Initialisation
– 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
Program Level Events

• INITIALIZATION.
– This event is triggered prior to the first display of the selection
screen.

Example
INITIALIZATION.
CLEAR s_blart.
s_blart-sign = ‘I’.
s_blart-option = ‘EQ’.
s_blart-low = ‘WA’.
APPEND s_blart.
CLEAR s_blart.
Initialisation
In itializin g th e S e lectio n S c re en

I N IT IA L I ZA TI ON .

R EP O R T s ap b c 4 s0 s5_c d_ i n i ti al iz at io n .
. ..

IINI
NI
N ITTI ALIZ
TIIAL IIZ
ZAT
AT
A TIIIO
OON..
NN.
MO VE : m ar k TO pa _ a l l .
A irline AA to LH
M O V E : ' I' T O s o_ca rr-s ig n,
ca rr-o pt io n, F lig h t d ate
' BT ' T O s o_ to
' AA ' T O s o_
ca rr-l ow ,
' LH ' T O s o_
ca rr-h ig h. O u tpu t ...
A P P E ND s oc_a rr.
CL EA R soc_a r r. S eats ...
M O V E : ' E ' TO s o_ c a r r- s ig n, O ccu p ied
'E Q' T O soc _a r r- o p ti on , A v aila ble
'D L' T O so _c ar r- lo w.
A ll
A PP EN D s o _c a r r.
. .. S ele ction C olors Ic on s

 S A P A G19 99
Program Level Events

• 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.
Program Level Events
• 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.
Note : An implicit START-OF-SELECTION is defined by
the REPORT statement. Any code placed between the
REPORT statement and the first event declaration is
executed during the START-OF-SELECTION event.

• END-OF-SELECTION
– This event is triggered following the execution of the last statement in
the START-OF-SELECTION.
Note : STOP Statement causes an automatic branch to
END-OF-SELECTION.
Program Level Events

• 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.
– This event is triggered as soon as the LINE-COUNT reached.
NEW-PAGE statement causes this event to be ignored.
Program Level Event

• 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.
Sample Report

Sample Report
Events in ABAP Runtime Environment
Control Statements

• IF…ELSE…ENDIF

• CASE….ENDCASE

• LOOP….ENDLOOP

• DO…..ENDDO

• WHILE….ENDWHILE
IF Statement

Option 3 :

Option 2 : IF <condition1>.
Option 1 :
IF <condition1>.
IF <condition1>
<statement block> <statement block>
<statement block>
ELSE. ELSEIF <condition2>.
ENDIF
<statement block> <statement block>
ENDIF. .....
ELSE.
<statement block>
ENDIF.
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
OPERANDS
= , EQ Equal to.
<>, ><, NENot 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 ).
CASE Statement

CASE <var>.
WHEN <val1>.
<statement block>
WHEN <val2>.
<statement block>
......
WHEN OTHERS.
<statement block>
ENDCASE.
CASE..
• Example
DATA: txt1 VALUE 'X',
txt2 VALUE 'Y',
txt3 VALUE 'Z',
strng VALUE 'A'.
CASE strng.
WHEN text1.
WRITE: / 'String is', txt1.
WHEN text2.
WRITE: / ’String is’, txt2.
WHEN text3.
WRITE: / ’String is’, txt3.
WHEN OTHERS.
WRITE: / ’String is not’, txt1, txt2, txt3.
ENDCASE.
The output appears as follows: String is not X Y Z
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.


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.
Control Breaks in Loop

• Four forms of the AT statement exist for processing within a


LOOP.

1. AT FIRST … ENDAT for Statements to be executed before


any records are processed.

Example
LOOP AT itab.
AT FIRST.
WRITE : SY-ULINE.
ENDAT.
…….
ENDLOOP.
Control Breaks

2. AT LAST … ENDAT for Statements to be executed after all


records are processed. For both AT FIRST and AT LAST, all
fields in the header line of the internal table will contain an “*”

Example
LOOP AT itab.
AT LAST.
WRITE : SY-ULINE.
ENDAT.
……
ENDLOOP.
Control Breaks

3. AT NEW <Field Name> … ENDAT for Statements to be


executed at the beginning of a group of records
containing the same value for <Field Name>. All fields in
the internal table header line defined AFTER <Field
Name> will contain an “*”

Example
AT NEW I_LIFNR.
WRITE : SY-ULINE

ENDAT.
Control Breaks

4. AT END OF <Field name>. Statements to be executed at the end of


a group of records containing the same value for <Field Name>. All
fields in the internal table header line defined AFTER <Field Name>
will contain an “*”.

Example
AT END OF I_LIFNR.
SUM.
WRITE : SY-ULINE.
ENDAT.

Note : AT NEW and AT END OF only make sense for a sorted table.
Do Loop

DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f2>].


<statement block>
ENDDO.

Example

DO 2 TIMES.
Output
WRITE SY-INDEX. 1
SKIP. 123
DO 3 TIMES. 2
WRITE SY-INDEX. 123
ENDDO.
SKIP.
ENDDO.
WHILE Loop

WHILE <condition> [VARY <f> FROM <f1> NEXT <f2>].


<statement block>
ENDWHILE.
Example
DATA: length TYPE I VALUE 0,
strl TYPE I VALUE 0,
string(30) TYPE C VALUE 'Test String'.
Output
strl = STRLEN( string ).
WHILE string NE SPACE.
WRITE string(1). TestString
length = sy-index. STRLEN: 11
SHIFT string.
ENDWHILE. Length of string:
WRITE: / 'STRLEN: ', strl. 11
WRITE: / 'Length of string:', length.
LOOP
• To terminate the processing of a loop, one of the following
keywords is used.
• CONTINUE
-Terminating the current Loop Pass Unconditionally
• CHECK
- Terminating the current Loop Pass Conditionally
• EXIT
-Terminating a Loop Entirely
Other events

• NEW-PAGE.
– This will trigger a new page to be displayed.
– This can be used to skip from the current page.
Data Retrieval

SELECT * FROM <database table>


WHERE …
ENDSELECT.

<database table> is table defined within the TABLES statement.


WHERE clause identifies which records to retrieve.

Note : SY-SUBRC, 0 If records are retrieved, 4 if none are found.


SY-DBCNT, Number of database records retrieved.
Data Retrieval

• SELECT * FROM <dbtable> INTO <workarea>.


<workarea> must be defined in your program, and be at least as wide as the record
length of the <dbtable>. In this case, the record is read into the <workarea> and not
into the database table buffer.

• SELECT * FROM <dbtable> INTO TABLE <itab> …


<itab> must be defined as <workarea> above. The contents of the internal table are
replaced with the records retrieved. This form does not require an ENDSELECT, as
no loop processing is performed.

• SELECT * FROM <dbtable> APPENDING TABLE <itab> …


same as INTO TABLE, but the records are added to the end of <itab>, leaving the
original contents.
Data Retrieval

• SELECT * FROM <dbtable> … ORDER BY <f1> <f2> …


specifies a sort order for records retrieved. By default is ascending order; if the field
should be in descending order use …<f1> DESCENDING

• SELECT * FROM <dbtable> … CLIENT SPECIFIED


For client-dependent tables, the first field is always the client or MANDT. This does
not normally need to be specified; SAP will automatically only retrieve records from
the client from which the report is being executed.
Data Retrieval
• SELECT SINGLE * FROM <dbtable> WHERE …
This statement retrieves one and only one record from the database. This form does
not require an ENDSELECT, as no loop processing is performed.

• ABAP/4 is more like SQL. In addition to SELECT * form, individual fields or


columns can be selected. In this case, the INTO clause must be specified:
SELECT belnr blart INTO t_bkpf-belnr t_bkpf-blart FROM bkpf ….

• The addition INTO CORRESPONDING FIELDS OF <structure> can also be


used. Also, the GROUP BY clause has also been added, with the following
aggregate functions available :
– MIN, MAX, AVG, SUM, COUNT

• Finally, the table name in the FROM clause can now be a variable; thus
SELECT * FROM (var1)
Joins

• Joins are more efficient than logical database and nested selects.

• They access multiple tables with one select statement.


Inner Joins
• Inner Joins allow access to multiple tables with a single select
statement by creating a temporary table based on the conditions in
the ON Statement. Multiple tables are joined based on the key fields
specified by the join condition.

• Inner Joins are equivalent to views created in the Dictionary.

Syntax for inner join


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.
Inner Joins Syntax.

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.
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

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.
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.
System Variables - List

• The system fields used in interactive reporting

– SY-CUROW - Cursor position (line)


– SY-CUCOL - Cursor position (column)
– SY-CPAGE - Number of the current page
– SY-STACO - First displayed column of the list on display
– SY-STARO - First displayed line of the list on display
– SY-LSIND - Index of the displayed list level
– SY-LISTI - Index of the selected list level
– SY-LILLI - Number of the selected line
– SY-LISEL - Contents of the selected line

Você também pode gostar