Вы находитесь на странице: 1из 31

Slide

1 of
31

Lesson Purpose

The purpose of this lesson is to examine and describe dynamic language constructs that
are contained within the ABAP/4 Open SQL .

The ABAP/4 language contains several dynamic commands that allow the devopler to
handle situations where relevant information to carry out a command is available only at
runtime. These dynamic features increase the flexibility of application programs.
Slide
2 of
31

Lesson Topics

This lesson consists of the following main topics:

• SELECT statement
• Function usage in SELECT
• WHERE clause alternatives

• LIKE
• IN

• Other SELECT statement clauses

• INTO
• GROUP BY
• ORDER BY

• Dynamic SORT field


• Messages
• Message class
• MESSAGE statement

• Authorization checks

Slide
3 of
31

Lesson Objectives

Upon completion of this lesson, you should be able to:

• Describe the concepts of dynamic language constructs and their purpose in


an ABAP/4 program.
• Describe the use of a dynamic tablename in the SELECT statement and in
its WHERE clause
• Use the LIKE and IN operator with the WHERE clause
• Use the GROUP BY and ORDER BY clauses with the SELECT statement
to arrange table data
• Use a dynamic SORT allow a user to specify a sort field at runtime
• Create messages and use them with the MESSAGE statement in ABAP/4
programs.
• Describe the use of authorization checks

Slide
4 of
31

Reading Data - Overview

You use the SELECT statement to read data from database tables.

• The ABAP/4 system uses statements related to standard SQL. You can use these to
access your database. ABAP/4 Open SQL has the following properties:
o ABAP/4 Open SQL Syntax corresponds to Standard SQL.
o ABAP/4 Open SQL is a subset of Standard SQL.
o ABAP/4 Open SQL contains SAP abbreviated forms.
• SELECT evaluates database tables using the ABAP/4 Open SQL interface. The tables
must be defined in the ABAP/4 Dictionary.
• SELECT does not carry out any authorization checks.
Slide
5 of
31


• Dynamic Open SQL Commands
• The table name is set dynamically as shown in the following example:
• parameters tablename(10) default ‘CUSTOMERS’.
data count_rows type i.
select count( * ) from (tablelname) into count_rows.
write: tablename, count_rows.
• In the SELECT statement, you can also create a dynamic WHERE clause. You do this by
defining an internal table and filling it a runtime with the source code for the WHERE
clause. Once the internal table has been created, then that table is referenced in the
WHERE clause of the SELECT table. The tablename of the table containing the where
clause is enclosed in parentheses. This is illustrated in the following example:
• tables customers.
data where_tab(80) occurs 10 with header line. “ Table containing WHERE
data all_customers like customers occurs 100.
select * form customers into table all_customers
where id between 1 and 99
and (where_tab). “This references the table with the dynamic WHERE
Slide
6 of
31

SELECT Overview

The SELECT command consists of a sequence of clauses with various tasks:

• The SELECT clause indicates:


o whether the result of the selection is to be a table or a single
record,
o which columns the result should contain
o whether identical entries may occur in the result.
• The INTO clause determines the target area into which the selected data is
read.
• The FROM clause specifies the source (database table or view) from
which the data is to be selected.
• The WHERE clause specifies conditions which must be met in the
selection. In other words, it determines which records will be included in the
result table.
• The GROUP BY clause combines groups of entries into individual entries.
• The ORDER BY clause determines how the entries in the result table
should be sorted.
Slide
7 of
31

Single or Multiple Entry Processing

Multiple entry processing

• The SELECT statement allows you to access all records matching the selection criteria
line by line using a SELECT ... ENDSELECT loop. Each loop pass reads one matching
entry into a work area.
• Once the loop has finished, SY-SUBRC has the value zero as long as at least one table
entry satisfied the selection criteria. At the end of each loop pass, SY-DBCNT contains
the number of entries read so far. After ENDSELECT, SY-DBCNT contains the total
number of entries read.

Single entry processing

• The SELECT SINGLE statement gives access to the first entry satisfying the selection
criteria. Since SELECT SINGLE does not start a loop, there is no ENDSELECT. SY-
SUBRC has the value zero as long as a single entry was read.
• After the SELECT statement has been executed, SY-DBCNT has the value zero or one.
Dynamic processing

• The tablename is obtained using a PARAMETERS screen at runtime.


• Additional processing may be desired to make sure the table exists or an error message
should be output to the user.

Slide
8 of
31


• SELECT Clause
• When you use SELECT *, the selection result contains all columns in the database table.
• A field in SELECT <field 1> ... <field n> can be either a table column or an aggregate
function of a table column. You can use the following aggregate functions in the
SELECT clause:
MIN( <field> ) finds the smallest value in column < field>
MAX( <field> ) finds the largest value in column < field>
AVG( <field> ) finds the mean value of column < field>
SUM( <field> ) finds the sum of column < field>
COUNT(*) finds the number of table entries
• SY-SUBRC is always zero on aggregate functions.
Slide
9 of
31

