Escolar Documentos
Profissional Documentos
Cultura Documentos
In a SAP system, data from a database can be accessed by an ABAP program by using the following type of statements: Open SQL It 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. A native SQL statement is used within the EXEC SQL and ENDEXEC statements.
I.
Fields - name of field, key, data element, data type, length, short description etc.
Database Access
Read database data into internal tables. Internal tables are
tables for holding data during runtime. Change data per row use work areas to hold one row of the table and change data in the work area. Write changes back to database.
Inserting data into tableThe Open SQL statement for inserting data into a database table is: INSERT INTO <target> <lines>. It allows you to insert one or more lines into the database table <target>. You can specify the database table <target> either statically or dynamically.
Specifying a Database Table To specify the database table statically, enter the following for <target>: INSERT INTO <dbtab> <lines>. where <dbtab> is the name of a database table defined in the ABAP Dictionary. To specify the database table dynamically, enter the following for <target>: INSERT INTO (<name>) <lines>. where the field <name> contains the name of a database table defined in the ABAP Dictionary.
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.
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.
Specifying a Database Table To specify the database table statically, enter the following for <target>: UPDATE <dbtab> <lines>. where <dbtab> is the name of a database table defined in the ABAP Dictionary. To specify the database table dynamically, enter the following for <target>: UPDATE (<name>) <lines>. where the field <name> contains the name of a database table defined in the ABAP Dictionary.
The expressions <set i > are three different SET statements that determine the columns to be changed, and how they are to be changed: <si> = <f> The value in column <si> is set to the value <f> for all lines selected. <si> = <si> + <f> The value in column <si> is increased by the value of <f> for all lines selected. <si> = <si> - <f> The value in column <si> is decreased by the value of <f> for all lines selected.
<f> 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-
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 <target> FROM <wa> . The contents of the work area <wa> overwrite the line in the database table <dbtab> 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.
A shortened form of the above statement is: UPDATE <dbtab>. In this case, the contents of the table work area <dbtab> 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 database table dynamically.
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 <target> FROM TABLE <itab> .
The contents of the internal table <itab> overwrite the lines in the database table <dbtab> that have the same primary keys. The same rules apply to the line type of <itab> as to the work area <wa> described above.
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 SYDBCNT 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 a database table, it is more efficient to work with an internal table than to change the lines one by one.
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 in SFLIGHT where CARRID contains LH and CONNID contains 402.
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.
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 work area WA or the table work area SPFLI.
Reading Data from database tableThe 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>].
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.
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.
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_flightscityto. endloop. else. write: 'sql statement was not successfully'. endif.
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 SYSUBRC 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.
Data Types in ABAP Dictionary You can assign a predefined ABAP Dictionary type and a number of characters to an elementary type. The ABAP Dictionary 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.
Maximum length n ABAP type 1-31, 1-17 in tables 3 P((n+1)/2) Internal only
INT2
INT4 CURR CUKY QUAN
Two-byte integer
Four-byte integer Currency field Currency key Quantity
5
10 1-17 5 1-17
Internal only
I P((n+1)/2) C(5) P((n+1)/2)
UNIT
Unit
2-3
C(n)
PREC
Accuracy
16
Internal only
FLTP
NUMC CHAR
16
1-255 1-255
F(8)
N(n) C(n)
LCHR
STRING RAWSTRING
Long character
String of variable length Byte sequence of variable length
256-max
1-max 1-max
C(n)
STRING XSTRING
DATS
Date
ACCP
N(6)
TIMS
RAW LRAW
6
1-255 256-max
T
X(n) X(n)
CLNT
LANG DATS
Client
Language Date
C(3)