ABAP Chapter 3

 

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
(Windows Server/UNIX)
Request Queue

Dispatcher

M
SAP Buffer (Shared Mem)

D

D

B

V

S

E G

Database Layer
(Windows Server/UNIX)

Oracle Informix DB2 MS SQL Server SAP DB/MaxDB

Database Server

SAP System : Dialog Processing
SAP GUI
Report zpsm1.

Request

Application Server
Store request to queue 3

Generate 1 10 Screen(List) Send Request

List

Tables customers. Select single * from customers where id = 1. Write: / customersname.

Send List
9

Dispatcher
2

Request Queue Send request to WP4

Search for free WP Check Program in Program Buffer 5

SAP Buffer
Program Table

7

D

D

D …

D
6

Execute ABAP statement

8

Database Server

SQL Request

Load&Gen Program

Dialog Work Process Architecture
Dialog Work Process TaskHandler ABAP Processor
List buffer

Local Memory
Memory Space

DYNPRO Processor DB Interface

Result Set Memory

Database Server

Open SQL
   

SELECT ... INSERT ... UPDATE ... DELETE ...

DB Interface
SAP Application Server Dialog WP
TaskHandler ABAP Processor DYNPRO

Local Memory
Memory Space

DB Interface Result Set
~ 32 KB in length

List Buffer

Database Server

D a ta

D a ta

D a ta

D a ta

D a ta

Example Tables in DB
spfli
carrid connid cityfrom cityto distance

customers
id name city

1 2 3

John New York Peter Singapore David London

LH LH SQ

0400 0402 0110

LA BK SQ

NY NY BK

100 540 250

Example Tables in DB
sflight
carrid connid fldate price

LH LH LH SQ

0400 20010101 0400 20010110 0400 20010228 0110 20010226

150 145 130 75

Select Overview
Select <result> Columns? From <table> Into <destination> place? Where <condition> Which Which Table? Where to Which Lines?

Select Statement

Select multiple records from database * FROM customers. SELECT
… ENDSELECT.

SelectSELECT SINGLErecord from database single * FROM customers WHERE id = 1.

Select Multiple Records
Tables spfli. Seclect * from spfli. write: / spfli-carrid, spfli-connid, spfli-cityto. endselect. if sy-subrc <> 0. write: / ‘No Data’. endif.

Dialog WP
Dialog WP TaskHandler
ABAP Processor
DYNPRO Processor
List buffer

Local Memory
Memory Space

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
SELECT * FROM spfli. … ENDSELECT.

SELECT * FROM spfli;

2. Loop with data in result set and transfer each record to work area in memory space
SELECT * FROM spfli. … ENDSELECT. Table Structure in Memory Space

Select … Into Table Structure
Tables spfli. Seclect * from spfli into spfli. write: / spfli-carrid, spfli-connid, spfli-cityfrom, spfli-cityto. endselect. if sy-subrc <> 0. write: / ‘No Data’. endif.

Select … Into Work Area
Data wa like spfli. Seclect * from spfli into wa. write: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto. endselect. if sy-subrc <> 0. write: / ‘No Data’. endif.

Exercise I

customers-id

customersname

customerscity

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, customerscity. 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-nam 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.

Aggregate Functions : COUNT,MIN,MAX,AVG and SUM

SELECT … GROUP BY ...
Data: carrid like sflight-carrid, mindat Type P sflight Decimals 2, maxdat Type P carrid connid 2.fldate Price Decimals LH 0400 20010101 150 Select carrid Min( price ) Max( price ) Into (carrid, mindat, maxdat) 0400 20010110 145 LH From sflight Group by carrid. LH 0400 20010228 130 Write: / carrid, mindat, maxdat. Endselect. SQ 0110 20010226 75

กทราบว่า ในแต่ละสายการบิน มีราคาตัวต่่าสุดและสูงสุดเท่าไร ๋

Sub Query
ลูกค้าคนใดที่อยููเมือง tables customers. เดียวกับลูกค้ารหัส ID select * 1 from customers where id <> 1 and city = ( select city from customers where id = 1 ). write: / customers-id, customers-name. endselect.

