Beruflich Dokumente
Kultur Dokumente
To make ABAP/4 programs independent of the database system in use, SAP has
created a set of separate SQL statements called Open SQL. Open SQL contains a
subset of standard SQL statements as well as some enhancements which are specific
to SAP. Using Open SQL enables you to access any database tables available to the
SAP system, regardless of the manufacturer.
A database interface translates SAP's Open SQL statements into SQL commands
specific to the database in use.
2
Data Retrieval
3
Data Retrieval contd.
Data Manipulation Language (DML)
Statements for reading and changing data in database tables.
Data Definition Language (DDL)
Statements for creating and administering database tables.
Data Control Language (DCL)
Statements for authorization and consistency checks.
4
Open SQL Statement
The Open SQL statement for reading data from database tables is:
SELECT <result>
INTO <target>
FROM <source>
[WHERE <condition>]
[GROUP BY <fields>]
[HAVING <cond>]
[ORDER BY <fields>].
The SELECT statement is divided into a series of simple clauses, each of which has
a different part to play in selecting, placing, and arranging the data from the
database.
5
Return Codes
All Open SQL statements fill the following two system fields with return codes:
SY-SUBRC
After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the
operation was successful, a value other than 0 if not.
SY-DBCNT
After an open SQL statement, the system field SY-DBCNT contains the number of
database lines processed.
6
Reading Data from Database Tables
The three variants of the SELECT clause are described in the following topics:
7
Selecting All Data from Several Lines
Example:
TABLES SPFLI.
SELECT * FROM SPFLI WHERE CITYFROM EQ 'FRANKFURT'.
WRITE: / SPFLI-CARRID, SPFLI-CONNID,
SPFLI-CITYFROM, SPFLI-CITYTO.
ENDSELECT.
8
Selecting and Processing Data from Specific Columns
Example
Suppose the following database table TEST consists of 5 lines:
COL_1 COL_2
1 3
2 1
3 5
4 7
5 2
TABLES TEST.
DATA RESULT TYPE P DECIMALS 2.
SELECT COL_2 INTO RESULT FROM TEST.
WRITE RESULT.
ENDSELECT.
9
Specifying the Target Area for the Selected Data
To specify the target area for the selected data, you use the INTO clause of the SELECT
statement.
The INTO clause has three main variants which are described in the following topics.
10
Reading Data into a Work Area
Example
TABLES SPFLI.
ENDSELECT.
11
Reading Data into an Internal Table
Example
TABLES SPFLI.
LOOP AT ITAB.
ENDLOOP.
In this example, all lines from the database table SPFLI in which CARRID field contains
"LH" are read into the internal table ITAB, where they can be processed further.
12
Reading Data Component by Component
To read data component by component into the target area, use the CORRESPONDING
FIELDS option of the INTO clause.
TABLES SPFLI.
DATA: BEGIN OF WA,
NUMBER TYPE I VALUE 1,
CITYFROM LIKE SPFLI-CITYFROM,
CITYTO LIKE SPFLI-CITYTO,
END OF WA.
SELECT * FROM SPFLI INTO CORRESPONDING FIELDS OF WA.
WRITE: / WA-NUMBER, WA-CITYFROM, WA-CITYTO.
ENDSELECT.
13
Specifying Database Tables Dynamically
Example
DATA : NAME(10) TYPE C VALUE SPFLI,
LOOP AT ITAB_SPFLI.
WRITE : / ITAB_SPFLI-CITYFROM,
ITAB-SPFLI-CITYTO.
ENDLOOP.
14
Restricting the Number of Lines
To restrict the absolute number of lines included in the selection, use the following
Example
SELECT *
INTO wa
ENDSELECT.
15
Client Handling
You can switch off the automatic client handling in Open SQL statements using a
special addition. In the SELECT statement, the addition comes after the options in the
FROM clause:
If you use this addition, you can then address the client fields in the individual clauses
of the SELECT statement.
16
Syntax: Restricted Loop Processing
ENDSELECT.
EQ =
GE >= =>
LE <= =<
Relational
NE <> ><
Operators
GT >
LT <
17
Syntax: Between Values, Templates and Lists
18
WHERE Clause: Loop Processing with Restricted Selection
report YAP00002.
tables: ytabna.
* The result of the select statement is limited by the conditions of
* the where clause.
19
LIKE: Example of a Template (%_)
*& *
*& *
The _ is an any character
* &-----------------------------------------------------------------------------------------------*
positional parameter.
report YAP00003. The % allows for any string
of any length.
tables: t001.
* Using a template during selection.-----------------------------------------------
20
IN: Example of a Comparison Value List
tables: ytabna.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.
21
IN: Example of a Range
This internal table is the same one
that is created automatically when
tables: lfa1. a selection screen is processed.
data: begin of tab occurs 10. The RANGES statement builds the
sign (1),
option (2),
structure of the table automatically.
low like lfa1-lifnr,
high like lfa1-lifnr,
end of tab. TYPES <range variable> TYPE
move: I to tab-sign,
BT to tab-option,
RANGE OF <data element name>.
0000000001 to tab-low,
9999999999 to tab-high. tables: lfa1.
append tab. Ranges: tab for lfa1-lifnr.
select from lfa1 move: I to tab-sign, DATA: BEGIN OF
where lifnr in tab. BT to tab-option, <name>
write: / lfa1-lifnr, lfa1-name1. 0000000001 to tab-low, OCCURS <n>,
endselect. 9999999999 to tab-high. SIGN(1),
append tab. OPTION(2),
select* from lfa1 LOW LIKE <field>,
where lifnr in tab. HIGH LIKE <field>,
write: / lfa1-lifnr, lfa1-name1.
END OF <name>.
endselect.
22
The ORDER BY Clause
WHERE <condition>
*&----------------------------------------------------------------------------------------------- *
*& Report YAP00006 ORDER * BY <table field 1>
*& * <table field 2>
*&----------------------------------------------------------------------------------------------- * <table field 3>
*& * :
*& * :
*&----------------------------------------------------------------------------------------------- *
<table field n>.
report YAP00006.
PRIMARY KEY.
tables: lfa1.
ENDSELECT.
* Notice the order by clause below.---------------------------------------------- *
select * from lfa1
order by name1.
write: / lfa1-lifnr, lfa1-name1.
endselect.
23
Specifying Two or More Database Tables as an Inner Join
In a relational database, you normally need to read data simultaneously from more than
one database table into an application program. You can read from more than one table in
a single SELECT statement, such that the data in the tables all has to meet the same
conditions, using the following join expression:
SELECT...
...
FROM <tab> [INNER] JOIN <dbtab> [AS <alias>] ON <cond> <options>
...
24
Example
DATA: BEGIN OF wa,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
fldate TYPE sflight-fldate,
bookid TYPE sbook-bookid,
END OF wa.
DATA : itab like standard table of wa occurs 0 with header line.
SELECT p~carrid p~connid f~fldate b~bookid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( spfli AS p
INNER JOIN sflight AS f ON p~carrid = f~carrid AND
p~connid = f~connid )
INNER JOIN sbook AS b ON b~carrid = f~carrid AND
b~connid = f~connid AND
b~fldate = f~fldate )
WHERE p~cityfrom = 'FRANKFURT' AND
p~cityto = 'NEW YORK' AND f~seatsmax > f~seatsocc.
25
Specifying Two or More Database Tables as a Left Outer Join
In an inner join, a line from the left-hand database table or join is only included in the
selection if there is one or more lines in the right-hand database table that meet the ON
condition <cond>. The left outer join, on the other hand, reads lines from the left-hand
database table or join even if there is no corresponding line in the right-hand table.
SELECT...
...
FROM <tab> LEFT [OUTER] JOIN <dbtab> [AS <alias>] ON <cond>
<options>
...
26
Example
27
Inserting Lines into Tables
INSERT <dbtab>.
28
Changing Lines
[WHERE <cond>].
UPDATE <dbtab>.
29
Deleting Lines
DELETE <dbtab>.
30
Native SQL Statements in ABAP Programs
To use a Native SQL statement, you must precede it with the EXEC SQL statement, and
follow it with the ENDEXEC statement as follows
ENDEXEC.
Stored Procedures
EXEC SQL
ENDEXEC.
31
Performance Notes
32
Performance Notes contd.
33
Field Symbols
FIELD-SYMBOLS <fs>.
fs - name the field symbol.
34
Field Symbols contd.
You can force a field symbol to be of a different type and to have a different
number of decimal places than the assigned field.
Field symbols may have a structure that you can use to point to individual
components of structures.
35
Field Symbols Assign Statement
ASSIGN f TO <fs>.
Assigns the field f to the field symbol <fs>. The field symbol <fs> points to the contents
of the field f at runtime.
ASSIGN (f) TO <fs>.
Assigns the field whose name is stored in the field f to the field symbol.
ASSIGN Example :
36
Thank You