ABAP Chapter 3   Open SQL   Internal Table
SAP System : 3 Tier Client/ServerSAP GUI           SAP GUI         SAP GUI   Presentation                                 ...
SAP SYSTEM (3 Tier Architecture)                                       SAP GUI            SAP GUIPresentation Layer(Window...
SAP System : Dialog Processing                                    SAP GUI                                                 ...
Dialog Work Process Architecture   Dialog Work Process             Local Memory                                      Memor...
Open SQL   SELECT ...   INSERT ...   UPDATE ...   DELETE ...
DB InterfaceSAP Application Server        Dialog WP                 Local Memory                  TaskHandler      Memory ...
Example Tables in DB                                                 customersspfli                                       ...
Example Tables in DBsflightcarrid   connid     fldate   price LH      0400     20010101     150 LH      0400     20010110 ...
Select OverviewSelect <result>         Which Columns? From <table>           Which Table?   Into <destination>   Where to ...
Select Statement   Select multiple records from database           SELECT * FROM customers.            …           ENDSEL...
Select Multiple RecordsTables spfli.Seclect * from spfli.       write: / spfli-carrid, spfli-connid,                 spfli...
Dialog WPDialog WP                Local Memory                         Memory Space   TaskHandler   ABAP Processor        ...
SELECT Statement Working Steps1. Transform open SQL to DB SQL and return result set into result set work area   SELECT * F...
Select … Into Table StructureTables spfli.Seclect * from spfli into spfli.       write: / spfli-carrid, spfli-connid,     ...
Select … Into Work AreaData wa like spfli.Seclect * from spfli into wa.       write: / wa-carrid, wa-connid,              ...
Exercise I                customers-name   customers-citycustomers-id
SELECT with WHERE Clause
Loop Processing with RestrictionTables spfli.Select * from spfli           where cityfrom = ‘FRANKFURT’.   write: / spfli-...
Select With RangeTables     sflight.Select * From sflight           Where price between 100 and           1000. Write:    ...
SELECT … With IN ListTables     sflight.Select * From sflight     Where price in ( 100, 1000 ). Write: /       sflight-car...
Select Single Record
Select Single RecordTables spfli.Select single * from spfli     where carrid = ‘LH’ and             connid = ‘0400’.if sy-...
Select Column List
Select * : ExampleSELECT *
Reading Selected ColumnData: id like customers-id,        name like customers-name,        city like customers-city.Select...
Reading Selected ColumnData: begin of wa,       id    like customers-id,       name like customers-name,       city like c...
Select Column : Example I
Reading Selected ColumnTables customers.Select id name city  into (customers-id, customers-name, customers-city)      from...
Select Column : Example II
Corresponding Fields of...Tables: customers.Select id name city into corresponding fields of customers   from customers. W...
Select Statement : Special Topics
DB Count : SY-DBCNTTables customers.Select * from customers.   write: / sy-dbcnt, customers-id, customers-name.endselect.i...
SELECT … ORDER BY ...Tables: spfli.Select * from spfli          Order by cityfrom. Write: / spfli-carrid, spfli-connid,   ...
SELECT … With TemplateTables     customers.Select * From customers            Where name Like ‘_r%’.  Write: /     custome...
Aggregate Functions      Data: maxdat like sflight-distance,            mindat like sflight-distance,            counter t...
SELECT … GROUP BY ...    Data: carrid like sflight-carrid,                              sflight         mindat Type P Deci...
Sub Querytables customers.               ลูกค้าคนใดทีอยู่เมือง                                              ่             ...
Exercise I               ห้ามใช้ SELECT *                  customers-name   customers-citycustomers-id
Exercise II              ห้ามใช้ SELECT *usr02-bname       usr02-trdat    usr02-ltime
ABAP : Inner Join
Tables Join                                                 sflightspfli                                                 c...
Tables JoinQuestion: Select carrid, connid and cityto from spfli         and fldate,price from sflight where carrid = ‘LH’...
Standard SQLSelect spfli.carrid, spfli.connid, sflight.fldate,        sflight.price   From spfli, sflight    Where spfli.c...
Tables Join Methods   Nested select statement   Internal table   View   Inner join of Select statement
Nested Select StatementTables: spfli,sflight.Select * from spfli where carrid = ‘LH’. Select * from sflight     where carr...
Open SQL – Inner JoinTables: spfli,sflight.Select spfli~carrid spfli~connid sflight~fldate spfli~cityto sflight~price  int...
Open SQL – Inner JoinTables: A,B.                            A-a B-b B-cSelect A~a B~b B~c   into (A-a,B-b,B-c)      from ...
Open SQL – Inner Join                 Table : A                    Table : BDatabase                     a             b  ...
Open SQL – Alias Table NameTables: spfli,sflight.Select a~carrid a~connid b~fldate a~cityto b~price into (spfli-carrid, sp...
Inner Join/Outer Join Example  ZCUSTOMERS                                             ZSALEREPS id     name            cit...
Open SQL – Inner JoinREPORT ZINNERJOIN01 .TABLES: ZCUSTOMERS,ZSALES.SELECT A~NAME B~PROD_ID INTO (ZCUSTOMERS-NAME,ZSALES-P...
Open SQL – Inner Join > 2 Tables                                       Table : CTables: A,B,C.           A-a B-c C-ySelect...
Open SQL – Inner Join > 2 TablesREPORT ZINNERJOIN02 .TABLES: ZCUSTOMERS,ZPRODUCTS,ZSALES.SELECT A~NAME C~PROD_NAME B~QTY I...
Exercise   List customers who buy product from    company as following fields:      zcustomers-id     zcustomers-name ...
Exercise : User MasterUSR02-BNAME    USR02-TRDAT   ADCP-TEL_NUMBER   Tables Relationship                                  ...
ABAP : Outer Join
Open SQL – Outer JoinREPORT ZOUTERJOIN .TABLES: ZCUSTOMERS,ZSALES.SELECT A~NAME B~PROD_ID INTO (ZCUSTOMERS-NAME,ZSALES-PRO...
Exercise   List customers name who do not buy any    product from company
Sub QueryREPORT ZSUBQUERY .tables: zcustomers.               ลูกค้าชื่ออะไรที่ไม่ได้ซื้อ                                  ...
Internal Table
Data Objects in ABAPMemory Space   Variable                 Structure  Table Structure                       Internal Tabl...
INTERNAL TABLE    Flight (Structure)      Carrid     Connid   Date   PriceInternal Table    Flight (Internal Table)       ...
StructureData:     Begin of   flight,          carrid       like sflight-carrid,          connid       like sflight-connid...
INTERNAL TABLEData:   begin of tab occurs 10,        carrid like sflight-carrid,        connid like sflight-connid,       ...
USING ABAP DICTIONARY STRUCTUREData:   begin of tab occurs 0.        Include structure sflight.Data    end of tab.
INTERNAL TABLE USING LIKEData tab LIKE sflight OCCURS 0 WITH HEADER LINE.
FILLING INTERNAL TABLE (APPEND)Tables sflight.Data flight like sflight occurs 0 with header line.Select * from sflight. Mo...
Standard Key of Internal Table                               tabData: begin of tab occurs 0,   f1    f2   f3   f4     f1 t...
Reading Data From Internal TableData tab like sflight occurs 0 with header line.Select * from sflight into table tab.If sy...
Access Database Without Internal Table
Access Database Using Internal Table
Reading Data From Internal TableData: begin of tab occurs 0,       id like customers-id,       name like customers-name,  ...
Exercise I : Change   Using Internal Table
SORTING INTERNAL TABLE (SORT)Sort   flight.Sort   flight by   price fldate.Sort   flight by   price ascending             ...
SORTING INTERNAL TABLEData tab like spfli occurs 0 with header line.Select * from spfli into table tab.Sort tab by cityfro...
PROCESSING INTERNAL TABLE    ...Loop at flight. Write: / flight-carrid, flight-connid.Endloop.Loop at flight where carrid ...
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’                c...
Reading Single Record using Index         ...Read table flight index 3.If sy-subrc = 0.   write: / flight-carrid, flight-c...
CHANGING INTERNAL TABLE       ... Delete      flight index 5. Delete      flight where carrid = ‘LH’. flight-carrid = ‘XX’...
DELETING INTERNAL TABLEDATA flight LIKE sflight occurs 0 with header line.        Clear flight.        Refresh flight.    ...
Total Record of Internal TableData: line_count type i.Data tab like sflight occurs 0 with header line.Select * from sfligh...
Exercise I
Internal Table ProcessingData tab like spfli occurs 0 with Header line.…Select * from spfli        appending table tab    ...
SELECT … INNER JOINREPORT ZINNERJOIN01 .TABLES: ZCUSTOMERS,ZSALES.SELECT A~NAME B~PROD_ID INTO (ZCUSTOMERS-NAME,ZSALES-PRO...
Inner Join into Internal TableREPORT ZJOIN01 .DATA: begin of tab occurs 0,     name like zcustomers-name,     prod_id like...
Internal Table Without Header LineDATA tab LIKE customers OCCURS 0.DATA wa LIKE customers.…LOOP AT tab INTO wa. WRITE: / w...
Internal Table DeclarationDATA tab TYPE TABLE OF customers.DATA wa LIKE LINE OF customers.…
ABAP Practice
Database Table Processing   INSERT   UPDATE               Database   MODIFY   DELETE
Insert (Table)Tables customers.customers-id = ‘999’.customers-name = ‘Test’.Insert customers.if sy-subrc <> 0.   write: / ...
Update StatementTables customers.Select single * from customers where id    = 1.If sy-subrc = 0.   customers-name = ‘John’...
Update StatementData wa like customers.wa-id = ‘1’.wa-name = ‘Test No 1’.wa-city = ‘Bangkok’.update customers from wa.If s...
Modify StatementTables customers.customers-id = ‘1’.customers-name = ‘Test No 1’.Modify customers.
Deleting Database Table EntriesTables customers.customers-id = ‘1’.Delete customers.Delete customers From Table   delcusto...
Exercise II
Exercise II1. ห้ามใช้ SELECT * 2. ใช้ Internal Table    usr02-bname    usr02-trdat   usr02-ltime
Exercise III
Tables Relationship for User MasterUSR02-BNAME   USR02-TRDAT   ADCP-TEL_NUMBER   Tables Relationship                      ...
Exercise III : User Master                          usr02-trdatusr02-bname                                        adcp-tel...
Upcoming SlideShare
Loading in …5
×

03 abap3-090715081232-phpapp01 (1)

524 views
474 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
524
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

03 abap3-090715081232-phpapp01 (1)

  1. 1. ABAP Chapter 3 Open SQL Internal Table
  2. 2. SAP System : 3 Tier Client/ServerSAP GUI SAP GUI SAP GUI Presentation Server SAP Application Server DB Server
  3. 3. SAP SYSTEM (3 Tier Architecture) SAP GUI SAP GUIPresentation Layer(Windows based) SAP InstanceApplication Layer Dispatcher M(Windows Server/UNIX) Request SAP Buffer Queue (Shared Mem) D D B V S E G OracleDatabase Layer Informix(Windows Server/UNIX) DB2 Database Server MS SQL Server SAP DB/MaxDB
  4. 4. SAP System : Dialog Processing SAP GUI Report zpsm1. Request Tables customers. List Select single * from Generate 1 10 customers where id = 1. Screen(List)Application Server Send Request Write: / customers-name.Store requestto queue3 Dispatcher Send 2 Search for SAP Buffer List 9 free WP Request Check Program in 7 Program Queue Send request Program Buffer 5 Execute to WP 4 ABAP Table D D D … D stateme nt … 8 6 SQL Load&GenDatabase Server Request Program
  5. 5. 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
  6. 6. Open SQL SELECT ... INSERT ... UPDATE ... DELETE ...
  7. 7. DB InterfaceSAP Application Server Dialog WP Local Memory TaskHandler Memory Space ABAP Processor DYNPRO List Buffer DB Interface Result Set ~ 32 KB in lengthDatabase Server Data Data Data Data Data
  8. 8. Example Tables in DB customersspfli id name citycarrid connid cityfrom cityto distance 1 John New York LH 0400 LA NY 100 2 Peter Singapore LH 0402 BK NY 540 3 David London SQ 0110 SQ BK 250
  9. 9. Example Tables in DBsflightcarrid connid fldate price LH 0400 20010101 150 LH 0400 20010110 145 LH 0400 20010228 130 SQ 0110 20010226 75
  10. 10. Select OverviewSelect <result> Which Columns? From <table> Which Table? Into <destination> Where to place? Where <condition> Which Lines?
  11. 11. Select Statement Select multiple records from database SELECT * FROM customers. … ENDSELECT. Select single record from database SELECT SINGLE * FROM customers WHERE id = 1. …
  12. 12. Select Multiple RecordsTables spfli.Seclect * from spfli. write: / spfli-carrid, spfli-connid, spfli-cityto.endselect.if sy-subrc <> 0. write: / ‘No Data’.endif.
  13. 13. Dialog WPDialog WP Local Memory Memory Space TaskHandler ABAP Processor List buffer DYNPRO Processor DB Interface Result Set Database
  14. 14. SELECT Statement Working Steps1. Transform open SQL to DB SQL and return result set into result set work area SELECT * FROM spfli. SELECT * FROM spfli; … ENDSELECT.2. Loop with data in result set and transfer each record towork area in memory space Table Structure in Memory SELECT * FROM spfli. Space … ENDSELECT.
  15. 15. Select … Into Table StructureTables spfli.Seclect * from spfli into spfli. write: / spfli-carrid, spfli-connid, spfli-cityfrom, spfli-cityto.endselect.if sy-subrc <> 0. write: / ‘No Data’.endif.
  16. 16. Select … Into Work AreaData 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.
  17. 17. Exercise I customers-name customers-citycustomers-id
  18. 18. SELECT with WHERE Clause
  19. 19. Loop Processing with RestrictionTables spfli.Select * from spfli where cityfrom = ‘FRANKFURT’. write: / spfli-carrid, spfli-cityto.endselect.If sy-subrc <> 0. write / ‘no data’.endif.
  20. 20. Select With RangeTables sflight.Select * From sflight Where price between 100 and 1000. Write: / sflight-carrid, sflight-connid, sflight-price.Endselect.
  21. 21. SELECT … With IN ListTables sflight.Select * From sflight Where price in ( 100, 1000 ). Write: / sflight-carrid, sflight-connid, sflight-price.Endselect.
  22. 22. Select Single Record
  23. 23. Select Single RecordTables 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.
  24. 24. Select Column List
  25. 25. Select * : ExampleSELECT *
  26. 26. Reading Selected ColumnData: 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.
  27. 27. Reading Selected ColumnData: 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.
  28. 28. Select Column : Example I
  29. 29. Reading Selected ColumnTables 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.
  30. 30. Select Column : Example II
  31. 31. Corresponding Fields of...Tables: customers.Select id name city into corresponding fields of customers from customers. Write: / customers-id, customers-name, customers-city.Endselect.
  32. 32. Select Statement : Special Topics
  33. 33. DB Count : SY-DBCNTTables 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.
  34. 34. SELECT … ORDER BY ...Tables: spfli.Select * from spfli Order by cityfrom. Write: / spfli-carrid, spfli-connid, spfli-cityfrom.Endselect.
  35. 35. SELECT … With TemplateTables customers.Select * From customers Where name Like ‘_r%’. Write: / customers-id,customers-name.Endselect.
  36. 36. 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
  37. 37. SELECT … GROUP BY ... Data: carrid like sflight-carrid, sflight mindat Type P Decimals 2, maxdat Type P Decimals 2. carrid connid fldate Price Select carrid Min( price )LHMax( price ) 0400 20010101 150 Into (carrid, mindat, maxdat) LH 0400 20010110 145 From sflight Group by carrid. LH 0400 20010228 130 Write: / carrid, mindat,SQ 0110 20010226 maxdat. 75 Endselect.ยากทราบว่า ในแต่ละสายการบิน มีราคาตั๋วตำำาสุดและสูงสุดเท่าไร
  38. 38. Sub Querytables customers. ลูกค้าคนใดทีอยู่เมือง ่ เดียวกับลูกค้ารหัส ID 1select * from customers where id <> 1 and city = ( select city from customers where id = 1 ). write: / customers-id, customers-name.endselect.
  39. 39. Exercise I ห้ามใช้ SELECT * customers-name customers-citycustomers-id
  40. 40. Exercise II ห้ามใช้ SELECT *usr02-bname usr02-trdat usr02-ltime
  41. 41. ABAP : Inner Join
  42. 42. Tables Join sflightspfli carrid connid fldate pricecarrid 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
  43. 43. Tables JoinQuestion: 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’ เท่านัน ้
  44. 44. Standard SQLSelect 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’;
  45. 45. Tables Join Methods Nested select statement Internal table View Inner join of Select statement
  46. 46. Nested Select StatementTables: 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.
  47. 47. Open SQL – Inner JoinTables: spfli,sflight.Select spfli~carrid spfli~connid sflight~fldate spfli~cityto sflight~price into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price) 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.
  48. 48. Open SQL – Inner JoinTables: A,B. A-a B-b B-cSelect A~a B~b B~c into (A-a,B-b,B-c) from A inner join B on A~b = B~b. Table : B Write: / A-a,B-b,B-c. b cEndselect. Table : A b1 c1 a b b2 c2 a1 b1 b3 c3 a2 b2
  49. 49. Open SQL – Inner Join Table : A Table : BDatabase a b b c Server a1 b1 b1 c1 a2 b2 b2 c2 b3 c3Application Server Single Result Table(Result set) 1 2 Select … A~a B~b B~c inner join.. a1 b1 c1 Endselect. a2 b2 c2
  50. 50. Open SQL – Alias Table NameTables: 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-priceEndselect.
  51. 51. 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_idZPRODUCTS 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
  52. 52. Open SQL – Inner JoinREPORT 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.
  53. 53. Open SQL – Inner Join > 2 Tables Table : CTables: A,B,C. A-a B-c C-ySelect 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 … …
  54. 54. Open SQL – Inner Join > 2 TablesREPORT 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.
  55. 55. 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
  56. 56. Exercise : User MasterUSR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER Tables Relationship USR02 BNAME USR21 PERSNUMBER ADDRNUMBER ADCP
  57. 57. ABAP : Outer Join
  58. 58. Open SQL – Outer JoinREPORT 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
  59. 59. Exercise List customers name who do not buy any product from company
  60. 60. Sub QueryREPORT 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.
  61. 61. Internal Table
  62. 62. Data Objects in ABAPMemory Space Variable Structure Table Structure Internal Table Constants <Field-symbols>
  63. 63. INTERNAL TABLE Flight (Structure) Carrid Connid Date PriceInternal Table Flight (Internal Table) Header Line Carrid Connid Date Price
  64. 64. StructureData: 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.
  65. 65. INTERNAL TABLEData: 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.
  66. 66. USING ABAP DICTIONARY STRUCTUREData: begin of tab occurs 0. Include structure sflight.Data end of tab.
  67. 67. INTERNAL TABLE USING LIKEData tab LIKE sflight OCCURS 0 WITH HEADER LINE.
  68. 68. FILLING INTERNAL TABLE (APPEND)Tables sflight.Data flight like sflight occurs 0 with header line.Select * from sflight. Move sflight to flight. Append flight.Endselect.
  69. 69. Standard Key of Internal Table tabData: begin of tab occurs 0, f1 f2 f3 f4 f1 type C, f2 type I, f3 type N, f4 type P, end of tab.
  70. 70. Reading Data From Internal TableData 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.
  71. 71. Access Database Without Internal Table
  72. 72. Access Database Using Internal Table
  73. 73. Reading Data From Internal TableData: 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.
  74. 74. Exercise I : Change Using Internal Table
  75. 75. SORTING INTERNAL TABLE (SORT)Sort flight.Sort flight by price fldate.Sort flight by price ascending fldate descending.
  76. 76. SORTING INTERNAL TABLEData 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.
  77. 77. 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.
  78. 78. Internal Table Template Condition...loop at tab where name cp ‘+r*’....
  79. 79. 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.
  80. 80. Reading Single Record using Index ...Read table flight index 3.If sy-subrc = 0. write: / flight-carrid, flight-connid.Endif.
  81. 81. CHANGING INTERNAL TABLE ... Delete flight index 5. Delete flight where carrid = ‘LH’. flight-carrid = ‘XX’. flight-price = 100. … Insert flight index 1.
  82. 82. DELETING INTERNAL TABLEDATA flight LIKE sflight occurs 0 with header line. Clear flight. Refresh flight. Free flight.
  83. 83. Total Record of Internal TableData: 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.
  84. 84. Exercise I
  85. 85. Internal Table ProcessingData tab like spfli occurs 0 with Header line.…Select * from spfli appending table tab where carrid = ‘LH’.
  86. 86. SELECT … INNER JOINREPORT 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.
  87. 87. Inner Join into Internal TableREPORT 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.
  88. 88. Internal Table Without Header LineDATA tab LIKE customers OCCURS 0.DATA wa LIKE customers.…LOOP AT tab INTO wa. WRITE: / wa-id, wa-name.ENDLOOP.
  89. 89. Internal Table DeclarationDATA tab TYPE TABLE OF customers.DATA wa LIKE LINE OF customers.…
  90. 90. ABAP Practice
  91. 91. Database Table Processing  INSERT  UPDATE Database  MODIFY  DELETE
  92. 92. Insert (Table)Tables customers.customers-id = ‘999’.customers-name = ‘Test’.Insert customers.if sy-subrc <> 0. write: / ‘Data Already Exists’.endif.
  93. 93. Update StatementTables 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.
  94. 94. Update StatementData 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.
  95. 95. Modify StatementTables customers.customers-id = ‘1’.customers-name = ‘Test No 1’.Modify customers.
  96. 96. Deleting Database Table EntriesTables customers.customers-id = ‘1’.Delete customers.Delete customers From Table delcustomers.Delete From customers Where city =
  97. 97. Exercise II
  98. 98. Exercise II1. ห้ามใช้ SELECT * 2. ใช้ Internal Table usr02-bname usr02-trdat usr02-ltime
  99. 99. Exercise III
  100. 100. Tables Relationship for User MasterUSR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER Tables Relationship USR02 BNAME USR21 PERSNUMBER ADDRNUMBER ADCP
  101. 101. Exercise III : User Master usr02-trdatusr02-bname adcp-tel_number ใช้ Internal Table

×