Sie sind auf Seite 1von 37

Data Retrieval and Field Symbols

Database Tables and SQL Concepts

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.

Native SQL statements access the database directly.

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.

Open SQL keywords


SELECT - Reading Data from Database Tables
INSERT - Adding Lines to Database Tables
UPDATE - Changing Lines in Database Tables
MODIFY - Adding or Changing Lines
DELETE - Deleting Lines from Database Tables

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

To read data from a database table, use the SELECT statement.

The three variants of the SELECT clause are described in the following topics:

Selecting All Data from Several Lines

Selecting All Data from a Single Line

Selecting and Processing Data from Specific Columns

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.

Selecting All Data from a Single Line


Example
TABLES SPFLI.
SELECT SINGLE * FROM SPFLI WHERE CARRID EQ 'LH AND CONNID EQ '2407'.
WRITE: / SPFLI-CARRID, SPFLI-CONNID, SPFLI-CITYFROM,SPFLI-CITYTO.

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.

Reading Data into a Work Area

Reading Data into an Internal Table

Reading Data Component by Component

10
Reading Data into a Work Area

Example

TABLES SPFLI.

DATA WA LIKE SPFLI.

SELECT * FROM SPFLI INTO WA.

WRITE: / WA-CITYFROM, WA-CITYTO.

ENDSELECT.

11
Reading Data into an Internal Table

Example
TABLES SPFLI.

DATA ITAB LIKE SPFLI OCCURS 10 WITH HEADER LINE.

SELECT * FROM SPFLI INTO TABLE ITAB

WHERE CARRID = 'LH'.

LOOP AT ITAB.

WRITE: / ITAB-CONNID, ITAB-CARRID.

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,

ITAB_SPFLI LIKE SPFLI.

SELECT * FROM ( NAME ) INTO ITAB_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

DATA wa TYPE scarr.

SELECT *

INTO wa

FROM scarr UP TO 4 ROWS.

WRITE: / wa-carrid, wa-carrname.

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:

SELECT... FROM <tables> CLIENT SPECIFIED. ..

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

SELECT * FROM <table>


WHERE <table field 1> <relational operator> <field 1>
AND <table field 2> <relational operator> <field 2>
OR <table field 3> <relational operator> <field 3>
AND <table field 4> <relational operator> <field 4>
:
:
OR<table field n> <relational operator> <field n>.

ENDSELECT.
EQ =
GE >= =>
LE <= =<
Relational
NE <> ><
Operators
GT >
LT <

17
Syntax: Between Values, Templates and Lists

SELECT * FROM <table> WHERE <table field>...

BETWEEN <field 1> AND <field 2>

LIKE <with % and _ masked literal>

IN (<field 1, field 2,......field n>)

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.

select * from ytabna The result set is limited by the


where country eq USA conditions in the
and id between 0000001 WHERE clause.
and 0000002 .
write: / ytabna-country, ytabna-id.
ytabna-name1.
endselect.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.

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

select * from t001


where bukrs like __01
and butxt like SAP%.
write: / t001-bukrs, t001-butxt.
endselect.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.

20
IN: Example of a Comparison Value List

tables: ytabna.

parameters: country1 like ytabna-country.


country2 like ytabna-country.

select * from ytabna


where country in (country1, country2).

* where country = country1 or country = country2.

write: / ytabna-country, ytabna-id,


ytabna-name1.
endselect.

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

SELECT * FROM <table>

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

DATA: BEGIN OF wa,


carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa.
DATA : itab like standard table of wa occurs 0 with header line.
SELECT s~carrid s~carrname p~connid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM scarr AS s
LEFT OUTER JOIN spfli AS p ON s~carrid = p~carrid AND
p~cityfrom = 'FRANKFURT'.

27
Inserting Lines into Tables

INSERT INTO <target> <lines>.

INSERT INTO <dbtab> [CLIENT SPECIFIED] <lines>.

INSERT INTO <target> VALUES <wa> .

INSERT <target> FROM <wa >.

INSERT <dbtab>.

28
Changing Lines

UPDATE <target> <lines>.

UPDATE <dbtab> [CLIENT SPECIFIED] <lines>.

UPDATE <target> SET <set1> <set 2> ...

[WHERE <cond>].

UPDATE <target> FROM <wa> .

UPDATE <dbtab>.

29
Deleting Lines

DELETE [FROM] <target> <lines>.

DELETE [FROM] <dbtab> [CLIENT SPECIFIED] <lines>.

DELETE FROM <target> WHERE <cond> .

DELETE <target> FROM <wa> .

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

EXEC SQL [PERFORMING <form>].


<Native SQL statement>

ENDEXEC.

Stored Procedures

EXEC SQL

EXECUTE PROCEDURE <name> ( <parameter list> )

ENDEXEC.

31
Performance Notes

Keep the Result Set Small

Using the WHERE Clause

Using the HAVING Clause

Minimize the Amount of Data Transferred

Restrict the Number of Lines

Restrict the Number of Columns

Use Aggregate Functions

32
Performance Notes contd.

Minimize the Number of Data Transfers


Avoid Repeated Access
Avoid Nested SELECT Loops
Joins in the FROM Clause
Minimize the Search Overhead
Database Indexes
Checking for Null Values
Avoid Complex Conditions
Reduce the Database Load
Buffer Tables on the Application Server

33
Field Symbols

FIELD-SYMBOLS <fs>.
fs - name the field symbol.

Placeholders for existing fields.


Does not physically reserve space for a field, but points to a field which is not
known until runtime of the program.
You can specify the offset and length of the assigned field as variables.
You can assign field symbols to other field symbols and even specify offset and
length there.

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.

Type can also be specified while defining the field symbols

FIELD-SYMBOLS <fs> TYPE type.

The field symbols can be declared even as structures

FIELD-SYMBOLS <fs>STRUCTURE s DEFAULT wa

( Fields of the structure - <fs>-fieldname )

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 :

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.


DATA: text(20) TYPE c VALUE 'Hello, how are you?',
num TYPE i VALUE 5,
BEGIN OF line1,
col1 TYPE f VALUE '1.1e+10',
col2 TYPE i VALUE '1234',
END OF line1,
line2 LIKE line1.
ASSIGN text TO <f1>.ASSIGN num TO <f2>.
WRITE: / <f1>,<f2>.
Output :
Hello, how are you? 5

36
Thank You

Das könnte Ihnen auch gefallen