Open SQL
Open SQL
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
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.
7
Inserting Values
8
Deleting Entries using SAP Open SQL
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
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.
12
Select with Appending
13
Select with Appending CORRESPONDING
FIELDS OF TABLE
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:
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.
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
22
1- Declare the Cursor:
· The cursor is declared by the DATA statement with keyword CURSOR.
23
3- Fetch Next Cursor Statement:
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