Presentation is loading. Please wait.

Presentation is loading. Please wait.

ABAP Chapter 3 Open SQL Internal Table. SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI.

Similar presentations


Presentation on theme: "ABAP Chapter 3 Open SQL Internal Table. SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI."— Presentation transcript:

1 ABAP Chapter 3 Open SQL Internal Table

2 SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI

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

4 Database Server Application Server Dispatcher Request Queue DDDD … SAP Buffer Program Table … 1 3 4 5 68 9 10 Report zpsm1. Tables customers. Select single * from customers where id = 1. Write: / customers-name. Execute ABAP stateme nt 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 SAP System : Dialog Processing

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

6 Open SQL SELECT... INSERT... UPDATE... DELETE...

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 Example Tables in DB carridconnidcityfromcitytodistance LH0400LANY100 LH0402BKNY540 SQ0110SQBK250 id namecity 1JohnNew York 2PeterSingapore 3DavidLondon customers spfli

9 Example Tables in DB carridconnidfldateprice LH040020010101150 LH040020010110145 LH040020010228130 SQ01102001022675 sflight

10 Select Overview Select Which Columns? From Which Table? Into Where to place? Where Which Lines?

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

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

13 Dialog WP TaskHandler DYNPRO Processor ABAP Processor Database Local Memory Memory Space DB Interface Lis t b uffer Result Set

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 Select … Into Table Structure Tables spfli. Seclect * from spfli into spfli. write: /spfli-carrid, spfli-connid, spfli-cityfrom, spfli-cityto. endselect. ifsy-subrc <> 0. write: / ‘No Data’. endif.

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

17 Exercise I customers-id customers-name customers-city

18 SELECT with WHERE Clause

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

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

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

22 Select Single Record

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

24 Select Column List

25 Select * : Example SELECT *

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

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

28 Select Column : Example I

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

30 Select Column : Example II

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 Select Statement : Special Topics

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

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

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

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 SELECT … GROUP BY... Data: carrid like sflight-carrid, mindat Type P Decimals 2, maxdat Type P Decimals 2. Select carrid Min( price ) Max( price ) Into (carrid, mindat, maxdat) From sflight Group by carrid. Write: / carrid, mindat, maxdat. Endselect. อยากทราบว่า ในแต่ละสายการบิน มีราคาตั๋วต่ำสุดและสูงสุดเท่าไร carridconnidfldatePrice LH040020010101150 LH040020010110145 LH040020010228130 SQ01102001022675 sflight

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

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

40 Exercise II usr02-ltime usr02-trdat usr02-bname ห้ามใช้ SELECT *

41 ABAP : Inner Join

42 Tables Join carridconnidcityfromcitytodistance LH0400NYBK100 LH0402BKNY540 SQ0110SQBK250 carridconnidfldate price LH040020010101150 LH040020010110145 LH040020010228130 SQ01102001022675 sflight spfli

43 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’ เท่านั้น

44 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’;

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

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

47 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, 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 Open SQL – Inner Join Tables: A,B. Select A~a B~b B~c into (A-a,B-b,B-c) from A inner join B on A~b = B~b. Write: / A-a,B-b,B-c. Endselect. ab a1b1 a2b2 Table : A bc b1c1 b2c2 b3c3 Table : B A-a B-b B-c

49 Open SQL – Inner Join ab a1b1 a2b2 Table : A bc b1c1 b2c2 b3c3 Table : B A~aB~bB~c a1b1c1 a2b2c2 Single Result Table(Result set) Select … inner join.. Endselect. Database Server Application Server 1 2

50 Open SQL – Alias Table Name Tables: spfli,sflight. Select a~carrid a~connid b~fldate a~cityto b~price into (spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price) from spfli as a inner join sflight as b on a~carrid = b~carrid and a~connid = b~connid where a~carrid = ‘LH’. Write: / spfli-carrid, spfli-connid, sflight-fldate, spfli-cityto, sflight-price. Endselect.

