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

Application Server Store request to queue 3 Send Request 1 Generate Screen(List) 10 List Select single * from customers where id = 1. Write: / customers-name. Dispatcher Send List 2 9 Search for free WP SAP Buffer Program Table … Request Queue Send request to WP 4 Check Program in Program Buffer 5 7 Execute ABAP statement D 8 D SQL Request D … D 6 Load&Gen Program Database Server .SAP System : Dialog Processing SAP GUI Report zpsm1. Request Tables customers.

Dialog Work Process Architecture Dialog Work Process TaskHandler Local Memory Memory Space ABAP Processor List buffer DYNPRO Processor DB Interface Result Set Memory Database Server .

.. .... INSERT ...Open SQL     SELECT .. DELETE . UPDATE .

DB Interface SAP Application Server Dialog WP TaskHandler ABAP Processor DYNPRO List Buffer Local Memory Memory Space DB Interface Result Set ~ 32 KB in length Database Server Data Data Data Data Data .

Example Tables in DB spfli carrid connid cityfrom cityto distance customers id name city 1 John New York LH LH SQ 0400 0402 0110 LA BK SQ NY NY BK 100 540 250 2 3 Peter David Singapore London .

Example Tables in DB sflight carrid connid fldate price LH 0400 20010101 150 LH LH SQ 0400 0400 0110 20010110 20010228 20010226 145 130 75 .

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

Select Statement  Select multiple records from database SELECT * FROM customers. … ENDSELECT. … .  Select single record from database SELECT SINGLE * FROM customers WHERE id = 1.

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

Dialog WP Dialog WP TaskHandler ABAP Processor List buffer Local Memory Memory Space DYNPRO Processor DB Interface Result Set Database .

Transform open SQL to DB SQL and return result set into result set work area SELECT * FROM spfli. Table Structure in Memory Space . Loop with data in result set and transfer each record to work area in memory space SELECT * FROM spfli. SELECT * FROM spfli. … ENDSELECT.SELECT Statement Working Steps 1. 2. … ENDSELECT.

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

customers-name

customers-city

SELECT with WHERE Clause .

write: / spfli-carrid. write / ‘no data’. endif.Loop Processing with Restriction Tables spfli. endselect. . If sy-subrc <> 0. Select * from spfli where cityfrom = ‘FRANKFURT’. spfli-cityto.

Endselect.Select With Range Tables sflight. . Write: / sflight-carrid. Select * From sflight Where price between 100 and 1000. sflight-price. sflight-connid.

Endselect. sflight-price.SELECT … With IN List Tables sflight. . Select * From sflight Where price in ( 100. sflight-connid. Write: / sflight-carrid. 1000 ).

Select Single Record .

else. write: / spfli-carrid. if sy-subrc = 0.Select Single Record Tables spfli. Select single * from spfli where carrid = ‘LH’ and connid = ‘0400’. spfli-connid. . endif. write: / ‘Data not found’. spfli-cityto. spfli-cityfrom.

Select Column List .

Select * : Example SELECT * .

write: / id. write / ‘No Data found’. if sy-subrc <> 0. city like customers-city. name like customers-name. . name. Select id name city into (id. endif. city. endselect. city) from customers.Reading Selected Column Data: id like customers-id. name.

write: / wa-id. wa-city. city like customers-city. name like customers-name. endif. Select id name city into wa from customers. . if sy-subrc <> 0. wa-name .Reading Selected Column Data: begin of wa. write / ‘No Data found’. id like customers-id. endselect. end of wa.

Select Column : Example I .

