3 SAP ABAP Reports Part 1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 68

Reports (Part – I)

Basic Functions of the ABAP Editor – Transaction SE38

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

• DATA <Name> TYPE or LIKEVALUEDECIMALS

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

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

• 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,
END OF flightrec1_type.

TYPES: f_type TYPE flightrec1_type.


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

• Texts on the selection screen can be maintained as language-dependent text elements.

• In an ABAP/4 program, you use the following statements to design selection screens:

– PARAMETERS, to define input fields for variables

– SELECT-OPTIONS, to define input fields for selection criteria

– SELECTION-SCREEN, to format the selection screen.

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.

PARAMETERS <p> ...... DEFAULT <f> ......

Example :

PARAMETERS: VALUE TYPE I DEFAULT 100,


NAME LIKE SY-UNAME DEFAULT SY-UNAME,
DATE LIKE SY-DATUM DEFAULT '19950627'.

Output Screen :

10
PARAMETERS contd.
PARAMETERS <p> ...... LOWER CASE ......
Example :
PARAMETERS: FIELD1(10),
FIELD2(10) LOWER CASE.
WRITE: FIELD1, FIELD2.

Output Screen :

• The output appears as follows:


UPPER lower
• The contents of FIELD1 are changed to upper case.

11
PARAMETERS contd.

PARAMETERS <p> ...... OBLIGATORY ......

Example :

PARAMETERS FIELD(10) OBLIGATORY.

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

PARAMETERS <p>...... RADIOBUTTON GROUP <radi>…

Example :

PARAMETERS: R1 RADIOBUTTON GROUP RAD1,

R2 RADIOBUTTON GROUP RAD1 DEFAULT 'X',

R3 RADIOBUTTON GROUP RAD1,

S1 RADIOBUTTON GROUP RAD2,

S2 RADIOBUTTON GROUP RAD2,

S3 RADIOBUTTON GROUP RAD2 DEFAULT 'X'.

Output Screen :

14
PARAMETERS contd.

PARAMETERS <p>...... MEMORY ID <pid>…

Example :

PARAMETERS TEST(16) MEMORY ID RID.

Output Screen :

15
PARAMETERS contd.

PARAMETERS <p>...... MATCHCODE OBJECT <obj>…

Example :

PARAMETERS p_carrid(3) MATCHCODE OBJECT demo_f4_de.

Output Screen :

16
PARAMETERS contd.

PARAMETERS <p>...... MODIF ID <id>…


Example :
PARAMETERS: TEST1(10) MODIF ID SC1,
TEST2(10) MODIF ID SC2,
TEST3(10) MODIF ID SC1,
TEST4(10) MODIF ID SC2.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
  IF SCREEN-GROUP1 = 'SC1'.
    SCREEN-INTENSIFIED = '1'.
    MODIFY SCREEN.
    CONTINUE.
  ENDIF.
  IF SCREEN-GROUP1 = 'SC2'.
    SCREEN-INTENSIFIED = '0'.
    MODIFY SCREEN.
  ENDIF.
ENDLOOP.

17
PARAMETERS contd.

PARAMETERS <p>...... VALUE CHECK

Example :

PARAMETERS P_CARR LIKE SPFLI-CARRID OBLIGATORY VALUE CHECK.

Output Screen :

18
PARAMETERS contd.

PARAMETERS <p> LIKE (field)

Example :
DATA NAME(20) TYPE C.

PARAMETERS P_CARR LIKE (NAME).

INITIALIZATION.

NAME = 'SPFLI-CARRID'.

19
PARAMETERS contd.

PARAMETERS … VISIBLE LENGTH…

PARAMETERS: p1(10) TYPE c VISIBLE LENGTH 1,


            p2(10) TYPE c VISIBLE LENGTH 5,
            p3(10) TYPE c VISIBLE LENGTH 10.

START-OF-SELECTION.
  WRITE: / 'P1:', p1,
         / 'P2:', p2,
         / 'P3:', p3.

