ABAP Chapter 3: Open SQL Internal Table
ABAP Chapter 3: Open SQL Internal Table
Open SQL
Internal Table
SAP System : 3 Tier Client/Server
SAP GUI SAP GUI SAP GUI Presentation
Server
SAP
Application
Server
DB Server
SAP SYSTEM (3 Tier Architecture)
SAP GUI SAP GUI
Presentation Layer
(Windows based)
SAP Instance
Application Layer
Dispatcher M
(Windows Server/UNIX)
Request SAP Buffer
Queue
(Shared Mem)
D D B V S E
G
Oracle
Database Layer
Informix
(Windows Server/UNIX)
DB2
Database Server
MS SQL Server
SAP DB/MaxDB
SAP System : Dialog Processing
SAP GUI
Report zpsm1.
Store request
to queue3 Dispatcher
Send 2 Search for SAP Buffer
List
9 free WP
Request Check Program in 7
Program
Queue Send request Program
5 Buffer Execute
to WP
4 ABAP
Table
D D D … D stateme
nt
…
8 6
SQL Load&Gen
Database Server Request Program
Dialog Work Process Architecture
Dialog Work Process Local Memory
Memory Space
TaskHandler
ABAP Processor
List buffer
DYNPRO Processor
DB Interface
Result Set Memory
Database Server
Open SQL
SELECT ...
INSERT ...
UPDATE ...
DELETE ...
DB Interface
SAP Application Server
ABAP Processor
DYNPRO
List Buffer
DB Interface
Result Set
~ 32 KB in length
Database Server
Data Data
Data Data
Data
Example Tables in DB
customers
spfli id name city
SQ 0110 20010226 75
Select Overview
Select <result> Which Columns?
From <table> Which Table?
Into <destination> Where to place?
Where <condition> Which Lines?
Select Statement
Select multiple records from database
SELECT * FROM customers.
…
ENDSELECT.
ABAP Processor
List buffer
DYNPRO Processor
DB Interface
Result Set
Database
SELECT Statement Working Steps
1. Transform open SQL to DB SQL and return result set
into result set work area
customers-name customers-city
customers-id
SELECT with WHERE Clause
Loop Processing with Restriction
Tables spfli.
Select * from spfli
where cityfrom = ‘FRANKFURT’.
write: / spfli-carrid, spfli-cityto.
endselect.
If sy-subrc <> 0.
write / ‘no data’.
endif.
Select With Range
Tables sflight.
Select * From sflight
Where price between 100 and
1000.
Write: / sflight-carrid, sflight-connid,
sflight-price.
Endselect.
SELECT … With IN List
Tables sflight.
Select * From sflight
Where price in ( 100, 1000 ).
Write: / sflight-carrid, sflight-connid,
sflight-price.
Endselect.
Select Single Record
Select Single Record
Tables spfli.
Select single * from spfli
where carrid = ‘LH’ and
connid = ‘0400’.
if sy-subrc = 0.
write: / spfli-carrid, spfli-connid,
spfli-cityfrom, spfli-cityto.
else.
write: / ‘Data not found’.
endif.
Select Column List
Select * : Example
SELECT *
Reading Selected Column
Data: id like customers-id,
name like customers-name,
city like customers-city.
Select id name city
into (id, name, city)
from customers.
write: / id, name, city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Reading Selected Column
Data: begin of wa,
id like customers-id,
name like customers-name,
city like customers-city,
end of wa.
Select id name city
into wa
from customers.
write: / wa-id, wa-name , wa-city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Select Column : Example I
Reading Selected Column
Tables customers.
Select id name city
into (customers-id, customers-name, customers-city)
from customers.
write: / customers-id, customers-name, customers-city.
endselect.
if sy-subrc <> 0.
write / ‘No Data found’.
endif.
Select Column : Example II
Corresponding Fields of...
Tables: customers.
Select id name city
into corresponding fields of customers
from customers.
Write: / customers-id, customers-name,
customers-city.
Endselect.
Select Statement : Special Topics
DB Count : SY-DBCNT
Tables customers.
Select * from customers.
write: / sy-dbcnt, customers-id, customers-name.
endselect.
if sy-subrc <> 0.
write: / ‘No Data found’.
else.
write: / sy-dbcnt, ‘Record found’.
endif.
SELECT … ORDER BY ...
Tables: spfli.
Select * from spfli
Order by cityfrom.
Write: / spfli-carrid, spfli-connid,
spfli-cityfrom.
Endselect.
SELECT … With Template
Tables customers.
Select * From customers
Where name Like ‘_r%’.
Write: / customers-id,customers-name.
Endselect.
Aggregate Functions
Data: maxdat like sflight-distance,
mindat like sflight-distance,
counter type I.
Select COUNT( * ) MIN( distance ) MAX( distance )
into (counter ,mindat, maxdat)
from spfli.
Write: / ‘Count :’ , counter,
/ ‘Min :’ , mindat,
/ ‘Max :’ , maxdat.
ห้ามใช้ SELECT *
customers-name customers-city
customers-id
Exercise II
ห้ามใช้ SELECT *
sflight
spfli
carrid connid fldate price
carrid connid cityfrom cityto distance
LH 0400 20010101 150
LH 0400 NY BK 100
LH 0400 20010110 145
LH 0402 BK NY 540
LH 0400 20010228 130
SQ 0110 SQ BK 250
SQ 0110 20010226 75
Tables Join
Question: Select carrid, connid and cityto from spfli
and fldate,price from sflight where carrid = ‘LH’
2
Select … A~a B~b B~c
inner join.. a1 b1 c1
Endselect. a2 b2 c2
Open SQL – Alias Table Name
Tables: spfli,sflight.
Select a~carrid a~connid b~fldate a~cityto b~price
into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price)
from spfli as a inner join sflight as b
on a~carrid = b~carrid and
a~connid = b~connid
where a~carrid = ‘LH’.
Write: / spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price
Endselect.
Inner Join/Outer Join Example
ZCUSTOMERS ZSALEREPS
id name city tel sale_id name
1 John New York 111111 01 Somchai
2 Peter London 222222 02 Pipop
3 David Singapore 432555
4 Micheal Bangkok 234111
ZSALES
cust_id prod_id sale_date qty sale_id
ZPRODUCTS
1 A1 20020318 10 01
p_id prod_name on_hand
1 A2 20020318 50 01
A1 Pen 100
3 X1 20020321 90 02
A2 Pencil 125
B1 Ruler 80
X1 Tape 120
Y1 CD 99
Open SQL – Inner Join
REPORT ZINNERJOIN01 .
TABLES: ZCUSTOMERS,ZSALES.
SELECT A~NAME B~PROD_ID
INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID)
FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A
ON B~CUST_ID = A~ID.
WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID.
ENDSELECT.
Open SQL – Inner Join > 2 Tables
Table : C
Tables: A,B,C. A-a B-c C-y
Select A~a B~c C~y x y
into (A-a,B-c,C-y) … ...
from A inner join B
on A~b = B~b Table : B
inner join C
b c
on C~x = B~c.
Write: / A-a,B-c,C-y.
… ...
Endselect. … ...
Table : A … …
a b
… …
Open SQL – Inner Join > 2 Tables
REPORT ZINNERJOIN02 .
TABLES: ZCUSTOMERS,ZPRODUCTS,ZSALES.
SELECT A~NAME C~PROD_NAME B~QTY
INTO (ZCUSTOMERS-NAME, ZPRODUCTS-PROD_NAME, ZSALES-QT
FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A
ON B~CUST_ID = A~ID
INNER JOIN ZPRODUCTS AS C
ON C~P_ID = B~PROD_ID.
WRITE: / ZCUSTOMERS-NAME,ZPRODUCTS-PROD_NAME,ZSALES-
QTY.
ENDSELECT.
Exercise
List customers who buy product from
company as following fields:
zcustomers-id
zcustomers-name
zsales-sale_date
zproducts-prod_name
zsales-qty
zsalereps-name
Exercise : User Master
USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER Tables Relationship
USR02
BNAME
USR21
PERSNUMBER
ADDRNUMBER
ADCP
ABAP : Outer Join
Open SQL – Outer Join
REPORT ZOUTERJOIN .
TABLES: ZCUSTOMERS,ZSALES.
SELECT A~NAME B~PROD_ID
INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID)
FROM ZCUSTOMERS AS A LEFT OUTER JOIN ZSALES AS B
ON A~ID = B~CUST_ID.
WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID.
ENDSELECT.
Single Result Table
A~NAME B~PROD_ID
John A1
John A2
Peter
David X1
Micheal
Exercise
List customers name who do not buy any
product from company
Sub Query
REPORT ZSUBQUERY .
tables: zcustomers. ลูกค้าชื่ออะไรที่ไม่ได้ซื้อ
สินค้าจากเรา มีใครบ้าง
select * from zcustomers as a
where not exists
( select *
from zsales as b
where b~cust_id = a~id ).
write: / zcustomers-name.
endselect.
Internal Table
Data Objects in ABAP
Memory Space
Variable Structure
Constants <Field-symbols>
INTERNAL TABLE
Flight (Structure)
Carrid Connid Date Price
Internal Table
Flight (Internal Table) Header Line
Carrid Connid Date Price
Structure
Data: Begin of flight,
carrid like sflight-carrid,
connid like sflight-connid,
date like sflight-fldate,
price like sflight-price.
Data: End of flight.
flight-carrid = ‘LH’.
Write: / flight-carrid.
INTERNAL TABLE
Data: begin of tab occurs 10,
carrid like sflight-carrid,
connid like sflight-connid,
fldate like sflight-fldate,
price like sflight-price.
Data end of tab.
USING ABAP DICTIONARY STRUCTURE
Tables sflight.
Data flight like sflight occurs 0 with header line.
Select * from sflight.
Move sflight to flight.
Append flight.
Endselect.
Standard Key of Internal Table
tab
Sort flight.
Sort flight by price fldate.
Sort flight by price ascending
fldate descending.
SORTING INTERNAL TABLE
Data tab like spfli occurs 0 with header line.
Select * from spfli into table tab.
Sort tab by cityfrom.
…
Loop at tab.
write: / tab-carrid, tab-connid,tab-cityfrom.
Endloop.
PROCESSING INTERNAL TABLE
...
Loop at flight.
Write: / flight-carrid, flight-connid.
Endloop.
...
Delete flight index 5.
Delete flight where carrid = ‘LH’.
flight-carrid = ‘XX’.
flight-price = 100.
…
Insert flight index 1.
DELETING INTERNAL TABLE
DATA flight LIKE sflight occurs 0 with header line.
Clear flight.
Refresh flight.
Free flight.
Total Record of Internal Table
INSERT
UPDATE
Database
MODIFY
DELETE
Insert (Table)
Tables customers.
customers-id = ‘999’.
customers-name = ‘Test’.
Insert customers.
if sy-subrc <> 0.
write: / ‘Data Already Exists’.
endif.
Update Statement
Tables customers.
Select single * from customers where id
= 1.
If sy-subrc = 0.
customers-name = ‘John’.
update customers.
Endif. Update customers
set name = ‘John’
where id = 1.
Update Statement
Data wa like customers.
wa-id = ‘1’.
wa-name = ‘Test No 1’.
wa-city = ‘Bangkok’.
update customers from wa.
If sy-subrc <> 0.
write: / ‘Data not found’.
Endif.
Modify Statement
Tables customers.
customers-id = ‘1’.
customers-name = ‘Test No 1’.
Modify customers.
Deleting Database Table Entries
Tables customers.
customers-id = ‘1’.
Delete customers.
USR02
BNAME
USR21
PERSNUMBER
ADDRNUMBER
ADCP
Exercise III : User Master
usr02-trdat
usr02-bname
adcp-tel_number