Exercise I
ห้ามใช้ SELECT *

customers-id

customersname

customerscity

Exercise II
ห้ามใช้ SELECT *

usr02-bname

usr02-trdat

usr02-ltime

ABAP : Inner Join

Tables Join
spfli
carrid connid cityfrom cityto distance

sflight
carrid connid fldate price

LH LH LH SQ

0400 20010101 0400 20010110 0400 20010228 0110 20010226

150 145 130 75

LH LH SQ

0400 0402 0110

NY BK SQ

BK NY BK

100 540 250

Tables Join
Question: Select carrid, connid and cityto from spfli and fldate,price from sflight where carrid = ‘LH’ spfli-carrid spfli-connid sflight-fldate spfli-cityto sflight-price

เงือนไข : ให้แสดงข้อมูลเฉพาะสายการบิน ‘LH’ เท่า ่

Standard SQL
Select spfli.carrid, spfli.connid, sflight.fldate, sflight.price From spfli, sflight Where spfli.carrid = sflight.carrid and spfli.connid = sflight.connid and spfli.carrid = ‘LH’;

Tables Join Methods
   

Nested select statement Internal table View Inner join of Select statement

Nested Select Statement
Tables: spfli,sflight. Select * from spfli where carrid = ‘LH’. Select * from sflight where carrid = spfli-carrid and connid = spfli-connid. Write: / spfli-carrid, spfli-connid, sflight-fldate, sflight-price. Endselect. Endselect.

Open SQL – Inner Join
Tables: spfli,sflight. Select spfli~carrid spfli~connid sflight~fldate spfli~cityto sflight~price into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflightprice) from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid where spfli~carrid = ‘LH’. Write: / spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price. Endselect.

Open SQL – Inner Join
Tables: A,B. A-a B-b B-c Select A~a B~b B~c into (A-a,B-b,B-c) from A inner join B Table : B on A~b = B~b. Write: / A-a,B-b,B-c. b Endselect. Table : A b1

c
c1 c2 c3

a
a1 a2

b
b1 b2

b2 b3

Open SQL – Inner Join
Table : A
Databas e Server

Table : B

a
a1 a2

b
b1 b2

b

c
c1 c2 c3
1

b1 b2 b3

Application Server

Single Result Table(Result set)

Select …

2

A~a
a1 a2

B~b
b1 b2

B~c
c1 c2

inner join.. Endselect.

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, sflightprice) 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.

ZCUSTOMERS
id
1 2 3 4

Inner Join/Outer Join Example ZSALEREPS
city tel
111111 222222

name
John Peter David Micheal

sale_id
01 02

name
Somchai Pipop

New York London

Singapore 432555

ZSALES ZPRODUCTS
p_id
A1 A2 B1 X1 Y1 Bangkok 234111cust_id 1 1 3 prod_id A1 A2 X1 sale_date qty 20020318 10 20020318 50 20020321 90 sale_id 01 01 02 prod_name Pen Pencil Ruler Tape CD on_hand 100 125 80 120 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.

A-a B-c C-y Tables: A,B,C. Select A~a B~c C~y into (A-a,B-c,C-y) from A inner join B on A~b = B~b inner join C on C~x = B~c. Write: / A-a,B-c,C-y. Endselect. Table : A

Open SQL – Inner Join > 2 TablesTable : C
x

Table : B

y
...

b
… … …

c
... ... …

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-QTY 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,ZSALESQTY. 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. Single Result Table ENDSELECT.
A~NAME John John Peter David Micheal X1 B~PROD_ID A1 A2

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

Table Structure

Internal Table

Constants

<Field-symbols>

INTERNAL TABLE
Flight (Structure)
Carrid Connid Date Price

Internal Table
Flight (Internal Table)
Carrid Connid Date

Header Line
Price

Structure
Data: Begin of carrid connid date price flight, like sflight-carrid, like sflight-connid, like sflight-fldate, like sflight-price.

Data: Write:

End of flight. / flight-carrid.

flight-carrid = ‘LH’.