20
Selection Criteria

• You define selection criteria with the SELECT-OPTIONS statement.

• 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

SELECT-OPTIONS <seltab> FOR <f>.

• The field <f> cannot have data type F. The data type F is not supported on the
selection screen.

• For SELECT-OPTIONS statement, the system creates a selection table. The


purpose of selection tables is to store complex selection limits in a standardized
manner.

• 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 :

• Type C with length 1.

• Possible values are I and E.

– OPTION :

• Type of OPTION is C with length 2.

• OPTION contains the selection operator.

• If HIGH is empty, you can use EQ, NE, GT, LE, LT,CP, and NP.

• If HIGH is filled, you can use BT and NB.

– LOW & HIGH:

• Type is the same as the column type of the database table, to which the selection
criterion is attached.

23
SELECT-OPTIONS Variations

SELECT-OPTIONS <seltab> FOR <f> ... DEFAULT <g> [TO <h>]

SELECT-OPTIONS <seltab> FOR <f> ... NO-EXTENSION

SELECT-OPTIONS <seltab> FOR <f> ... NO INTERVALS

SELECT-OPTIONS <seltab> FOR <f> ... NO-DISPLAY

SELECT-OPTIONS <seltab> FOR <f> ... LOWER CASE

SELECT-OPTIONS <seltab> FOR <f> ... OBLIGATORY

SELECT-OPTIONS <seltab> FOR <f> ... MEMORY ID <pid>

SELECT-OPTIONS <seltab> FOR <f> ... MODIF ID <key>

SELECT-OPTIONS <seltab> FOR <f> ... MATCHCODE OBJECT <obj>

24
SELECT-OPTIONS

SELECT-OPTIONS <seltab> FOR <f> DEFAULT <g> [TO <h>] ....

Example :

DATA WA_SPFLI TYPE SPFLI.

SELECT-OPTIONS AIRLINE FOR WA_SPFLI-CARRID


               DEFAULT 'AA'
                    TO 'LH'
                OPTION  NB
                  SIGN  I.

Output Screen :

25
SELECT-OPTIONS contd.

SELECT-OPTIONS <seltab> FOR <f> ..... NO-EXTENSION .....

Example :

DATA WA_SPFLI TYPE SPFLI.

SELECT-OPTIONS AIRLINE FOR WA_SPFLI-CARRID NO-EXTENSION.

Output Screen :

26
SELECT-OPTIONS contd.

SELECT-OPTIONS <seltab> FOR <f> ..... NO INTERVALS .....

Example :

REPORT demo_sel_screen_select_no_int1.

DATA wa_spfli TYPE spfli.

SELECT-OPTIONS airline FOR wa_spfli-carrid NO INTERVALS.

Output Screen :

27
Using Selection Tables

• Using Selection Tables in the WHERE Clause

WHERE <f> IN <seltab>.

– To limit the database access of the Open SQL statements

SELECT, UPDATE, and DELETE, you use the WHERE clause.

• Using Selection Tables in Logical Expressions

<f> IN <seltab> ....

– To control the internal flow of your program, you must have

program conditions with logical expressions. You can program

special logical expressions using selection tables.

28
Formatting Selection Screen

• The selection screen, which appears when the PARAMETERS or SELECT-


OPTIONS statements are used in a program, has a standard layout where all
parameters appear line after line.

• SELECTION-SCREEN statement can be used to format selection screen when


the standard selection screen layout is not sufficient.

• Blank Lines

• To produce blank lines on the selection screen, use the SKIP option with the
SELECTION-SCREEN statement.

SELECTION-SCREEN SKIP [<n>].

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

• To produce blank lines, the SKIP option is used.

– SELECTION-SCREEN SKIP [<n>].

• To underline a line or part of a line, the ULINE option is used.

– SELECTION-SCREEN ULINE [[/]<pos(len)>]

• To write text on the selection screen, the COMMENT option is used

– SELECTION-SCREEN COMMENT [/]<pos(len)> <comm> [FOR FIELD <f>]

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

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

