Você está na página 1de 6

2/26/2019 Open SQL & Native SQL in SAP ABAP

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

NLTK (/nltk-tutorial.html) TensorFlow (/tensorflow-


tutorial.html)

R Programming (/r-
tutorial.html)

Open SQL & Native SQL in SAP ABAP


The goal of this tutorial is not to teach you
SQL (/sql.html)or database concepts but to 
introduce you to the SQL diversity in ABAP

In ABAP/4 programming language, there are


two types of SQL being used.

1. NATIVE SQL
2. OPEN SQL.

Open SQL allows you to access the database


tables declared in the ABAP dictionary regardless of the database platform that the R/3
system is using.

Native SQL allows you to use database-specific SQL statements in an ABAP/4 program. This
means that you can use database tables that are not administered by ABAP dictionary, and
therefore integrate data that is not part of the R/3 system.

Open SQL consists of a set of ABAP statements that perform operations on the 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 the database systems supported by SAP. ABAP programs that only use
Open SQL statements will work in any R/3 system, regardless of the database system in use.
Open SQL statements can only work with database tables that have been been created in
the ABAP dictionary.
https://www.guru99.com/native-open-sql.html 1/9
2/26/2019 Open SQL & Native SQL in SAP ABAP

(https://help.sap.com/static/saphelp_nw70/en/fc/eb3976358411d1829f0000e829fbfe/h-
00100010000_image002.gif)

Basic Open SQL Commands

SELECT
INSERT
UPDATE
MODIFY
DELETE
OPEN CURSOR, FETCH, CLOSE CURSOR

Example

https://www.guru99.com/native-open-sql.html 2/9
2/26/2019 Open SQL & Native SQL in SAP ABAP

TABLES SBOOK.

DATA C TYPE CURSOR,

WA LIKE SBOOK.

OPEN CURSOR C FOR SELECT * FROM SBOOK WHERE CARRID = 'LH '

AND CONNID = '0400'

AND FLDATE = '19950228'

ORDER BY PRIMARY KEY.

DO.

FETCH NEXT CURSOR C INTO WA.

IF SY-SUBRC <> 0.

CLOSE CURSOR C.

EXIT.

ENDIF.

WRITE: / WA-BOOKID, WA-CUSTOMID, WA-CUSTTYPE,

WA-SMOKER, WA-LUGGWEIGHT, WA-WUNIT,

WA-INVOICE.

ENDDO.

 
Output the passenger list for the Lu hansa flight 0400 on 28-02.1995:

Open SQL Return Codes


All Open SQL statements fill the following two system fields with return codes.

SY-SUBRC

New

price drop

https://www.guru99.com/native-open-sql.html 3/9
A er every Open SQL statement, the system
2/26/2019
field SY-SUBRC contains the value 0 if the
Open SQL & Native SQL in SAP ABAP

operation was successful, a value other than 0 if not.

SY-DBCNT

A er an Open SQL statement, the system field SY-DBCNT contains the number of database
lines processed.

Native SQL
As already mentioned, Native SQL allows you to use database-specific SQL statements in an
ABAP program.

To use Native SQL statement, you must precede it with the EXEC SQL statement, and follow
it with the ENDEXEC statement.

Syntax

EXEC SQL [PERFORMING <form>].

<Native SQL statement>

ENDEXEC.

There is no period a er Native SQL statements. Furthermore, using inverted commas (") or
an asterisk (*) at the beginning of a line in a native SQL statement does not introduce a
comment as it would in normal ABAP syntax. You need to know whether table and field
names are case-sensitive in your chosen database.

Ad

Up to 30% Off at Nykaa


Visit Site
Nykaa.com

In Native SQL statements, the data is transported between the database table and the ABAP
program using host variables. These are declared in the ABAP program, and preceded in the
Native SQL statement by a colon (:). You can use elementary structures as host variables.
Exceptionally, structures in an INTO clause are treated as though all of their fields were listed
individually.

As in Open SQL, a er the ENDEXEC statement, SY-DBCNT contains the number of lines
processed. In nearly all cases, SY-SUBRC contains the value 0 a er the ENDEXEC statement.

https://www.guru99.com/native-open-sql.html 4/9
2/26/2019 Open SQL & Native SQL in SAP ABAP
Open SQL - Performance Rules
To improve the performance of the SQL and in turn of the ABAP program, one should take
care of the following rules-

Keep the Result Set Small

Using the where clause


If only one record is required from the database, use SELECT SINGLE whenever possible .

Minimize the Amount of Data Transferred

Restrict the number of lines


If only certain fields are required from a table, use the SELECT <field1> <field2> INTO ...
statement
Restrict no of columns
Use aggregate functions

Minimize the Number of Data Transfers

Avoid nested select loops


An alternative option is to use the SELECT .. FOR ALL ENTRIES statement. This statement
can o en be a lot more e icient than performing a large number of SELECT or SELECT
SINGLE statements during a LOOP of an internal table.

Use dictionary views


Use Joins in the FROM clause
Use subqueries in the where clause

Minimize the Search Overhead

Use index fields in the where clause


When accessing databases, always ensure that the correct index is being used .

Reduce the Database Load

Bu ering
Logical databases
Avoid repeated database access

Using Internal Tables to Bu er Records


https://www.guru99.com/native-open-sql.html 5/9
To avoid executing the same SELECTOpen
2/26/2019
multiple times (and therefore have duplicate
SQL & Native SQL in SAP ABAP

selects), an internal table of type HASHED can be used to improve performance.

 Prev (/sap-macro-include-function-module-group-subroutine.html) Report a Bug

Next  (/all-about-sap-internal-tables.html)

YOU MIGHT LIKE:

SAP-HANA SAP-HANA SAP-QM

(/sap-hana-data- (/sap-hana-sql-data- (/quality-notification-sap-


type.html) (/sap- profiling.html) qm.html) (/quality-
hana-data- (/sap-hana-sql- notification-sap-
type.html) data-profiling.html) qm.html)
SAP HANA Data Type & SAP HANA SQL DATA Quality Notification in SAP
Identifiers Profiling: Learn in 5 Minutes QM: QM02
(/sap-hana-data-type.html) (/sap-hana-sql-data- (/quality-notification-sap-
profiling.html) qm.html)

SAP-HANA SAP-QM SAP-HANA

(/sap-hana-create- (/master-data-sap- (/sap-hana-tutorial.html)


sequence.html) qm.html) (/master- (/sap-hana-
(/sap-hana-create- data-sap-qm.html) tutorial.html)
sequence.html) What is Master Data in SAP? SAP HANA Training Tutorials
SAP HANA Tutorial: Create QS21, QP01, QS31, QDV1, for Beginners
Sequence QI01 (/sap-hana-tutorial.html)
(/sap-hana-create- (/master-data-sap-qm.html)
sequence.html)

https://www.guru99.com/native-open-sql.html 6/9

Você também pode gostar