INTERNAL TABLE
Data: begin of tab occurs 10, carrid like sflight-carrid, connid like sflight-connid, fldate like sflight-fldate, price like sflight-price. end of tab.

Data

USING ABAP DICTIONARY STRUCTURE Data: Data begin of tab occurs 0. Include structure sflight. end of tab.

INTERNAL TABLE USING LIKE
Data tab LIKE sflight OCCURS 0 WITH HEADER LINE.

FILLING INTERNAL TABLE (APPEND)

Tables sflight. Data flight like sflight occurs 0 with header lin Select * from sflight. Move sflight to flight. Append flight. Endselect.

Standard Key of Internal Table
tab

Data: begin of tab occurs 0, f1 f1 type C, f2 type I, f3 type N, f4 type P, end of tab.

f2

f3

f4

Reading Data From Internal Table
Data tab like sflight occurs 0 with header line. Select * from sflight into table tab. If sy-subrc = 0. Loop at tab. Write: / tab-carrid, tab-price. Endloop. Else. Write: / ‘No Data’. Endif.

Access Database Without Internal Table

Access Database Using Internal Table

Reading Data From Internal Table
Data: begin of tab occurs 0, id like customers-id, name like customers-name, end of tab. Select id name from customers into table tab. If sy-subrc = 0. Loop at tab. Write: / tab-id, tab-name. Endloop. else. Write: / ‘No Data’. Endif.

Exercise I : Change
Using Internal Table

SORTING INTERNAL TABLE (SORT)
Sort Sort Sort flight. flight by price fldate. 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. Loop at flight where carrid = ‘LH’. Write: / flight-carrid, flight-connid. Endloop. Loop at flight from 1 to 10. Write: / sy-tabix ,flight-carrid, flight-connid. Endloop.

Internal Table Template Condition
... loop at tab where name cp ‘+r*’. ...

Reading Single Record
... Sort flight by carrid connid fldate. Read table flight with key carrid = ‘LH’ connid = ‘0400’ fldate = ‘19990201’ Binary Search. if sy-subrc = 0. write : / flight-carrid,flight-connid, flight-fldate, flight-price. endif.

... Read table flight index 3. If sy-subrc = 0. write: / flight-carrid, flight-connid. Endif.

Reading Single Record using Index

CHANGING INTERNAL TABLE
...
Delete Delete flight index 5. 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
Data: line_count type i. Data tab like sflight occurs 0 with header line. Select * from sflight into table tab. Describe table tab lines line_count. Write: / line_count.

Exercise I

Internal Table Processing
Data tab like spfli occurs 0 with Header line.


Select * from spfli appending table tab where carrid = ‘LH’.

SELECT … 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.

Inner Join into Internal Table
REPORT ZJOIN01 . DATA: begin of tab occurs 0, name like zcustomers-name, prod_id like zsales-prod_id, end of tab. SELECT A~NAME B~PROD_ID INTO TABLE tab FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A ON B~CUST_ID = A~ID. … LOOP AT tab. WRITE: / TAB-NAME,TAB-PROD_ID. ENDLOOP.

Internal Table Without Header Line
DATA tab LIKE customers OCCURS 0. DATA wa LIKE customers. … LOOP AT tab INTO wa. WRITE: / wa-id, wa-name. ENDLOOP.

Internal Table Declaration
DATA tab TYPE TABLE OF customers. DATA wa LIKE LINE OF customers. …

ABAP Practice

Database Table Processing
    INSERT UPDATE MODIFY DELETE

Database

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. Update customers Endif.
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. Delete customers delcustomers. Delete From From Table Where city =

customers

Exercise II

Exercise II
1. ห้ามใช้ SELECT * 2. ใช้ Internal Table

usr02-bname

usr02-trdat

usr02-ltime

Exercise III

Tables Relationship for User Master
USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER

Tables Relationship

USR02

BNAME USR21
PERSNUMBER ADDRNUMBER

ADCP

Exercise III : User Master
usr02-trdat usr02-bname adcp-tel_number

ใช้ Internal Table

Sign up to vote on this title
UsefulNot useful