0% found this document useful (0 votes)
79 views27 pages

Open SQL

Open SQL is a set of ABAP statements that allows reading, modifying, and deleting data in the SAP database. It is independent of the underlying database, using a uniform syntax across supported databases. Open SQL statements are converted to native SQL and passed to the database. Common Open SQL statements include SELECT, INSERT, UPDATE, DELETE, and MODIFY for retrieving, adding, changing, and removing data from database tables.

Uploaded by

Mostafa Hassanin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views27 pages

Open SQL

Open SQL is a set of ABAP statements that allows reading, modifying, and deleting data in the SAP database. It is independent of the underlying database, using a uniform syntax across supported databases. Open SQL statements are converted to native SQL and passed to the database. Common Open SQL statements include SELECT, INSERT, UPDATE, DELETE, and MODIFY for retrieving, adding, changing, and removing data from database tables.

Uploaded by

Mostafa Hassanin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Open SQL

What is Open SQL?

▪ Open SQL is a set of ABAP statements that performs


operations like reads, modifies or deletes data in the SAP
database.
▪ Open SQL is independent of the database system, so the
syntax of the open SQL is uniform for all the databases
supported by SAP.
▪ All open SQL statements are passed to the database
interface. The DB interface converts the open SQL to
native SQL and passes it on to the database. 2
3
List Of Open SQL Statments

Open SQL Description


SELECT Reads data from database
INSERT Inserts lines to database
UPDATE Changes the contents of lines in database
Inserts lines into database or changes the contents of
MODIFY existing lines
DELETE Deletes lines from database

All Open SQL statements fill the following two system fields:
 SY-SUBRC – After every Open SQL statement, the system field SY-SUBRC
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 4
the number of database lines processed.
Reading Data using Open SQL

SELECT <result> INTO <target>


FROM <source> [WHERE <condition>]

SELECT * FROM zemployee INTO gwa_employee WHERE dept_id = 2. ENDSELECT.


OR
SELECT id phone dept_id FROM zemployee INTO CORRESPONDING FIELDS OF
gwa_employee WHERE dept_id = 2.ENDSELECT.
5
Changing Values using SAP Open SQL

▪ UPDATE <database table> FROM <work area>

DATA: gwa_employee TYPE zemployee.


gwa_employee-id = 6.
gwa_employee-name = 'JOSEPH'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.
1- UPDATE zemployee FROM gwa_employee.

UPDATE <target> SET <set1> <set 2> … [WHERE <condition>].

2- UPDATE zemployee SET place = 'MUMBAI' WHERE dept_id = 2.


6
MODIFY <database table> FROM <work area>

If the database table does not already contain a line with the
same primary key as specified in the work area, a new line is
inserted. If the database table does already contain a line
with the same primary key as specified in the work area, the
existing line is overwritten.
DATA: gwa_employee TYPE zemployee.

gwa_employee-id = 6.
gwa_employee-name = 'JOSEPH'.
gwa_employee-place = 'FRANKFURT'.
gwa_employee-phone = '7897897890'.
gwa_employee-dept_id = 5.

MODIFY zemployee FROM gwa_employee.

7
Inserting Values

INSERT <database table> FROM <work area>


or
INSERT INTO <database table> VALUES <work
area>

INSERT zemployee FROM gwa_employee.

8
Deleting Entries using SAP Open SQL

DELETE <database table> FROM <work area>

DELETE zemployee FROM gwa_employee.


We can also multiple lines from the table using
the WHERE clause in the DELETE statement.

DELETE FROM <database table> WHERE <condition>


DELETE FROM zemployee WHERE dept_id = 2.

9
Select Single

Select single statement only selects the first record of any series of
records from a database table. That means this statement can read a
single record from a database .

SELECT SINGLE ebeln ebelp matnr werks lgort FROM ekpo INTO wa_ekpo
WHERE ebeln = '3000000232'.

10
Select up to

Select up to statement is used to mention the rows need to be selected


from the database table. If the database contains that number of rows then
it will display accordingly. Here we are declaring an internal table to store
the multiple row records and print the output as well.

SELECT ebeln ebelp matnr werks lgort UP TO 5 ROWS


FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = '3000000232'.

11
Select Distinct

Select distinct only selects the unique entries of the fields in the select
statement. It will not allow any duplicate entry into the internal table.

SELECT DISTINCT ebeln FROM ekpo INTO TABLE it_ekpo


WHERE ebeln IN s_ebeln.

12
Select with Appending

The output is like this.


We can directly append records into an
internal table with
select statement by using APPENDING
clause. Syntax is as follows.

SELECT db_field1, db_field2,…


FROM db_table APPENDING TABLE internal_table
WHERE db_field = condition.

13
Select with Appending CORRESPONDING
FIELDS OF TABLE

In this case the output will come with the


corresponding fields. The system will put the
respective data on the respective fields of
the output screen. But the records will come
one by one (different rows) rather the same
row.

SELECT db_field1, db_field2,…


FROM db_table
APPENDING CORRESPONDING FIELDS OF
TABLE internal_table
WHERE db_field = condition.
14
Average Sum Maximum Minimum by Select

SELECT AVG( menge )


SUM( menge )
MAX( menge )
MIN( menge )
FROM ekpo
INTO (average, sum, maximum, minimum)
WHERE ebeln = '3000000057'.

