In order to retrieve stored Data from SAP Tables, ABAP programs use Open SQL or Native SQL to read, update or delete entries in the persistance layer (SAP db).
In this article, we will discuss What’s Open SQL, then What’s Native SQL illustrated by 2 samples.
Table of Contents
What’s Open SQL ?
Open SQL consists of a set of ABAP statements that perform operation on central database in the R/3 System. The results of the operations and any error messages are independent of the database system in use. Open SQL thus provides a uniform syntax and semantics for all of database systems supported by SAP. ABAP programs that only use Open SQL statements will work in any SAP R/3 System, regardless of the database system in use. Open SQL statements can work with database tables that have been created in the ABAP Dictionary.
The method actually is simple that when a programmer writes an ABAP program with Open SQL statements, the kernel SAP programs convert Open SQL statements to real / native SQL statements for database in use. So like that write once, run for all databases and even for all operating systems. Like Java’s “Write Once. Run Anywhere“. Think about Java, even the Java uses the same principal that is Java Virtual Machine which looks like SAP’s kernel programs. Right? Can we say SAP did “Write Once. Run Anywhere” before Java?
Open SQL contains the following keywords:
- SELECT – Reads data from database tables.
- INSERT – Adds lines to database tables.
- UPDATE – Changes the contents of lines of database tables.
- MODIFY – Inserts lines into database tables or changes the contents of existing lines.
- DELETE – Delete lines from database tables.
- OPEN CURSOR, FETCH, CLOSE CURSOR – Reads lines of database tables using the cursor.
All Open SQL statements fill the following two system fields with return codes:
After every Open SQL statement, the system field SY-SUBRC contains 0 if the operation was successful, a value other than 0 if not.
After an OPEN SQL statement, the system field SY-DBCNT contains the number of database lines processed.
Check the official definition on help.sap.com
What is Native SQL (in SAP ABAP)
Native SQL is real SQL for database in use. It means beside OPEN SQL, if you need you can use the native SQL for databases. 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, and therefore integrate data that is not part of the R/3 System.
As a rule, an ABAP program containing database-specific SQL statements will not run under different database systems. If your program will be used on more than one database platform, only use Open SQL statements.
Native SQL Vs Open SAP
All ABAP programs in SAP R/3 System have been written with Open SQL. I think if you have a different database instant in the same database, you can use Native SQL statement to connect and do operation on this database instant.
Let’s assume you have an SAP R/3 system that uses Oracle database instant ORC1. You have another application, even it uses the same database Oracle, but as normally different database instant ORC2. So like data inside ABAP program, you can use Native SQL statements to connect ORC2, non-SAP database instant, to integrate SAP R/3 and non-SAP system. It is kind of an integration activity.
If you create a table by using database tools, without ABAP Dictionary, you are not able to use Open SQL to reach this table. You just can use Native SQL to do that.
Native SQL statements bypass the R/3 database interface. There is no table logging, and no synchronization with the database buffer on the application server. For this reason, you should, wherever possible, use Open SQL to change database tables declared in the ABAP Dictionary. In particular, tables declared in the ABAP Dictionary that contain log columns with types LCHR and LRAW should only be addressed using Open SQL, since the columns contain extra, database-specific length information for the column. Native SQL does not take this information into account, and may therefore produce incorrect results. Furthermore, Native SQL does not support automatic client handling. Instead, you must treat client fields like any other.
To ensure that transactions in the R/3 System are consistent, you should not use any transaction control statements (COMMIT, ROLLBACK WORK), or any statements that set transaction parameters (isolation level…) using Native SQL.
Use of Native SQL
Using Native SQL, you can
- Transfer values from ABAP fields to the database
- Read data from the database and process it in ABAP programs.
Native SQL works without the administrative data about database tables stored in the ABAP Dictionary. Consequently, it cannot perform all of the consistency check used in Open SQL. This places a larger degree responsibility on application developers to work with ABAP fields of the correct type. You should always ensure that the ABAP data type and the type of database column are identical.
What does an EXEC SQL statement do in ABAP?
Syntax of EXEC SQL in SAP ABAP
EXEC SQL [PERFORMING subr].
What’s EXEC SQL’s Effect ?
These statements define an area in an ABAP program in which one or more Native SQL statements are to be carried out. The area between EXEC and ENDEXEC is not completely checked by the syntax check. The statements entered there are passed to the Native SQL interface and processed there as follows:
Almost all SQL statements that are valid for the addressed database system can be included between EXEC and ENDEXEC, in particular the DDL statements. These SQL statements are passed from the Native SQL interface to the database system largely unchanged. The syntax rules are specified by the database system, in particular the case sensitivity rules for database objects. If the syntax allows a separator character between individual statements, you can include several Native SQL statements between EXEC and ENDEXEC. Generally, the semicolon (;) is used as the separator character.
- You can also include SAP-specific Native SQL language elements between EXEC and ENDEXEC. These statements are not passed directly from the Native SQL interface to the database, but are converted appropriately. These SAP-specific language elements are::
- Host variables
- Statements for cursor processing
- Database procedure calls
- Statements for establishing database connections
All Native SQL statements bypass SAP buffering. Automatic client handling is not performed.
The statement ENDEXEC sets the system fields sy-subrc and sy-dbcnt. When using the addition PERFORMING, note that implicit cursor processing is carried out and the system fields are set for every read process.
After implicit cursor processing with PERFORMING, sy-subrc always contains the value 4.
|The statements between EXEC and ENDEXEC were executed successfully.|
|4||The statements between EXEC and ENDEXEC were not executed.|
The ENDEXEC statement sets sy-dbcnt to the number of table rows processed in the last Native SQL statement. So After implicit cursor processing with PERFORMING, sy-dbcnt contains the total number of lines read.
Native SQL Advantages and Disadvantages – EXEC SQL statement
Advantages of Native SQL
- Tables are not declared in ABAP Dictionary can be accessed. (e.g. Tables belonging to sys or system user of Oracle, etc.)
- To use some of the special features supported by the database-specific SQL. (e.g. Passing hints to Oracle optimizer.)
Native SQL Disadvantages
- No syntax check is performed whatever is written between EXEC and ENDEXEC.
- ABAP program containing database-specific SQL statements will not run under different database systems.
- There is no automatic client handling for client dependent tables.
- Care has to be taken during migration to higher versions.
Sample of Native SQLI
Inserting two rows in the database table SCARR. If neither of these rows exists, sy-subrc is set to 0 by ENDEXEC and sy-dbcnt to 1. Otherwise, an exception is raised and handled.
DATA: exc_ref TYPE REF TO cx_sy_native_sql_error,
error_text TYPE string.
INSERT INTO scarr
(MANDT, CARRID, CARRNAME, CURRCODE, URL)
VALUES ('000', 'FF', 'Funny Flyers', 'EUR', 'http://www.ff.com');
INSERT INTO scarr
(MANDT, CARRID, CARRNAME, CURRCODE, URL)
VALUES ('000', 'EF', 'Easy Flyers', 'EUR', 'http://www.ef.com');
CATCH cx_sy_native_sql_error INTO exc_ref.
error_text = exc_ref->get_text( ).
MESSAGE error_text TYPE 'I'.
Reading several rows from the database table SPFLI using cursor handling and host variables in native SQL. If rows were found, sy-subrc is set to 0 and sy-dbcnt is increased by one for each row read.
PARAMETERS p_carrid TYPE spfli-carrid.
DATA: connid TYPE spfli-connid,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto.
OPEN dbcur FOR
SELECT connid, cityfrom, cityto
WHERE carrid = :p_carrid
FETCH NEXT dbcur INTO :connid, :cityfrom, :cityto
write: / connid,cityfrom,cityto.
IF sy-subrc <> 0.