8/13/2019 Database Access 01 (1)
1/36
Accessing Database tables
In a SAP system, data from a database can be accessed by
an ABAP program by using the following type of statements:Open SQLIt is a subset of standard SQL, consists of a
set of ABAP statements that perform operations on the
database tables. It provides a uniform syntax and semantics
for all the database systems supported by SAP. Open SQL
statements can work with database tables that have been
created in the ABAP dictionary.
Native SQL- It contains only database manipulation
statements. Database tables that are not administered by the
ABAP dictionary can be accessed by the Native SQL. Anative SQL statement is used within the EXEC SQL and
ENDEXEC statements.
8/13/2019 Database Access 01 (1)
2/36
Steps to create Database Tables:
Go to ABAP Dictionary.
Choose the Database table and type in the table name spfli.
Then use the Display button to have a look at the table
definition. The following tabs are available.
Attributes- The attribute tab show you to which package the
table is assigned and who did the last changes.
Delivery and maintenanceThis is a very important tab as it
show you to which delivery class the table is assigned and if
changes to the table are allowed or not.
8/13/2019 Database Access 01 (1)
3/36
8/13/2019 Database Access 01 (1)
4/36
I. Display/Maintenance Allowed with Restrictions
II. Display/Maintenance Allowed
III. Display/Maintenance Not Allowed
8/13/2019 Database Access 01 (1)
5/36
Fields - name of field, key, data element, data type, length,
short description etc.
Entry help/checkThis tab shows you if any check tables
are used and if there is a search help available.
Currency/quantity fieldsThe last tab is important when the
table contains currency data as it is necessary to choose a
reference currency from a customizing table then.
8/13/2019 Database Access 01 (1)
6/36
8/13/2019 Database Access 01 (1)
7/36
8/13/2019 Database Access 01 (1)
8/36
8/13/2019 Database Access 01 (1)
9/36
Commonly Used Open SQL Statements
Select Reads data from databasetables
Insert Add lines to tables
Update Changes the contents of a
line
Delete Delete lines of table
Open cursor, fetch, close
cursor
Read lines of database
tables using the cursor
8/13/2019 Database Access 01 (1)
10/36
Database Access
Read database data into internal tables. Internal tables are
tables for holding data during runtime.
Change data per rowuse work areas to hold one row of
the table and change data in the work area.
Write changes back to database.
8/13/2019 Database Access 01 (1)
11/36
Inserting data into table-
The Open SQL statement for inserting data into a
database table is:
INSERT INTO .
It allows you to insert one or more lines into the
database table .
You can specify the database table either
statically or dynamically.
8/13/2019 Database Access 01 (1)
12/36
Specifying a Database Table
To specify the database table statically, enter the
following for :INSERT INTO .
where is the name of a database table
defined in the ABAP Dictionary.
To specify the database table dynamically, enter thefollowing for :
INSERT INTO () .
where the field contains the name of adatabase table defined in the ABAP Dictionary.
8/13/2019 Database Access 01 (1)
13/36
Inserting a Single Line
To insert a single line into a database table, use thefollowing:
INSERT INTO VALUES .
You can also insert single lines using the followingshortened form of the INSERT statement:
INSERT FROM .
Using FROM instead of VALUE allows you to omit the INTO
clause.
8/13/2019 Database Access 01 (1)
14/36
tables spfli.
data wa type spfli.
wa-carrid = 'lh'.wa-cityfrom = 'washington'.
...
insert into spfli values wa.
wa-carrid = 'ua'.
wa-cityfrom = 'london'.
...insert spfli from wa.
8/13/2019 Database Access 01 (1)
15/36
spfli-carrid = 'lh'.
spfli-cityfrom = 'berlin'.
...insert spfli.
This program inserts a single line into the database
table SPFLI using each of the three possible variants of
the INSERT statement.
8/13/2019 Database Access 01 (1)
16/36
Updating database rows
The Open SQL statement for changing data in a
database table is:
UPDATE .
It allows you to change one or more lines in the database
table . You can only change lines in an ABAP
Dictionary view if it only contains fields from one table,and its maintenance status is defined as Read and
change. You may specify the database table
either statically or dynamically.
8/13/2019 Database Access 01 (1)
17/36
Specifying a Database Table
To specify the database table statically, enter the
following for :UPDATE .
where is the name of a database table defined
in the ABAP Dictionary.
To specify the database table dynamically, enter the
following for :
UPDATE () .where the field contains the name of a
database table defined in the ABAP Dictionary.
8/13/2019 Database Access 01 (1)
18/36
Changing Lines Column by Column
To change certain columns in the database table, use the
following:
UPDATE SET ... [WHERE ].
The WHERE clause determines the lines that are changed. If you
do not specify a WHERE clause, all lines are changed.
8/13/2019 Database Access 01 (1)
19/36
The expressions are three different SET
statements that determine the columns to be
changed, and how they are to be changed: =
The value in column is set to the value for
all lines selected.
= +
The value in column is increased by the value
of for all lines selected.
= -
The value in column is decreased by the value
of for all lines selected.
8/13/2019 Database Access 01 (1)
20/36
can be a data object or a column of the database
table. You can use the attributes of the table using their
direct names.
If at least one line is changed, the system sets SY-
SUBRC to 0, otherwise to 4. SY-DBCNT contains thenumber of lines changed.
If you use SET statements, you cannot specify the
database table dynamically.
8/13/2019 Database Access 01 (1)
21/36
Overwriting Individual Lines From a Work Area
To overwrite a single line in a database table with the
contents of a work area, use the following:
UPDATE FROM .
The contents of the work area overwrite the line in
the database table that has the same primary key.
If the database table contains a line with the same primary
key as specified in the work area, the operation is completed
successfully and SY-SUBRC is set to 0. Otherwise, the line is
not updated, and SY-SUBRC is set to 4.
8/13/2019 Database Access 01 (1)
22/36
A shortened form of the above statement is:
UPDATE .
In this case, the contents of the table work area are used to update the database table with
the same name. You must declare this table work
area using the TABLES statement. In this case, it is
not possible to specify the name of the databasetable dynamically.
8/13/2019 Database Access 01 (1)
23/36
Overwriting Several Lines Using an Internal Table
To overwrite several lines in a database table with the
contents of an internal table, use the following:UPDATE FROM TABLE .
The contents of the internal table overwrite the
lines in the database table that have the sameprimary keys. The same rules apply to the line type of
as to the work area described above.
8/13/2019 Database Access 01 (1)
24/36
If the system cannot change a line because no line with
the specified key exists, it does not terminate the
operation, but continues processing the next line of the
internal table.If all lines from the internal table have been processed,
SY-SUBRC is set to 0. Otherwise, it is set to 4. If not all
lines are used, you can calculate the number of unused
lines by subtracting the number of processed lines in SY-DBCNT from the total number of lines in the internal table.
If the internal table is empty, SY-SUBRC and SY-DBCNT
are set to 0.
Whenever you want to overwrite more than one line in adatabase table, it is more efficient to work with an internal
table than to change the lines one by one.
8/13/2019 Database Access 01 (1)
25/36
Example -
UPDATE SFLIGHT SET PLANETYPE = 'A310'
PRICE = PRICE - '100.00'WHERE CARRID = 'LH' AND CONNID = '0402'.
This example overwrites the contents of the
PLANETYPE column with A310 and decreases the
value of the PRICE column by 100 for the entry inSFLIGHT where CARRID contains LH and
CONNID contains 402.
8/13/2019 Database Access 01 (1)
26/36
TABLES SPFLI.
DATA WA TYPE SPFLI.
MOVE 'AA' TO WA-CARRID.MOVE '0064' TO WA-CONNID.
MOVE 'WASHINGTON' TO WA-CITYFROM.
...
UPDATE SPFLI FROM WA.
MOVE 'LH' TO SPFLI-CARRID.
MOVE '0017' TO SPFLI-CONNID.
MOVE 'BERLIN' TO SPFLI-CITYFROM.
...
UPDATE SPFLI.
8/13/2019 Database Access 01 (1)
27/36
In this example, CARRID and CONNID are the primary key
fields of table SPFLI. All fields of those lines where the primary
key fields are "AA" and "0064", or "LH" and "0017", are
replaced by the values in the corresponding fields of the workarea WA or the table work area SPFLI.
8/13/2019 Database Access 01 (1)
28/36
Reading Data from database table-
The Open SQL statement for reading data from database
tables is:
SELECT
INTO
FROM
[WHERE ]
[GROUP BY ][HAVING ]
[ORDER BY ].
8/13/2019 Database Access 01 (1)
29/36
REPORT ZY_PRGM1.
tables zzemp320.
data: itab type table of zzemp320, wa type zzemp320.wa-empid = 11.
wa-name = 'ss'.
insert into zzemp320 values wa.
write sy-subrc.
select * from zzemp320 into table itab.
loop at itab into wa.
write: / wa-empid,wa-name.endloop.
8/13/2019 Database Access 01 (1)
30/36
REPORT ZY_100_FLIGHTS.
data it_flights type table of spfli.
data wa_flights type spfli.select * from spfli into table it_flights.
loop at it_flights into wa_flights.
write: / wa_flights-connid,wa_flights-cityfrom,
wa_flights-cityto.
endloop.
8/13/2019 Database Access 01 (1)
31/36
REPORT ZY_100_FLIGHTS.
data it_flights type table of spfli.
data wa_flights type spfli.select * from spfli into table it_flights.
if sy-subrc = 0.
loop at it_flights into wa_flights.
write: / wa_flights-connid,wa_flights-cityfrom,wa_flights-
cityto.
endloop.
else.
write: 'sql statement was not successfully'.
endif.
8/13/2019 Database Access 01 (1)
32/36
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.
8/13/2019 Database Access 01 (1)
33/36
Data Types in ABAP Dictionary
You can assign a predefined ABAP Dictionary type and a
number of characters to an elementary type. The ABAPDictionary has considerably more predefined types than the
ABAP programming language. The number of characters here
is not the field length in bytes, but the number of valid
characters excluding formatting characters. The data types are
different because the predefined data types in the ABAP
Dictionary have to be compatible with the external data types
of the database tables supported by the SAP Web AS ABAP.
8/13/2019 Database Access 01 (1)
34/36
Dictionary type Meaning Maximum length n ABAP type
DEC Calculation/amount
field
1-31, 1-17 in tables P((n+1)/2)
INT1 Single-byte integer 3 Internal only
INT2 Two-byte integer 5 Internal only
INT4 Four-byte integer 10 I
CURR Currency field 1-17 P((n+1)/2)
CUKY Currency key 5 C(5)
QUAN Quantity 1-17 P((n+1)/2)
8/13/2019 Database Access 01 (1)
35/36
8/13/2019 Database Access 01 (1)
36/36
DATS Date 8 D
ACCP Accounting period
YYYYMM
6 N(6)
TIMS Time HHMMSS 6 T
RAW Byte sequence 1-255 X(n)
LRAW Long byte sequence 256-max X(n)
CLNT Client 3 C(3)
LANG Language internal 1, external 2 C(1)
DATS Date 8 D
Top Related