write: / customers-id. customers-city) from customers.Reading Selected Column Tables customers. endselect. customers-name. if sy-subrc <> 0. . write / ‘No Data found’. customers-city. Select id name city into (customers-id. customers-name. endif.

Select Column : Example II .

customers-city. Endselect. . Tables: customers.. Write: / customers-id.Corresponding Fields of. Select id name city into corresponding fields of customers from customers. customers-name..

Select Statement : Special Topics .

write: / sy-dbcnt. else. write: / sy-dbcnt. write: / ‘No Data found’. customers-id. customers-name. endif. Select * from customers. endselect. if sy-subrc <> 0.DB Count : SY-DBCNT Tables customers. ‘Record found’. .

Write: / spfli-carrid.. Endselect. . Tables: spfli. spfli-connid..SELECT … ORDER BY . spfli-cityfrom. Select * from spfli Order by cityfrom.

Select * From customers Where name Like ‘_r%’. Write: / customers-id. . Endselect.SELECT … With Template Tables customers.customers-name.

Write: / ‘Count :’ .MIN. counter type I. / ‘Max :’ . Select COUNT( * ) MIN( distance ) MAX( distance ) into (counter .AVG and SUM .mindat.Aggregate Functions Data: maxdat like sflight-distance. maxdat) from spfli.MAX. counter. mindat like sflight-distance. Aggregate Functions : COUNT. / ‘Min :’ . maxdat. mindat.

sflight mindat Type P Decimals 2. Select carrid Min( price ) Max( price ) LH 0400 Into (carrid. SQ 0110 Endselect..SELECT … GROUP BY . mindat.. maxdat) LH 0400 From sflight Group by carrid. LH 0400 Write: / carrid. maxdat. Data: carrid like sflight-carrid. fldate Price 20010101 20010110 20010228 20010226 150 145 130 75 อยากทราบว่ า ในแต่ ละสายการบิน มีราคาตั๋วต่าสุดและสูงสุดเท่ าไร . mindat. carrid connid maxdat Type P Decimals 2.

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

Exercise I ห้ ามใช้ SELECT * customers-id customers-name customers-city .

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 SQ 0400 0402 0110 NY BK SQ BK NY BK 100 540 250 LH LH LH SQ 0400 0400 0400 0110 20010101 20010110 20010228 20010226 150 145 130 75 .

price from sflight where carrid = ‘LH’ spfli-carrid spfli-connid sflight-fldate spfli-cityto sflight-price เงื่อนไข : ให้ แสดงข้ อมูลเฉพาะสายการบิน ‘LH’ เท่ านัน ้ .Tables Join Question: Select carrid. connid and cityto from spfli and fldate.

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

Tables Join Methods     Nested select statement Internal table View Inner join of Select statement .

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

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

B.B-c.B-c) from A inner join B on A~b = B~b. Write: / A-a. Select A~a B~b B~c into (A-a.B-b. Endselect. Table : A A-a B-b B-c Table : B b b b1 b2 b1 b2 b3 c c1 c2 c3 a a1 a2 .B-b.Open SQL – Inner Join Tables: A.

Endselect. A~a a1 a2 B~b b1 b2 B~c c1 c2 ..Open SQL – Inner Join Table : A Database Server Table : B a a1 a2 b b1 b2 b b1 b2 b3 1 c c1 c2 c3 Application Server 2 Single Result Table(Result set) Select … inner join.

spfli-connid. spfli-connid. spfli-cityto. . sflight-fldate. 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’. sflight-price.Open SQL – Alias Table Name Tables: spfli. sflight-fldate. Write: / spfli-carrid. Endselect. Select a~carrid a~connid b~fldate a~cityto b~price into (spfli-carrid.sflight. spfli-cityto.

Inner Join/Outer Join Example ZCUSTOMERS id 1 2 ZSALEREPS city tel 111111 222222 name John Peter sale_id 01 02 name Somchai Pipop New York London 3 4 David Micheal Singapore Bangkok 432555 234111 ZSALES cust_id prod_id A1 A2 X1 sale_date 20020318 20020318 20020321 qty 10 50 90 sale_id 01 01 02 1 1 3 ZPRODUCTS p_id A1 A2 prod_name Pen Pencil on_hand 100 125 B1 X1 Y1 Ruler Tape CD 80 120 99 .

ZSALES. WRITE: / ZCUSTOMERS-NAME. . ENDSELECT. 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. TABLES: ZCUSTOMERS.Open SQL – Inner Join REPORT ZINNERJOIN01 .ZSALES-PROD_ID.

… a … b … .B-c. Write: / A-a.B. Select A~a B~c C~y into (A-a... .C... b … … … c ..Open SQL – Inner Join > 2 Tables A-a B-c C-y Tables: A.C-y. Table : A Table : C x … Table : B y .B-c. Endselect.C-y) from A inner join B on A~b = B~b inner join C on C~x = B~c..

SELECT A~NAME C~PROD_NAME B~QTY INTO (ZCUSTOMERS-NAME. ZPRODUCTS-PROD_NAME.Open SQL – Inner Join > 2 Tables REPORT ZINNERJOIN02 . . ENDSELECT.ZPRODUCTS-PROD_NAME.ZSALES.ZPRODUCTS. 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. TABLES: ZCUSTOMERS.ZSALES-QTY.

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 .

ZSALES-PROD_ID. Single Result Table A~NAME John John Peter David Micheal X1 B~PROD_ID A1 A2 . WRITE: / ZCUSTOMERS-NAME.ZSALES-PROD_ID) FROM ZCUSTOMERS AS A LEFT OUTER JOIN ZSALES AS B ON A~ID = B~CUST_ID.ZSALES. ENDSELECT.Open SQL – Outer Join REPORT ZOUTERJOIN . SELECT A~NAME B~PROD_ID INTO (ZCUSTOMERS-NAME. TABLES: ZCUSTOMERS.

Exercise  List customers name who do not buy any product from company .

ิ ค ้าจากเรา มีใครบ ้าง สน select * from zcustomers as a where not exists ( select * from zsales as b where b~cust_id = a~id ).Sub Query REPORT ZSUBQUERY . . write: / zcustomers-name. endselect. ื่ อะไรทีไ ื้ ลูกค ้าชอ ่ ม่ได ้ซอ tables: zcustomers.

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 .

. flight-carrid = ‘LH’. like sflight-connid. like sflight-price. / flight-carrid.Structure Data: Begin of carrid connid date price flight. like sflight-fldate. like sflight-carrid. Data: Write: End of flight.

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

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

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

Endselect. Append flight. . Select * from sflight. Data flight like sflight occurs 0 with header line.FILLING INTERNAL TABLE (APPEND) Tables sflight. Move sflight to flight.

f1 type C.Standard Key of Internal Table tab Data: begin of tab occurs 0. f2 type I. end of tab. f4 type P. f1 f2 f3 f4 . f3 type N.

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

Access Database Without Internal Table .

Access Database Using Internal Table .

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

Exercise I : Change Using Internal Table .

flight by flight by price fldate. . price ascending fldate descending.SORTING INTERNAL TABLE (SORT) Sort Sort Sort flight.

write: / tab-carrid.tab-cityfrom. . … Loop at tab. Endloop. Select * from spfli into table tab. Sort tab by cityfrom. tab-connid.SORTING INTERNAL TABLE Data tab like spfli occurs 0 with header line.

Endloop. Endloop. Endloop. Loop at flight where carrid = ‘LH’. flight-connid. .. Write: / sy-tabix . Write: / flight-carrid. Write: / flight-carrid. Loop at flight.flight-carrid. flight-connid. flight-connid. Loop at flight from 1 to 10..PROCESSING INTERNAL TABLE .

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

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

. flight-connid. If sy-subrc = 0. . write: / flight-carrid. Read table flight index 3..Reading Single Record using Index . Endif.

Delete Delete flight index 5. … Insert flight index 1.. flight where carrid = ‘LH’.CHANGING INTERNAL TABLE . . flight-carrid = ‘XX’.. flight-price = 100.

DELETING INTERNAL TABLE DATA flight LIKE sflight occurs 0 with header line. Free flight. Refresh flight. . Clear flight.

Write: / line_count. Data tab like sflight occurs 0 with header line. .Total Record of Internal Table Data: line_count type i. Select * from sflight into table tab. Describe table tab lines 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. … LOOP AT tab INTO wa. DATA wa LIKE customers. WRITE: / wa-id. ENDLOOP. wa-name. .

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

customers-name = ‘Test’. . if sy-subrc <> 0. customers-id = ‘999’. write: / ‘Data Already Exists’. endif. Insert customers.Insert (Table) Tables customers.

Update Statement Tables customers. update customers. Update customers set name = ‘John’ where id = 1. Endif. . customers-name = ‘John’. Select single * from customers where id = 1. If sy-subrc = 0.

Update Statement Data wa like customers. Endif. . update customers from wa. wa-id = ‘1’. write: / ‘Data not found’. wa-name = ‘Test No 1’. wa-city = ‘Bangkok’. If sy-subrc <> 0.

Modify Statement Tables customers. customers-id = ‘1’. Modify customers. . customers-name = ‘Test No 1’.

customers-id = ‘1’.Deleting Database Table Entries Tables customers. Delete customers. Delete From customers Where city = ‘Bangkok’. . Delete customers From Table delcustomers.

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