INTO Clause

The INTO clause determines the data structure into which the selection result is inserted. If the
statement contains no INTO clause, the ABAP/4 system puts the selection result into the table
work area defined in the TABLES statement.

The following are some of the variants of the INTO clause:

• INTO <wa>
The fields in the selection result are placed from left to right in the components of <wa>.
• INTO (<field 1>, ..., <field n>)
The fields of the selection result are placed from left to right in target fields <field 1> ...
<field n>.
• INTO CORRESPONDING FIELDS OF <wa>
Each field of the selection result is placed into the field in <> with the corresponding
name.
• INTO TABLE <itab>
Same as INTO <wa>, except that the selected data is not placed line by line into the
internal table but rather all at once.
• INTO CORRESPONDING FIELDS OF TABLE <itab>
Same as INTO CORRESPONDING FIELDS OF <wa>, except that the selected data is
not placed line by line into the internal table but rather all at once. This is the most
efficient alternative.

Slide
10 of
31

WHERE Clause Overview

The WHERE clause specifies conditions which must be met by the selection result. In other
words, it determines which entries will be included in the result table.

• Within the WHERE clause, you should address database fields without
their table prefix (e.g. ...WHERE CITYFROM = ... and not WHERE SPFLI-
CITYFROM = ...)
• <value> can be a literal, a constant or a field.
• The conditions which you specify in the WHERE clause can consist of
several elementary comparisons linked with NOT, AND or OR.
Slide
11 of
31

WHERE Clause: LIKE

The above example selects entries where the second character in the CITYFROM field is an “R”
and this is followed by any sequence of characters.

The “_” and “%” characters have special meanings:

• " _ " represents a single character


• "%" represents any sequence of characters.

You can only use these generic characters in text fields.


Slide
12 of
31

WHERE Clause: IN

You use the IN operator in the WHERE clause to establish a comparison with a list of single
values.

• Listing individual values using the IN operator is equivalent to linking


identities with OR.
• You can use fields or literals in the list.
Slide
13 of
31

GROUP BY Clause

The GROUP BY clause combines groups of entries into single entries. In the above example, the
entries are grouped according to the contents of the CARRID field. Aggregate functions are used
to determine the highest and lowest values of the PRICE field, which are then output.

• A group consists of entries which have identical values in the columns listed after
GROUP BY.
• All database fields in the SELECT clause argument list except for aggregate expressions
(CARRID in this case) must be listed in the argument list for the GROUP BY clause.
Slide
14 of
31

ORDER BY Clause

The ORDER BY clause determines how the entries in the result table are sorted.

• ...ORDER BY PRIMARY KEY sorts the entries according to the primary key of the
database table in ascending order.
• ...ORDER BY <field 1>...<field n> sorts the entries according to the table fields
specified.
• You can determine the sort order using the ASCENDING or DESCENDING addition.
• The default sort order is ascending.
Slide
15 of
31


• Dynamic SORT
• The field name for the sort field is obtained from the field value from the
PARAMETERS screen.
• To sort by more than one column, then a different PARAMETERS field is needed for
each sort column. Each of these is enclosed in parentheses following the BY keyword.
That is, each must be entered separately and use a different variable to store the input for
use in the SORT command.
Slide
16 of
31


• Messages
• Messages are used to advise the user of a condition that affects processing. This could be
an error condition or the successful completion of a processing activity. Although
messages are used most frequently with dialog programs, they can also be used with the
PARAMETERS and SELECT-OPTIONS screens.
• For example, if a user enters an airline such as BB that is not in a database table and click
the Execute button, then a dialog box with a message can be displayed indicating the
condition. When the user clicks the message dialog box Enter button, processing returns
to the PARAMETERS or SELECT-OPTIONS screen where a different value can be
enter.
• Because messages are designed for use with dialog programs, some of the features of
messages used when dialog programs may produce a somewhat different response with
used with a PARAMETERS or SELECT-OPTIONS screen.
• Messages are stored in a message class table that can be used by many different
programs. Messages are language dependent so that a user selecting a different language
will obtain the message in the selected language.
Slide
17 of
31


• Message Dialog Box
• This example is for the information dialog box . The message displayed is a
combination of the text in the message table and a field value from the SELECT-
OPTIONS table. Up to four variables may provide values included within the message.
• It is not necessary to have any field values in the message, however as shown in this
example the value is useful indicating the problem with the input data of the user.
• The message types and their meanings are as follows:
Message Type Meaning
a abnormal end
e error
w warning
i information
s success
• When used with the PARAMETERS and SELECT-OPTIONS screens, some of these
message type result in a message that displays in the status bar, rather than a dialog box.
You need to check this for your release of R/3.

