Site icon DataFlair

Open SQL & Native SQL in SAP ABAP

Open SQL & Native SQL in SAP ABAP

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this tutorial, we’ll be learning about the two types of SQL (Structured Query Language) used in SAP ABAP. Let us dive deep into the world of database enquiry!

What is SQL?

SQL Types in ABAP

SQL has many different types, and in ABAP we use two types of SQL:

  1. OpenSQL
  2. Native SQL

Let us see both these types in detail.

OpenSQL in SAP ABAP

Here are some keywords in OpenSQL:

STATEMENT MEANING
SELECT this statement accesses, reads and displays data from the table
INSERT this statement adds information into the tables (writes)
UPDATE this statement modifies or changes the data within lines of tables
MODIFY this statement either adds lines or changes line content of tables
DELETE this statement deletes data from tables
OPEN CURSOR, FETCH, CLOSE CURSOR this statement uses the cursor to read through the database

OpenSQL statements also have two additional instructions that they perform, called system fields –

FIELD MEANING
SY-SUBRC This system field returns 0 if the operation was successful, and a nonzero value if it was not, to indicate type of error code
SY-DBCNT This system field contains the number of lines from the database that have been processed

OpenSQL Example in ABAP –

TABLES SDATAFLAIR.

DATA C TYPE CURSOR,
WA LIKE SDATAFLAIR.

OPEN CURSOR C FOR SELECT * FROM SDATAFLAIR WHERE CARRID = 'LH '
AND CONNID = '0400'
AND DATE = '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-WUNIT,WA-INVOICE.
ENDDO.

The above code will return the list of all customers of ID = LH & 0400, from date 1995-02-28

Native SQL in SAP ABAP

Native SQL Example in ABAP –

REPORT ZS_DATAFLAIR. 
DATA: BEGIN OF df,  
      connid  TYPE DF1-connid,
      cityfrom TYPE DF1-cityfrom,
      cityto  TYPE DF1-cityto,  
      END OF df. 
    
DATA DF1 TYPE DF1-carrid VALUE 'LH'. 
EXEC SQL PERFORMING loop_output.
   SELECT connid, cityfrom, cityto  
   INTO : df 
   FROM DF1 
   WHERE carrid = : DF1 
ENDEXEC. 

FORM loop_output.  
   WRITE: / df-connid, df-cityfrom, df-cityto. 
ENDFORM.

The above code will output all customers containing carrid = DF1.

OpenSQL vs Native SQL

OpenSQL in ABAP Native SQL in ABAP
OpenSQL statements, much like regular ABAP statements, need a period (.) after the end of each statement. In native SQL, there is no need for a period after the statement. Additionally, it does not follow typical ABAP rules.
OpenSQL allows data from multiple database platforms Native SQL does not allow data from multiple database platforms
Tables that belong to other systems cannot be accessed Tables that belong to other systems can be accessed
Syntax check is performed in between the EXEC and ENDEXEC statements No syntax check is performed

Summary

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Thus, in this tutorial, we learnt about two types of SQL used in ABAP – OpenSQL and native SQL.

We learnt why SQL is used and especially in ABAP. We learnt how to use both kinds of SQL and the difference between them.

Exit mobile version