The other possible way to access the database from a program
is Native SQL. It is only loosely integrated into ABAP. It allows access to all
of the functions contained in the programming interface of the respective
database system. Native SQL statements are not checked and converted as
compared to Open SQL statements. Unlike Open SQL, these are sent directly to
the database system. The function of the database-dependent layer remains
minimal when you use Native SQL. The Programs which use Native SQL are written
specifically for a database system. You should avoid using Native SQL wherever
possible when writing R/3 applications. However, you can use it in some parts
of the R/3 Basis System, for instance, for creating or changing table
definitions in the ABAP Dictionary.
Regardless of the database platform that your R/3 system is
using, Open SQL allows you to access database tables, which are declared in the
ABAP Dictionary. Native SQL allows you to use database specific SQL statements
in an ABAP program. This means that you can use database tables that are not
administered by the ABAP Dictionary. Aside from that, you can also integrate
data that is not part of the R/3 system.
As a rule, an ABAP program that contains database-specific
SQL statements will not run under different database systems. You have to use
Open SQL statements only, if your program is used on more than one database
platform.
You must proceed with the EXEC SQL statement, and follow the
ENDEXEC statement to use a: Native SQL statement. For example
Listing 1
EXEC SQL [PERFORMING <form>].
<native SQL, statement>
ENDEXEC.
There is no period after Native SQL statements. Also, using quotation
marks (") or an asterisk (*) at the beginning of a native SQL statement's
line does not introduce a comment as it would in normal ABAP syntax. You need
to know if the table and field names are case-sensitive in your chosen
database.
The data is transported between the database table and the
ABAP program using host variables in Native SQL statements. These are preceded
in a Native SQL statement by a colon (:) and are declared in the ABAP program.
The elementary structures can be used as host variables. The structures of an
INTO clause are treated exceptionally, as though all of their fields are listed
individually. If the selection in a Native SQL SELECT statement is a table,
then you can pass it to ABAP line by line using the PERFORMING addition. For
each line read, the program calls a subroutine <form>. Further, you can
process the data within the subroutine.
After the ENDEXEC statement, SY-DBCNT contains the number of
lines processed as it does in Open SQL. In almost all cases, SY-SUBRC contains
the value a after the ENDEXEC statement. Cursor operations form an exception: after
FETCH, SY-SUBRC is 4 if no more records could be read. This is also applied
when you read a result set using EXEC SQL PERFORMING.