Slide
18 of
31


• Display Message Class
• The message class is displayed as a table for entering and changing messages. Like text
symbols, messages are identified by a message number that is referenced in the program.
• An easy way to access the message class table is from your ABAP/4 program. Just
double-click the message number in the MESSAGE statement and the message class
table displays. However, the message class must be created and referenced in the
program before you can display the message class from the program.
• Creation of a message class is described in R/3 User Help.
Slide
19 of
31


• Specify Message Class
• The message class is specified by the message-id addition to the REPORT statement.
This makes the table available for access by the program.
• Double-click the message class to display the Display Message Class screen, then click
the Messages button to display the list of messages in the message class as illustrated
previously. That is, you drill-down from the ABAP program to the Display Message
Class screen.
• You can click the List message classes button to display a list of other available message
classes.
Slide
20 of
31


• Message Statement
• The MESSAGE statement specifies the message type and message number that are
displayed. The ‘a’ message type indicates a stop sign symbol would appear in the dialog
box and that the current program will be terminated when the user clicks the Enter button.

The ‘i’ message type indicate the information symbol is to appear in the dialog box
as illustrated previously.
• If you double-click the message number, the message class table is displayed for entering
or changing a message in the existing message class specified in the REPORT statement.
• In the MESSAGE statement, the WITH addition is used to specify the fields whose
values will be used in the message. Up to four fields may be listed after the WITH
keyword.
Slide
21 of
31

SAP Authorization Principles

All data in the R/3 System must be protected against access by unauthorized users.

• Authorizations are assigned in user master maintenance. You can determine explicitly
which data a user should be able to access and what type of access is possible. For
example, you may want a user to be able to display the data of all airline carriers, but not
be authorized to make any changes. During each authorization check, the system checks
the ‘activity’ and ‘airline carrier’ fields. When defining authorizations, you must specify
values for these fields (for example, activity ‘Change’ and airline carrier 'LH' or activity
'Display', airline '*'). To do this, you need to create an authorization object consisting of
the 'activity' and 'airline carrier' fields and address this object both when you assign
authorizations in user master records and when you perform authorization checks in
programs.
• Authorization objects themselves only define the combination of fields to be addressed
simultaneously and serve as templates for both authorizations and authorization checks.
To make the maintenance and identification of authorization objects easier, they are
combined to form object classes. One or more classes are assigned to an application.
From the Development menu on the initial screen of the ABAP/4 Development
Workbench, you can access the maintenance transaction for authorization objects and
display a list of all objects with their corresponding fields and documentation.

Slide
22 of
31

Authorization Checks in Programs

When making authorization checks in programs, you specify the object and the values the user
needs in an authorization to be able to access the object. You do not have to specify the name of
the authorization.

• The above example checks whether the user is authorized for the object S_CARRID
which has the value 'LH' in the field CARRID (airline carrier) and the value '02' for
'Change' in the field ACTVT (activity). The abbreviations for the possible activities are
documented in the tables TACT and TACTZ and also in the appropriate objects.
• Important: The AUTHORITY-CHECK statement performs the authorization check and
returns an appropriate return code value. When reading this return code, you can specify
yourself the consequences of a missing authorization (for example, program terminates or
skips some output lines).
Slide
23 of
31

AUTHORITY-CHECK Syntax

You must specify all fields of the object in an AUTHORITY-CHECK. If you do not want to
carry out a check for a particular field, enter DUMMY after the field name.

• Example:
When calling a transaction to change flight data, it makes sense to check whether the user
is authorized to change the entries for a particular airline carrier:

AUTHORITY-CHECK OBJECT 'S_CARRID'


ID 'ACTVT' FIELD '02'
ID 'CARRID' DUMMY.

• The most important return codes for AUTHORITY-CHECK are:


0 The user has an authorization containing the required values.
4 The user does not have the required authorization.
24 The check could not successfully be carried out since not all fields of
the object were specified.
• The online documentation for AUTHORITY-CHECK contains a complete list of return
codes.
• You can only specify a single field after the FIELD addition, not a selection table. There
are function modules which carry out the AUTHORITY-CHECK for all values in the
selection table.

Slide
24 of
31

Authorization Action

You perform an authorization check for each record read with SELECT. The AUTHORITY-
CHECK statement first checks whether the user has the authorization containing all the required
values. You then read the return code value in the system field SY-SUBRC. If this value is 0, the
user has the required authorization and the program can continue. If the value is something other
than 0, the user does not possess the required authorization and the system outputs an appropriate
message.

• Later in this course, you will learn how to make fields on the selection
screen ready for input again if you perform the authorization check right after the
selection screen, and how to output a message if the user does not have the
required authorization.

Вам также может понравиться