51 Inner Join/Outer Join Example idnamecitytel 1JohnNew York111111 2PeterLondon222222 3DavidSingapore432555 4MichealBangkok234111 p_id prod_nameon_hand A1Pen100 A2Pencil125 B1Ruler80 X1Tape120 Y1CD99 cust_idprod_idsale_dateqtysale_id 1A1200203181001 1A2200203185001 3X1200203219002 sale_idname 01Somchai 02Pipop ZPRODUCTS ZSALES ZSALEREPS ZCUSTOMERS

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

53 Open SQL – Inner Join > 2 Tables 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. ab …… Table : A bc …... … …… Table : B xy …... Table : C A-a B-c C-y

54 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,ZSALES-QTY. ENDSELECT.

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 Exercise : User Master USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER Tables Relationship

57 ABAP : Outer Join

58 Open SQL – Outer Join REPORT ZOUTERJOIN. TABLES: ZCUSTOMERS,ZSALES. SELECT A~NAME B~PROD_ID INTO (ZCUSTOMERS-NAME,ZSALES-PROD_ID) FROM ZCUSTOMERS AS A LEFT OUTER JOIN ZSALES AS B ON A~ID = B~CUST_ID. WRITE: / ZCUSTOMERS-NAME,ZSALES-PROD_ID. ENDSELECT. A~NAMEB~PROD_ID JohnA1 JohnA2 Peter DavidX1 Micheal Single Result Table

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

60 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. ลูกค้าชื่ออะไรที่ไม่ได้ซื้อ สินค้าจากเรา มีใครบ้าง

61 Internal Table

62 Data Objects in ABAP Memory Space Structure Table Structure Internal Table Variable Constants

63 INTERNAL TABLE Flight (Structure) CarridConnidDatePrice Internal Table Flight (Internal Table) CarridConnidDatePrice Header Line

64 Structure Data:Begin of flight, carridlike sflight-carrid, connidlike sflight-connid, datelike sflight-fldate, pricelike sflight-price. Data:End of flight. flight-carrid = ‘LH’. Write:/ flight-carrid.

65 INTERNAL TABLE Data:begin of tab occurs 10, carridlike sflight-carrid, connidlike sflight-connid, fldatelike sflight-fldate, pricelike sflight-price. Data end of tab.

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

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

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 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. f1f2f3f4 tab

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

71 Access Database Without Internal Table

72 Access Database Using Internal Table

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

74 Exercise I : Change Using Internal Table

75 SORTING INTERNAL TABLE (SORT) Sortflight. Sortflight byprice fldate. Sortflight byprice ascending fldate descending.

76 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. SORTING INTERNAL TABLE

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 Internal Table Template Condition... loop at tab where name cp ‘ +r* ’....

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 Reading Single Record using Index... Read table flight index 3. If sy-subrc = 0. write: / flight-carrid, flight-connid. Endif.

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

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

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

84 Exercise I

85 Internal Table Processing Data tab like spfli occurs 0 with Header line. … Select * from spfli appending table tab where carrid = ‘ LH ’.

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

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

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

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

90 ABAP Practice

91 Database Table Processing  INSERT  UPDATE  MODIFY  DELETE Database

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

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

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

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

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

97 Exercise II

98 usr02-ltime usr02-trdat usr02-bname 1. ห้ามใช้ SELECT * 2. ใช้ Internal Table

99 Exercise III

100 Tables Relationship for User Master USR02-BNAME USR02-TRDAT ADCP-TEL_NUMBER USR02 USR21 ADCP BNAME PERSNUMBER ADDRNUMBER Tables Relationship

101 Exercise III : User Master usr02-bname usr02-trdat adcp-tel_number ใช้ Internal Table


Download ppt "ABAP Chapter 3 Open SQL Internal Table. SAP System : 3 Tier Client/Server DB Server SAP Application Server SAP GUI Presentation Server SAP GUI."

Similar presentations


Ads by Google