Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
ABAP Chapter 3 <ul><li>Open SQL </li></ul><ul><li>Internal Table </li></ul>
SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI SAP GUI
SAP SYSTEM (3 Tier Architecture) Presentation Layer (Windows based) Application Layer (Windows Server/UNIX) Database Serve...
SAP System : Dialog Processing Database Server Application Server Dispatcher Request Queue D D D D … SAP Buffer Program Ta...
Dialog Work Process Architecture TaskHandler DYNPRO Processor ABAP Processor Local Memory Memory Space DB Interface List b...
Open SQL <ul><li>SELECT ... </li></ul><ul><li>INSERT ... </li></ul><ul><li>UPDATE ... </li></ul><ul><li>DELETE ... </li></ul>
DB Interface SAP Application Server Local Memory Dialog WP TaskHandler DB Interface Result Set Database Server ~ 32 KB in ...
Example Tables in DB <ul><li>spfli </li></ul>customers BK NY NY cityto 250 SQ 0110 SQ 540  BK 0402 LH 100 LA 0400 LH dista...
Example Tables in DB sflight 75 20010226 0110 SQ 130 20010228 0400 LH 145 20010110 0400 LH 150 20010101 0400 LH price flda...
Select Overview <ul><li>Select < result >  Which Columns? </li></ul><ul><li>From < table >  Which Table? </li></ul><ul><li...
Select Statement <ul><li>Select multiple records from database </li></ul><ul><li>Select single record from database </li><...
Select Multiple Records <ul><li>Tables  spfli. </li></ul><ul><li>Seclect  *  from spfli. </li></ul><ul><li>write: / spfli-...
Dialog WP <ul><ul><ul><ul><li>Dialog WP </li></ul></ul></ul></ul>TaskHandler DYNPRO Processor ABAP Processor Database Loca...
SELECT Statement Working Steps 1. Transform open SQL to DB SQL and return result set  into result set work area  SELECT * ...
Select … Into Table Structure <ul><li>Tables  spfli. </li></ul><ul><li>Seclect  *  from spfli  into spfli . </li></ul><ul>...
Select … Into Work Area <ul><li>Data wa like  spfli. </li></ul><ul><li>Seclect  *  from spfli  into wa . </li></ul><ul><li...
Exercise I  customers-id customers-name customers-city
SELECT with WHERE Clause
Loop Processing with Restriction <ul><li>Tables spfli. </li></ul><ul><li>Select  * from spfli </li></ul><ul><li>where city...
Select With Range <ul><li>Tables sflight. </li></ul><ul><li>Select  * From sflight </li></ul><ul><li>Where price between 1...
SELECT … With  IN  List <ul><li>Tables sflight. </li></ul><ul><li>Select  *  From sflight  </li></ul><ul><li>Where  price ...
Select Single Record
Select Single Record <ul><li>Table s  spfli. </li></ul><ul><li>Select single * from spfli </li></ul><ul><li>where carrid  ...
Select Column List
Select * : Example  SELECT *
Reading Selected Column <ul><li>Data:  id  like  customers - id, </li></ul><ul><li>name  like  customers-name, </li></ul><...
Reading Selected Column <ul><li>Data:  begin of wa, </li></ul><ul><li>id   like  customers - id, </li></ul><ul><li>name  l...
Select Column : Example I
Reading Selected Column <ul><li>Tables customers. </li></ul><ul><li>Select  id name city </li></ul><ul><li>into  (customer...
Select Column : Example II
Corresponding Fields of... <ul><li>Tables:  customers. </li></ul><ul><li>Select  id name city </li></ul><ul><li>into corre...
Select Statement : Special Topics
DB Count : SY-DBCNT <ul><li>Tables customers. </li></ul><ul><li>Select  *  from  customers. </li></ul><ul><li>write: /  sy...
SELECT  … ORDER BY ... <ul><li>Tables: spfli. </li></ul><ul><li>Select  * from spfli </li></ul><ul><li>Order by  cityfrom ...
SELECT … With  Template <ul><li>Tables customers. </li></ul><ul><li>Select  *  From customers  </li></ul><ul><li>Where  na...
Aggregate Functions <ul><li>Data: maxdat   like sflight-distance, </li></ul><ul><li>mindat   like sflight-distance, </li><...
SELECT  … GROUP BY ... <ul><li>Data:  carrid like sflight-carrid, </li></ul><ul><li>mindat  Type P  Decimals 2, </li></ul>...
Sub Query <ul><li>tables customers. </li></ul><ul><li>select * </li></ul><ul><li>from customers </li></ul><ul><li>where id...
Exercise I customers-id customers-name customers-city ห้ามใช้  SELECT *
Exercise II usr02-ltime usr02-trdat usr02-bname ห้ามใช้  SELECT *
ABAP : Inner Join
Tables Join <ul><li>spfli </li></ul>sflight BK NY BK cityto 250 SQ 0110 SQ 540  BK 0402 LH 100 NY 0400 LH distance cityfro...
Tables Join <ul><li>Question:  Select carrid, connid and cityto from spfli  </li></ul><ul><li>and fldate,price from sfligh...
Standard SQL <ul><li>Select spfli.carrid, spfli.connid, sflight.fldate, </li></ul><ul><li>sflight.price </li></ul><ul><li>...
Tables Join Methods <ul><li>Nested select statement </li></ul><ul><li>Internal table </li></ul><ul><li>View </li></ul><ul>...
Nested Select Statement <ul><li>Tables:  spfli,sflight. </li></ul><ul><li>Select  *  from spfli where carrid = ‘LH’. </li>...
Open SQL – Inner Join <ul><li>Tables:  spfli,sflight. </li></ul><ul><li>Select  spfli~carrid  spfli~connid  sflight~fldate...
Open SQL – Inner Join <ul><li>Tables:  A,B. </li></ul><ul><li>Select  A~a B~b B~c </li></ul><ul><li>into (A-a,B-b,B-c) </l...
Open SQL – Inner Join Table : A Table : B Single Result Table(Result set) Select … inner join.. Endselect. Database Server...
Open SQL – Alias Table Name <ul><li>Tables:  spfli,sflight. </li></ul><ul><li>Select  a~carrid  a~connid  b~fldate  a~city...
Inner Join/Outer Join Example ZPRODUCTS  ZSALES ZSALEREPS  ZCUSTOMERS 432555 Singapore David 3 234111 Bangkok Micheal 4 22...
Open SQL – Inner Join <ul><li>REPORT ZINNERJOIN01 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT ...
Open SQL – Inner Join > 2 Tables <ul><li>Tables:  A,B,C. </li></ul><ul><li>Select  A~a B~c C~y </li></ul><ul><li>into (A-a...
Open SQL – Inner Join > 2 Tables  <ul><li>REPORT ZINNERJOIN02 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZPRODUCTS,ZSALES. </l...
Exercise <ul><li>List customers who buy product from company as following fields: </li></ul><ul><li>zcustomers-id  </li></...
Exercise : User Master USR02-BNAME  USR02-TRDAT  ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER Tables Relat...
ABAP : Outer Join
Open SQL – Outer Join <ul><li>REPORT ZOUTERJOIN . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT  A...
Exercise <ul><li>List customers name who do not buy any product from company  </li></ul>
Sub Query <ul><li>REPORT ZSUBQUERY   . </li></ul><ul><li>tables: zcustomers. </li></ul><ul><li>select * from zcustomers as...
Internal Table
Data Objects in ABAP <ul><li>Memory Space </li></ul>Structure Table Structure Internal Table Variable Constants <Field-sym...
INTERNAL TABLE <ul><li>Flight (Structure) </li></ul>Carrid Connid Date Price Internal Table Flight (Internal Table) Carrid...
Structure <ul><li>Data: Begin of flight, </li></ul><ul><li>carrid like sflight-carrid, </li></ul><ul><li>connid like sflig...
INTERNAL TABLE <ul><li>Data: begin of  tab  occurs 10, </li></ul><ul><li>carrid like  sflight-carrid, </li></ul><ul><li>co...
USING ABAP DICTIONARY STRUCTURE <ul><li>Data: begin of tab  occurs 0 . </li></ul><ul><li>Include structure sflight. </li><...
INTERNAL TABLE USING LIKE <ul><li>Data tab LIKE sflight  OCCURS 0 WITH HEADER LINE . </li></ul>
FILLING INTERNAL TABLE (APPEND) <ul><li>Tables sflight. </li></ul><ul><li>Data flight like sflight occurs 0 with header li...
Standard Key of Internal Table Data: begin of tab occurs 0, f1 type  C , f2 type I, f3 type  N , f4 type P, end of tab. ta...
Reading Data From Internal Table <ul><li>Data  tab like sflight occurs 0 with header line. </li></ul><ul><li>Select  *   f...
Access Database Without Internal Table
Access Database Using Internal Table
Reading Data From Internal Table <ul><li>Data : begin of  tab occurs 0 , </li></ul><ul><li>id like customers-id, </li></ul...
Exercise I : Change Using Internal Table
SORTING INTERNAL TABLE (SORT) <ul><li>Sort flight. </li></ul><ul><li>Sort flight by price fldate. </li></ul><ul><li>Sort f...
<ul><li>Data tab like spfli occurs 0 with header line. </li></ul><ul><li>Select * from spfli into table tab. </li></ul><ul...
PROCESSING INTERNAL TABLE <ul><ul><li>... </li></ul></ul><ul><li>Loop at flight. </li></ul><ul><li>Write:  /  flight-carri...
Internal Table Template Condition <ul><li>... </li></ul><ul><li>loop at  tab where name cp ‘+r*’. </li></ul><ul><li>... </...
Reading Single Record <ul><li>... </li></ul><ul><li>Sort flight by carrid connid  fldate . </li></ul><ul><li>Read table fl...
Reading Single Record using Index <ul><ul><li>... </li></ul></ul><ul><li>Read table flight index 3. </li></ul><ul><li>If  ...
CHANGING INTERNAL TABLE <ul><ul><li>... </li></ul></ul><ul><li>Delete flight index 5. </li></ul><ul><li>Delete flight wher...
DELETING INTERNAL TABLE <ul><li>Clear  flight. </li></ul><ul><li>Refresh  flight. </li></ul><ul><li>Free  flight. </li></u...
Total Record of  Internal Table <ul><li>Data: line_count  type i. </li></ul><ul><li>Data tab like sflight occurs 0  with h...
Exercise I
Internal Table Processing <ul><li>Data tab like spfli occurs 0 with Header line. </li></ul><ul><li>… </li></ul><ul><li>Sel...
SELECT … INNER JOIN <ul><li>REPORT ZINNERJOIN01 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT  A...
Inner Join   into Internal Table <ul><li>REPORT ZJOIN01 . </li></ul><ul><li>DATA: begin of tab occurs 0, </li></ul><ul><li...
Internal Table Without Header Line <ul><li>DATA tab LIKE customers OCCURS 0. </li></ul><ul><li>DATA wa LIKE customers. </l...
Internal Table Declaration <ul><li>DATA tab TYPE TABLE OF customers. </li></ul><ul><li>DATA wa LIKE LINE OF customers. </l...
ABAP Practice
Database Table Processing <ul><li>INSERT </li></ul><ul><li>UPDATE </li></ul><ul><li>MODIFY </li></ul><ul><li>DELETE </li><...
Insert (Table) <ul><li>Tables  customers. </li></ul><ul><li>customers-id  = ‘999’. </li></ul><ul><li>customers-name = ‘Tes...
Update Statement <ul><li>Tables customers. </li></ul><ul><li>Select single * from customers where id = 1. </li></ul><ul><l...
Update Statement <ul><li>Data wa like  customers. </li></ul><ul><li>wa -id  =  ‘1’. </li></ul><ul><li>wa-nam e  = ‘Test No...
Modify Statement <ul><li>Tables customers. </li></ul><ul><li>customers-id  =  ‘1’. </li></ul><ul><li>customers-name  = ‘Te...
Deleting Database Table Entries <ul><li>Tables customers. </li></ul><ul><li>customers-id  =  ‘1’. </li></ul><ul><li>Delete...
Exercise II
Exercise II usr02-ltime usr02-trdat usr02-bname 1.  ห้ามใช้  SELECT * 2.  ใช้  Internal Table
Exercise III
Tables Relationship for User Master USR02-BNAME  USR02-TRDAT  ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER...
Exercise III : User Master usr02-bname usr02-trdat adcp-tel_number ใช้  Internal Table
Upcoming SlideShare
Loading in …5
×

03 abap3-090715081232-phpapp01

  • Be the first to comment

03 abap3-090715081232-phpapp01

  1. 1. ABAP Chapter 3 <ul><li>Open SQL </li></ul><ul><li>Internal Table </li></ul>
  2. 2. SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI SAP GUI
  3. 3. SAP SYSTEM (3 Tier Architecture) Presentation Layer (Windows based) Application Layer (Windows Server/UNIX) Database Server Database Layer (Windows Server/UNIX) M SAP Instance Oracle Informix DB2 MS SQL Server SAP DB/MaxDB G Dispatcher Request Queue D D B V S E SAP Buffer (Shared Mem) SAP GUI SAP GUI
  4. 4. SAP System : Dialog Processing Database Server Application Server Dispatcher Request Queue D D D D … SAP Buffer Program Table … 1 3 4 5 6 8 9 10 Report zpsm1. Tables customers. Select single * from customers where id = 1. Write: / customers-name. Execute ABAP statement Check Program in Program Buffer 7 Load&Gen Program SQL Request Send List Generate Screen(List) Send Request Request List 2 Search for free WP Store request to queue Send request to WP SAP GUI
  5. 5. Dialog Work Process Architecture TaskHandler DYNPRO Processor ABAP Processor Local Memory Memory Space DB Interface List buffer Database Server Dialog Work Process Result Set Memory
  6. 6. Open SQL <ul><li>SELECT ... </li></ul><ul><li>INSERT ... </li></ul><ul><li>UPDATE ... </li></ul><ul><li>DELETE ... </li></ul>
  7. 7. DB Interface SAP Application Server Local Memory Dialog WP TaskHandler DB Interface Result Set Database Server ~ 32 KB in length ABAP Processor DYNPRO Memory Space List Buffer
  8. 8. Example Tables in DB <ul><li>spfli </li></ul>customers BK NY NY cityto 250 SQ 0110 SQ 540 BK 0402 LH 100 LA 0400 LH distance cityfrom connid carrid London David 3 Singapore Peter 2 New York John 1 city name id
  9. 9. Example Tables in DB sflight 75 20010226 0110 SQ 130 20010228 0400 LH 145 20010110 0400 LH 150 20010101 0400 LH price fldate connid carrid
  10. 10. Select Overview <ul><li>Select < result > Which Columns? </li></ul><ul><li>From < table > Which Table? </li></ul><ul><li>Into < destination > Where to place? </li></ul><ul><li>Where < condition > Which Lines? </li></ul>
  11. 11. Select Statement <ul><li>Select multiple records from database </li></ul><ul><li>Select single record from database </li></ul>SELECT * FROM customers. … ENDSELECT. SELECT SINGLE * FROM customers WHERE id = 1. …
  12. 12. Select Multiple Records <ul><li>Tables spfli. </li></ul><ul><li>Seclect * from spfli. </li></ul><ul><li>write: / spfli-carrid, spfli-connid, </li></ul><ul><li>spf li-cityto. </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write: / ‘No Data’. </li></ul><ul><li>endif. </li></ul>
  13. 13. Dialog WP <ul><ul><ul><ul><li>Dialog WP </li></ul></ul></ul></ul>TaskHandler DYNPRO Processor ABAP Processor Database Local Memory Memory Space DB Interface Lis t b uffer Result Set
  14. 14. 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
  15. 15. Select … Into Table Structure <ul><li>Tables spfli. </li></ul><ul><li>Seclect * from spfli into spfli . </li></ul><ul><li>write: / spfli-carrid, spfli-connid, </li></ul><ul><li>spfli-cityfrom, spfli-cityto. </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write: / ‘No Data’. </li></ul><ul><li>endif. </li></ul>
  16. 16. Select … Into Work Area <ul><li>Data wa like spfli. </li></ul><ul><li>Seclect * from spfli into wa . </li></ul><ul><li>write: / wa -carrid, wa -connid, </li></ul><ul><li>wa -cityfrom, wa -cityto. </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write: / ‘No Data’. </li></ul><ul><li>endif. </li></ul>
  17. 17. Exercise I customers-id customers-name customers-city
  18. 18. SELECT with WHERE Clause
  19. 19. Loop Processing with Restriction <ul><li>Tables spfli. </li></ul><ul><li>Select * from spfli </li></ul><ul><li>where cityfrom = ‘FRANKFURT’. </li></ul><ul><li>write: / spfli-carrid, spfli-cityto. </li></ul><ul><li>endselect. </li></ul><ul><li>If sy-subrc <> 0. </li></ul><ul><li>write / ‘no data’. </li></ul><ul><li>endif. </li></ul>
  20. 20. Select With Range <ul><li>Tables sflight. </li></ul><ul><li>Select * From sflight </li></ul><ul><li>Where price between 100 and 1000. </li></ul><ul><li>Write: / sflight-carrid, sflight-connid, </li></ul><ul><li>sflight-price. </li></ul><ul><li>Endselect. </li></ul>
  21. 21. SELECT … With IN List <ul><li>Tables sflight. </li></ul><ul><li>Select * From sflight </li></ul><ul><li>Where price in ( 100, 1000 ). </li></ul><ul><li>Write: / sflight-carrid, sflight-connid, </li></ul><ul><li>sflight-price. </li></ul><ul><li>Endselect. </li></ul>
  22. 22. Select Single Record
  23. 23. Select Single Record <ul><li>Table s spfli. </li></ul><ul><li>Select single * from spfli </li></ul><ul><li>where carrid = ‘LH’ and </li></ul><ul><li> connid = ‘0400’. </li></ul><ul><li>if sy-subrc = 0. </li></ul><ul><li>write: / spfli-carrid, spfli-connid, </li></ul><ul><li> spfli-cityfrom, spfli-cityto. </li></ul><ul><li>else. </li></ul><ul><li>write: / ‘Data not found’. </li></ul><ul><li>endif. </li></ul>
  24. 24. Select Column List
  25. 25. Select * : Example SELECT *
  26. 26. Reading Selected Column <ul><li>Data: id like customers - id, </li></ul><ul><li>name like customers-name, </li></ul><ul><li>city like customers-city. </li></ul><ul><li>Select id name city </li></ul><ul><li>into ( id , name, city ) </li></ul><ul><li>from customers. </li></ul><ul><li>write: / id, name, city . </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write / ‘No Data found ’. </li></ul><ul><li>endif. </li></ul>
  27. 27. Reading Selected Column <ul><li>Data: begin of wa, </li></ul><ul><li>id like customers - id, </li></ul><ul><li>name like customers-name, </li></ul><ul><li>city like customers-city, </li></ul><ul><li>end of wa. </li></ul><ul><li>Select id name city </li></ul><ul><li>into wa </li></ul><ul><li>from customers. </li></ul><ul><li>write: / wa-id, wa-name , wa-city . </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write / ‘No Data found ’. </li></ul><ul><li>endif. </li></ul>
  28. 28. Select Column : Example I
  29. 29. Reading Selected Column <ul><li>Tables customers. </li></ul><ul><li>Select id name city </li></ul><ul><li>into (customers-id, customers-name, customers-city) </li></ul><ul><li>from customers. </li></ul><ul><li>write: / customers-id, customers-name, customers-city . </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write / ‘No Data found ’. </li></ul><ul><li>endif. </li></ul>
  30. 30. Select Column : Example II
  31. 31. Corresponding Fields of... <ul><li>Tables: customers. </li></ul><ul><li>Select id name city </li></ul><ul><li>into corresponding fields of customers </li></ul><ul><li>from customers. </li></ul><ul><li>Write: / customers-id, customers-name, </li></ul><ul><li>customers-city. </li></ul><ul><li>Endselect. </li></ul>
  32. 32. Select Statement : Special Topics
  33. 33. DB Count : SY-DBCNT <ul><li>Tables customers. </li></ul><ul><li>Select * from customers. </li></ul><ul><li>write: / sy-dbcnt , customers-id, customers-name . </li></ul><ul><li>endselect. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write : / ‘No Data found ’. </li></ul><ul><li>else. </li></ul><ul><li>write: / sy-dbcnt, ‘Record found’. </li></ul><ul><li>endif. </li></ul>
  34. 34. SELECT … ORDER BY ... <ul><li>Tables: spfli. </li></ul><ul><li>Select * from spfli </li></ul><ul><li>Order by cityfrom . </li></ul><ul><li>Write: / spfli-carrid, spfli-connid, </li></ul><ul><li>spfli-cityfrom. </li></ul><ul><li>Endselect. </li></ul>
  35. 35. SELECT … With Template <ul><li>Tables customers. </li></ul><ul><li>Select * From customers </li></ul><ul><li>Where name Like ‘_ r %’. </li></ul><ul><li>Write: / customers-id,customers-name. </li></ul><ul><li>Endselect. </li></ul>
  36. 36. Aggregate Functions <ul><li>Data: maxdat like sflight-distance, </li></ul><ul><li>mindat like sflight-distance, </li></ul><ul><li>counter type I. </li></ul><ul><li>Select C OUNT ( * ) M IN ( distance ) M AX ( distance ) </li></ul><ul><li>into (counter , m in dat, m ax dat) </li></ul><ul><li> from spfli. </li></ul><ul><li>Write: / ‘Count :’ , counter , </li></ul><ul><li>/ ‘Min :’ , mindat, </li></ul><ul><li>/ ‘Max :’ , maxdat . </li></ul>Aggregate Functions : COUNT,MIN,MAX,AVG and SUM
  37. 37. SELECT … GROUP BY ... <ul><li>Data: carrid like sflight-carrid, </li></ul><ul><li>mindat Type P Decimals 2, </li></ul><ul><li>maxdat Type P Decimals 2. </li></ul><ul><li>Select carrid Min( price ) Max( price ) </li></ul><ul><li>Into (carrid, mindat, maxdat) </li></ul><ul><li>From sflight </li></ul><ul><li>Group by carrid. </li></ul><ul><li>Write: / carrid, mindat, maxdat. </li></ul><ul><li>Endselect. </li></ul>อยากทราบว่า ในแต่ละสายการบิน มีราคาตั๋วต่ำสุดและสูงสุดเท่าไร sflight 75 20010226 0110 SQ 130 20010228 0400 LH 145 20010110 0400 LH 150 20010101 0400 LH Price fldate connid carrid
  38. 38. Sub Query <ul><li>tables customers. </li></ul><ul><li>select * </li></ul><ul><li>from customers </li></ul><ul><li>where id <> 1 and </li></ul><ul><li>city = </li></ul><ul><li>( select city </li></ul><ul><li>from customers </li></ul><ul><li>where id = 1 ). </li></ul><ul><li>write: / customers-id, customers-name. </li></ul><ul><li>endselect. </li></ul>ลูกค้าคนใดที่อยู่เมืองเดียวกับลูกค้ารหัส ID 1
  39. 39. Exercise I customers-id customers-name customers-city ห้ามใช้ SELECT *
  40. 40. Exercise II usr02-ltime usr02-trdat usr02-bname ห้ามใช้ SELECT *
  41. 41. ABAP : Inner Join
  42. 42. Tables Join <ul><li>spfli </li></ul>sflight BK NY BK cityto 250 SQ 0110 SQ 540 BK 0402 LH 100 NY 0400 LH distance cityfrom connid carrid 75 20010226 0110 SQ 130 20010228 0400 LH 145 20010110 0400 LH 150 20010101 0400 LH price fldate connid carrid
  43. 43. Tables Join <ul><li>Question: Select carrid, connid and cityto from spfli </li></ul><ul><li>and fldate,price from sflight where carrid = ‘LH’ </li></ul><ul><li>spfli-carrid spfli-connid sflight-fldate spfli-cityto sflight-price </li></ul><ul><li>เงื่อนไข : ให้แสดงข้อมูลเฉพาะสายการบิน ‘ LH’ เท่านั้น </li></ul>
  44. 44. Standard SQL <ul><li>Select spfli.carrid, spfli.connid, sflight.fldate, </li></ul><ul><li>sflight.price </li></ul><ul><li>From spfli, sflight </li></ul><ul><li>Where spfli.carrid = sflight.carrid and </li></ul><ul><li>spfli.connid = sflight.connid and </li></ul><ul><li> spfli.carrid = ‘LH’; </li></ul>
  45. 45. Tables Join Methods <ul><li>Nested select statement </li></ul><ul><li>Internal table </li></ul><ul><li>View </li></ul><ul><li>Inner join of Select statement </li></ul>
  46. 46. Nested Select Statement <ul><li>Tables: spfli,sflight. </li></ul><ul><li>Select * from spfli where carrid = ‘LH’. </li></ul><ul><li>Select * from sflight </li></ul><ul><li>where carrid = spfli-carrid and </li></ul><ul><li>connid = spfli-connid. </li></ul><ul><li>Write: / spfli-carrid, spfli-connid, sflight-fldate, </li></ul><ul><li>sflight-price. </li></ul><ul><li>Endselect. </li></ul><ul><li>Endselect. </li></ul>
  47. 47. Open SQL – Inner Join <ul><li>Tables: spfli,sflight. </li></ul><ul><li>Select spfli~carrid spfli~connid sflight~fldate spfli~cityto sflight~price </li></ul><ul><li>into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price) </li></ul><ul><li>from spfli inner join sflight </li></ul><ul><li>on spfli~carrid = sflight~carrid and </li></ul><ul><li>spfli~connid = sflight~connid </li></ul><ul><li>where spfli~carrid = ‘LH’. </li></ul><ul><li>Write: / spfli-carrid, spfli-connid, sflight-fldate, </li></ul><ul><li>spfli-cityto, sflight-price. </li></ul><ul><li>Endselect. </li></ul>
  48. 48. Open SQL – Inner Join <ul><li>Tables: A,B. </li></ul><ul><li>Select A~a B~b B~c </li></ul><ul><li>into (A-a,B-b,B-c) </li></ul><ul><li>from A inner join B </li></ul><ul><li>on A~b = B~b. </li></ul><ul><li>Write: / A-a,B-b,B-c. </li></ul><ul><li>Endselect. </li></ul>Table : A Table : B A-a B-b B-c b2 a2 b1 a1 b a c2 b2 c3 b3 c1 b1 c b
  49. 49. Open SQL – Inner Join Table : A Table : B Single Result Table(Result set) Select … inner join.. Endselect. Database Server Application Server 1 2 b2 a2 b1 a1 b a c2 b2 c3 b3 c1 b1 c b b2 b1 B~b c2 a2 c1 a1 B~c A~a
  50. 50. Open SQL – Alias Table Name <ul><li>Tables: spfli,sflight. </li></ul><ul><li>Select a~carrid a~connid b~fldate a~cityto b~price </li></ul><ul><li>into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price) </li></ul><ul><li>from spfli as a inner join sflight as b </li></ul><ul><li>on a~carrid = b~carrid and </li></ul><ul><li>a~connid = b~connid </li></ul><ul><li>where a~carrid = ‘LH’. </li></ul><ul><li>Write: / spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price. </li></ul><ul><li>Endselect. </li></ul>
  51. 51. Inner Join/Outer Join Example ZPRODUCTS ZSALES ZSALEREPS ZCUSTOMERS 432555 Singapore David 3 234111 Bangkok Micheal 4 222222 London Peter 2 111111 New York John 1 tel city name id CD Tape Ruler Pencil Pen prod_name 120 X1 80 B1 99 Y1 125 A2 100 A1 on_hand p_id 90 50 10 qty 02 20020321 X1 3 01 20020318 A2 1 01 20020318 A1 1 sale_id sale_date prod_id cust_id Pipop 02 Somchai 01 name sale_id
  52. 52. Open SQL – Inner Join <ul><li>REPORT ZINNERJOIN01 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT A~NAME B~PROD_ID </li></ul><ul><li>INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID) </li></ul><ul><li>FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A </li></ul><ul><li>ON B~CUST_ID = A~ID. </li></ul><ul><li>WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID. </li></ul><ul><li>ENDSELECT. </li></ul>
  53. 53. Open SQL – Inner Join > 2 Tables <ul><li>Tables: A,B,C. </li></ul><ul><li>Select A~a B~c C~y </li></ul><ul><li>into (A-a,B-c,C-y) </li></ul><ul><li>from A inner join B </li></ul><ul><li>on A~b = B~b </li></ul><ul><li>inner join C </li></ul><ul><li>on C~x = B~c. </li></ul><ul><li>Write: / A-a,B-c,C-y. </li></ul><ul><li>Endselect. </li></ul>Table : A Table : B Table : C A-a B-c C-y … … b a ... … … … ... … c b ... … y x
  54. 54. Open SQL – Inner Join > 2 Tables <ul><li>REPORT ZINNERJOIN02 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZPRODUCTS,ZSALES. </li></ul><ul><li>SELECT A~NAME C~PROD_NAME B~QTY </li></ul><ul><li>INTO (ZCUSTOMERS-NAME, ZPRODUCTS-PROD_NAME, ZSALES-QTY) </li></ul><ul><li>FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A </li></ul><ul><li>ON B~CUST_ID = A~ID </li></ul><ul><li>INNER JOIN ZPRODUCTS AS C </li></ul><ul><li>ON C~P_ID = B~PROD_ID. </li></ul><ul><li>WRITE: / ZCUSTOMERS-NAME,ZPRODUCTS-PROD_NAME,ZSALES-QTY. </li></ul><ul><li>ENDSELECT. </li></ul>
  55. 55. Exercise <ul><li>List customers who buy product from company as following fields: </li></ul><ul><li>zcustomers-id </li></ul><ul><li>zcustomers-name </li></ul><ul><li>zsales-sale_date </li></ul><ul><li>zproducts-prod_name </li></ul><ul><li>zsales-qty </li></ul><ul><li>zsalereps-name </li></ul>
  56. 56. Exercise : User Master USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER Tables Relationship
  57. 57. ABAP : Outer Join
  58. 58. Open SQL – Outer Join <ul><li>REPORT ZOUTERJOIN . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT A~NAME B~PROD_ID </li></ul><ul><li>INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID) </li></ul><ul><li>FROM ZCUSTOMERS AS A LEFT OUTER JOIN ZSALES AS B </li></ul><ul><li>ON A~ID = B~CUST_ID. </li></ul><ul><li>WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID. </li></ul><ul><li>ENDSELECT. </li></ul>Single Result Table B~PROD_ID A~NAME X1 David Micheal Peter A2 John A1 John
  59. 59. Exercise <ul><li>List customers name who do not buy any product from company </li></ul>
  60. 60. Sub Query <ul><li>REPORT ZSUBQUERY . </li></ul><ul><li>tables: zcustomers. </li></ul><ul><li>select * from zcustomers as a </li></ul><ul><li>where not exists </li></ul><ul><li>( select * </li></ul><ul><li>from zsales as b </li></ul><ul><li>where b~cust_id = a~id ). </li></ul><ul><li>write: / zcustomers-name. </li></ul><ul><li>endselect. </li></ul>ลูกค้าชื่ออะไรที่ไม่ได้ซื้อสินค้าจากเรา มีใครบ้าง
  61. 61. Internal Table
  62. 62. Data Objects in ABAP <ul><li>Memory Space </li></ul>Structure Table Structure Internal Table Variable Constants <Field-symbols>
  63. 63. INTERNAL TABLE <ul><li>Flight (Structure) </li></ul>Carrid Connid Date Price Internal Table Flight (Internal Table) Carrid Connid Date Price Header Line
  64. 64. Structure <ul><li>Data: Begin of flight, </li></ul><ul><li>carrid like sflight-carrid, </li></ul><ul><li>connid like sflight-connid, </li></ul><ul><li>date like sflight-fldate, </li></ul><ul><li>price like sflight-price. </li></ul><ul><li>Data: End of flight. </li></ul><ul><li>flight-carrid = ‘LH’. </li></ul><ul><li>Write: / flight-carrid. </li></ul>
  65. 65. INTERNAL TABLE <ul><li>Data: begin of tab occurs 10, </li></ul><ul><li>carrid like sflight-carrid, </li></ul><ul><li>connid like sflight-connid, </li></ul><ul><li>fldate like sflight-fldate, </li></ul><ul><li>price like sflight-price. </li></ul><ul><li>Data end of tab. </li></ul>
  66. 66. USING ABAP DICTIONARY STRUCTURE <ul><li>Data: begin of tab occurs 0 . </li></ul><ul><li>Include structure sflight. </li></ul><ul><li>Data end of tab. </li></ul>
  67. 67. INTERNAL TABLE USING LIKE <ul><li>Data tab LIKE sflight OCCURS 0 WITH HEADER LINE . </li></ul>
  68. 68. FILLING INTERNAL TABLE (APPEND) <ul><li>Tables sflight. </li></ul><ul><li>Data flight like sflight occurs 0 with header line. </li></ul><ul><li>Select * from sflight. </li></ul><ul><li>Move sflight to flight. </li></ul><ul><li>Append flight. </li></ul><ul><li>Endselect. </li></ul>
  69. 69. Standard Key of Internal Table Data: begin of tab occurs 0, f1 type C , f2 type I, f3 type N , f4 type P, end of tab. tab f4 f3 f2 f1
  70. 70. Reading Data From Internal Table <ul><li>Data tab like sflight occurs 0 with header line. </li></ul><ul><li>Select * from sflight into table tab . </li></ul><ul><li>If sy-subrc = 0. </li></ul><ul><li>Loop at tab. </li></ul><ul><li>Write: / tab-carrid, tab-price. </li></ul><ul><li>Endloop. </li></ul><ul><li>Else. </li></ul><ul><li>Write: / ‘No Data’. </li></ul><ul><li>Endif. </li></ul>
  71. 71. Access Database Without Internal Table
  72. 72. Access Database Using Internal Table
  73. 73. Reading Data From Internal Table <ul><li>Data : begin of tab occurs 0 , </li></ul><ul><li>id like customers-id, </li></ul><ul><li>name like customers-name, </li></ul><ul><li>end of tab. </li></ul><ul><li>Select id name from customers into table tab . </li></ul><ul><li>If sy-subrc = 0. </li></ul><ul><li>Loop at tab. </li></ul><ul><li>Write: / tab-id, tab -name . </li></ul><ul><li>Endloop. </li></ul><ul><li>else . </li></ul><ul><li>Write : / ‘No Data’. </li></ul><ul><li>Endif. </li></ul>
  74. 74. Exercise I : Change Using Internal Table
  75. 75. SORTING INTERNAL TABLE (SORT) <ul><li>Sort flight. </li></ul><ul><li>Sort flight by price fldate. </li></ul><ul><li>Sort flight by price ascending </li></ul><ul><li>fldate descending. </li></ul>
  76. 76. <ul><li>Data tab like spfli occurs 0 with header line. </li></ul><ul><li>Select * from spfli into table tab. </li></ul><ul><li>Sort tab by cityfrom. </li></ul><ul><li>… </li></ul><ul><li>Loop at tab. </li></ul><ul><li>write: / tab-carrid, tab-connid,tab-cityfrom. </li></ul><ul><li>Endloop. </li></ul>SORTING INTERNAL TABLE
  77. 77. PROCESSING INTERNAL TABLE <ul><ul><li>... </li></ul></ul><ul><li>Loop at flight. </li></ul><ul><li>Write: / flight-carrid, flight-connid. </li></ul><ul><li>Endloop. </li></ul><ul><li>Loop at flight where carrid = ‘LH’. </li></ul><ul><li>Write: / flight-carrid , flight-connid. </li></ul><ul><li>Endloop. </li></ul><ul><li>Loop at flight from 1 to 10. </li></ul><ul><li>Write: / sy-tabix , flight-carrid , flight-connid. </li></ul><ul><li>Endloop. </li></ul>
  78. 78. Internal Table Template Condition <ul><li>... </li></ul><ul><li>loop at tab where name cp ‘+r*’. </li></ul><ul><li>... </li></ul>
  79. 79. Reading Single Record <ul><li>... </li></ul><ul><li>Sort flight by carrid connid fldate . </li></ul><ul><li>Read table flight </li></ul><ul><li>with key c arrid = ‘LH’ </li></ul><ul><li>c onnid = ‘0400’ </li></ul><ul><li>fldate = ‘19990201’ </li></ul><ul><li>Binary Search. </li></ul><ul><li>if sy-subrc = 0 . </li></ul><ul><li>write : / flight-carrid,flight-connid, </li></ul><ul><li>flight-fldate, flight-price. </li></ul><ul><li>endif. </li></ul>
  80. 80. Reading Single Record using Index <ul><ul><li>... </li></ul></ul><ul><li>Read table flight index 3. </li></ul><ul><li>If sy-subrc = 0. </li></ul><ul><li>write: / flight-carrid , flight-connid . </li></ul><ul><li>Endif. </li></ul>
  81. 81. CHANGING INTERNAL TABLE <ul><ul><li>... </li></ul></ul><ul><li>Delete flight index 5. </li></ul><ul><li>Delete flight where carrid = ‘LH’. </li></ul><ul><li>flight-carrid = ‘XX’. </li></ul><ul><li>flight-price = 100. </li></ul><ul><li>… </li></ul><ul><li>Insert flight index 1. </li></ul>
  82. 82. DELETING INTERNAL TABLE <ul><li>Clear flight. </li></ul><ul><li>Refresh flight. </li></ul><ul><li>Free flight. </li></ul>DATA flight LIKE sflight occurs 0 with header line.
  83. 83. Total Record of Internal Table <ul><li>Data: line_count type i. </li></ul><ul><li>Data tab like sflight occurs 0 with header line. </li></ul><ul><li>Select * from sflight into table tab. </li></ul><ul><li>Describe table tab lines line_count. </li></ul><ul><li>Write: / line_count. </li></ul>
  84. 84. Exercise I
  85. 85. Internal Table Processing <ul><li>Data tab like spfli occurs 0 with Header line. </li></ul><ul><li>… </li></ul><ul><li>Select * from spfli </li></ul><ul><li>appending table tab </li></ul><ul><li>where carrid = ‘LH’. </li></ul>
  86. 86. SELECT … INNER JOIN <ul><li>REPORT ZINNERJOIN01 . </li></ul><ul><li>TABLES: ZCUSTOMERS,ZSALES. </li></ul><ul><li>SELECT A~NAME B~PROD_ID </li></ul><ul><li>INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID) </li></ul><ul><li>FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A </li></ul><ul><li>ON B~CUST_ID = A~ID. </li></ul><ul><li>WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID. </li></ul><ul><li>ENDSELECT. </li></ul>
  87. 87. Inner Join into Internal Table <ul><li>REPORT ZJOIN01 . </li></ul><ul><li>DATA: begin of tab occurs 0, </li></ul><ul><li>name like zcustomers-name, </li></ul><ul><li>prod_id like zsales-prod_id, </li></ul><ul><li>end of tab. </li></ul><ul><li>SELECT A~NAME B~PROD_ID </li></ul><ul><li>INTO TABLE tab </li></ul><ul><li>FROM ZSALES AS B INNER JOIN ZCUSTOMERS AS A </li></ul><ul><li>ON B~CUST_ID = A~ID. </li></ul><ul><li>… </li></ul><ul><li>LOOP AT tab. </li></ul><ul><li>WRITE: / TAB-NAME,TAB-PROD_ID. </li></ul><ul><li>ENDLOOP. </li></ul>
  88. 88. Internal Table Without Header Line <ul><li>DATA tab LIKE customers OCCURS 0. </li></ul><ul><li>DATA wa LIKE customers. </li></ul><ul><li>… </li></ul><ul><li>LOOP AT tab INTO wa . </li></ul><ul><li>WRITE: / wa-id, wa-name. </li></ul><ul><li>ENDLOOP. </li></ul>
  89. 89. Internal Table Declaration <ul><li>DATA tab TYPE TABLE OF customers. </li></ul><ul><li>DATA wa LIKE LINE OF customers. </li></ul><ul><li>… </li></ul>
  90. 90. ABAP Practice
  91. 91. Database Table Processing <ul><li>INSERT </li></ul><ul><li>UPDATE </li></ul><ul><li>MODIFY </li></ul><ul><li>DELETE </li></ul>Database
  92. 92. Insert (Table) <ul><li>Tables customers. </li></ul><ul><li>customers-id = ‘999’. </li></ul><ul><li>customers-name = ‘Test’. </li></ul><ul><li>Insert customers. </li></ul><ul><li>if sy-subrc <> 0. </li></ul><ul><li>write: / ‘Data Already Exists’. </li></ul><ul><li>endif. </li></ul>
  93. 93. Update Statement <ul><li>Tables customers. </li></ul><ul><li>Select single * from customers where id = 1. </li></ul><ul><li>If sy-subrc = 0. </li></ul><ul><li>customers-name = ‘John’. </li></ul><ul><li>update customers. </li></ul><ul><li>Endif. </li></ul>Update customers set name = ‘John’ where id = 1.
  94. 94. Update Statement <ul><li>Data wa like customers. </li></ul><ul><li>wa -id = ‘1’. </li></ul><ul><li>wa-nam e = ‘Test No 1’. </li></ul><ul><li>wa-city = ‘Bangkok’. </li></ul><ul><li>update customers from wa . </li></ul><ul><li>If sy-subrc <> 0. </li></ul><ul><li>write: / ‘Data not found’. </li></ul><ul><li>Endif. </li></ul>
  95. 95. Modify Statement <ul><li>Tables customers. </li></ul><ul><li>customers-id = ‘1’. </li></ul><ul><li>customers-name = ‘Test No 1’. </li></ul><ul><li>Modify customers. </li></ul>
  96. 96. Deleting Database Table Entries <ul><li>Tables customers. </li></ul><ul><li>customers-id = ‘1’. </li></ul><ul><li>Delete customers. </li></ul><ul><li>Delete customers From Table delcustomers. </li></ul><ul><li>Delete From customers Where city = ‘ Bangkok ’. </li></ul>
  97. 97. Exercise II
  98. 98. Exercise II usr02-ltime usr02-trdat usr02-bname 1. ห้ามใช้ SELECT * 2. ใช้ Internal Table
  99. 99. Exercise III
  100. 100. Tables Relationship for User Master USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER Tables Relationship
  101. 101. Exercise III : User Master usr02-bname usr02-trdat adcp-tel_number ใช้ Internal Table

×