33
Blocking Selection Screen

34
Formatting Selection Screen

Creating Push-Buttons in the Application Toolbar

SELECTION-SCREEN FUNCTION KEY <i>.

• 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

the buttons during runtime in ABAP/4.

• Dictionary fields SSCRFIELDS-FUNCTXT_0<i>. You must declare

SSCRFIELDS with a TABLES statement.

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

Creating Push-Buttons on the Selection Screen

SELECTION SCREEN PUSHBUTTON [/] <pos(len)> <name>

USER-COMMAND <ucom> [MODIF ID <key>].

• The parameters [/]<pos(len)>, <name>, and the MODIF ID option are the same as
described for the COMMENT option in Comments.

• The text specified in <name> is the pushbutton text.

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

DATA FLAG. BUT1 = 'Button 1'.


BUT2 = 'Button 2'.
SELECTION-SCREEN:
BUT3 = 'Button 3'.
BEGIN OF LINE,
BUT4 = 'Button 4'.
PUSHBUTTON 2(10) BUT1 USER-COMMAND
AT SELECTION-SCREEN.
CLI1,
CASE SSCRFIELDS.
PUSHBUTTON 12(10) BUT2 USER-COMMAND
WHEN 'CLI1'.
CLI2,
MESSAGE I999 WITH 'Button 1 was clicked'.
END OF LINE,
WHEN 'CLI2'.
BEGIN OF LINE,
MESSAGE I999 WITH 'Button 2 was clicked'.
PUSHBUTTON 2(10) BUT3 USER-COMMAND WHEN 'CLI3'.
CLI3,
MESSAGE I999 WITH 'Button 3 was clicked'.
PUSHBUTTON 12(10) BUT4 USER-COMMAND
WHEN 'CLI4'.
CLI4,
MESSAGE I999 WITH 'Button 4 was clicked'.
END OF LINE.
ENDCASE.

38
Formatting Selection Screen contd.

39
RANGES statement

• To create internal tables of the same type as selection tables

• RANGES <rangetab> FOR <f>.

• This is simply a shortened form of:

– DATA: BEGIN OF <rangetab> OCCURS 0,


         sign(1) TYPE c,
         option(2) TYPE c,
         low  LIKE <f>,
         high LIKE <f>,
      END OF <rangetab>.

• Use ranges in WHERE clause and logical expression as select-options are used

– WHERE <f> IN <rangetab>

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

Type M (Module pool)


• Program cannot run on its own and can be called via a transaction code

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

42
Types of programs contd.

Type F ( Function Group) Type S (Interface Definition )

• non-executable • non-executable

• container for function modules • container for subroutines

• can have its own screens • cannot have its own screens.

Type K (Class Definition ) Type K (Class Definition )

• non-executable • non-executable

• container for classes • container for classes

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

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

45
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

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

Note: SKIP -Will leave a blank line.

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

• This event is triggered prior to


the first display of the selection
screen.

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

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

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.

This event is triggered as soon as the LINE-COUNT reached. NEW-PAGE statement


causes this event to be ignored.

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

<statement block> CASE strng.


...... WHEN text1.
WRITE: / 'String is', txt1.
WHEN OTHERS.
WHEN text2.
<statement block> WRITE: / ’String is’, txt2.
ENDCASE. 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

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

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


<statement block>
ENDDO.
Example
DO 2 TIMES.
WRITE SY-INDEX.
SKIP.
DO 3 TIMES.
WRITE SY-INDEX.
ENDDO. Output
1
SKIP. 123
2
ENDDO. 123

61
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'.
strl = STRLEN( string ).
WHILE string NE SPACE.
WRITE string(1).
ength = sy-index.
Output :
SHIFT string.
TestString
ENDWHILE.
STRLEN: 11
WRITE: / 'STRLEN: ', strl.
Length of string: 11
WRITE: / 'Length of string:', length.

62
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

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.

• They access multiple tables with one select statement.

• 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!

You might also like