15
Client Specified Select

Client specified clause switches off the automatic client handling by open
SQL. If we select the MANDT (client) field then we have to use client
specified clause like follows:

SELECT mandt field1 field2 ... fieldn


INTO TABLE internal_table FROM database_table
CLIENT SPECIFIED "MANDT has been selected
"hence client specified is must
WHERE mandt = '800'
AND field1 IN select_option.

16
Bypassing Buffer in Select

One of an important feature of open SQL is that it fetches the data records from the buffer of
SAP system. Now fetching records directly from database may take time. Hence
performance will go down. That’s why SQL fetches data from buffer.

Now if the database table changes frequently (table like transaction table) then it will be a
problem to select updated data which will not be present in buffer. To avoid this problem
SAP system has introduced the BYPASSING BUFFER clause in the select statement after
from clause. This statement ensures that the records are updated data records fetched from
the database.
SELECT kunnr land1 name1 ort01 pstlz regio
INTO TABLE it_kna1 FROM kna1
BYPASSING BUFFER "it ensures that the system fetches
"data directly from the database
"not from the buffer
WHERE kunnr IN s_kunnr. 17
Select Dynamic Column

We can select the columns dynamically in a select statement. The syntax is like this:
SELECT (local_internal_table)
FROM database_table INTO TABLE internal_table.

Here the local internal table contains the field names dynamically. This table also has a line
type which holds the data of field names like this.
DATA: line TYPE char100,
itab TYPE TABLE OF line.

line = 'ebeln ebelp matnr werks lgort'.


APPEND line TO itab.

Now after appending the text to the itab it can be used dynamically in select statement. Here
the WHERE clause is optional. If we don’t use it then the total rows/records of the fields will
have been fetched by the system. 18
REPORT zabap_gui.
TABLES: ekpo.
* Creating a custom structure of Item Table
TYPES:BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.
* Creating a line type of predefined structure
DATA:wa_ekpo TYPE ty_ekpo,it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
* Creating a line type and internal table
* to use as dynamic columns specification
line TYPE char100,itab TYPE TABLE OF line.
line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.
SELECT (itab)FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = '3000000232'.

19
Here is the output.

20
Group By

SELECT ebeln
MAX( menge ) MIN( menge )
MAX( ktmng ) MIN( ktmng )
FROM ekpo
INTO (ebeln,
po_max, po_min,
tq_max, tq_min)
GROUP BY ebeln.

21
Using Cursor in ABAP

A cursor is a database object which is used to manipulate data in a set of


row by row. We can say that a cursor is a set of rows with a pointer and
this pointer actually points to the current row. Since cursor works row by
row, it actually kills the performance. So it is better to go with another way
with the help ABAP logic. In ABAP we use cursor with the following four
processes.

22
1- Declare the Cursor:
· The cursor is declared by the DATA statement with keyword CURSOR.

2- Open Cursor Statement:


· Open cursor opens a database cursor for a specific selection, defined after FOR.
· It links the cursor variable (cr_spfli) to the database cursor.
· If the cursor variable is already opened then it cannot be reopened.
· The statement takes the cursor position at the first row of the resulting set.
· The select statement declared after FOR doesn’t enter any record into any table or work
area.
· Select single statement cannot be used here.
· Only a limited number of database cursor can be open at the same time.
· Open cursor actually initialize the cursor at the first position of database.

23
3- Fetch Next Cursor Statement:

· It extracts the requested rows from the database.


· We can enter the fetched data into a table or work area. The append work can also be done
here.
· It changes the position of the database cursor to the next line to be extracted.
· System can fetch one or more data records by this statement.
· Sy-subrc will be zero when the system fetches data.
· When the cursor is at the last position of rows then the next cursor will cause sy-subrc = 4.
Because no line will be extracted further.

4- Close Cursor Statement:

· It closes the database cursor and initializes the cursor variable.


· We should close all the open database cursor if they are no longer required.
· Once the cursor is closed it no longer is accessed.

24
REPORT zsr_test NO STANDARD PAGE HEADING.
TABLES spfli.
DATA: wa_spfli TYPE spfli.
“ 1- Declare cursor
data: cr_spfli TYPE cursor.
PARAMETERS p_from TYPE spfli-countryfr.
“ 2- Open Cursor
OPEN CURSOR cr_spfli FOR SELECT * FROM spfli WHERE countryfr = p_from.
IF sy-subrc = 0.
WRITE: / 'Airline',
10 'Flight Number',
30 'Country From',
45 'City From',
66 'Departure airport',
ULINE.
SKIP.
ENDIF.

25
DO.
“ 3- Fetch Next Cursor
FETCH NEXT CURSOR cr_spfli
INTO wa_spfli.
IF sy-subrc = 0.
CHECK wa_spfli-countryfr = p_from.
WRITE: /3 wa_spfli-carrid,
10 wa_spfli-connid,
30 wa_spfli-countryfr,
45 wa_spfli-cityfrom,
66 wa_spfli-airpfrom,
ELSE.
EXIT.
ENDIF.
ENDDO.

“ 4- Close Cursor
CLOSE CURSOR cr_spfli.
26
THANKS!
Any questions?
You can find me at:
[email protected]

27

You might also like