Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)

Similar presentations


Presentation on theme: "ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)"— Presentation transcript:

1 ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)

2 What is ABAP ? ABAP = Advanced Business Application Programming Similarities with Cobol and Pascal Established in 1980 Since 1998 object oriented ABAP objects established Fully compatible to older versions Multilanguage support Embedded SQL statements Platform independent Database independent Reusability of code fragments

3 Machine code Assembler Fortran Cobol LISP Smalltalk Pascal C++ Java ABAP ABAP Objects PL1 1950 1954 1968 1980 1992 …. Source: Following SAP AG Historical view on ABAP

4 Presentation layer Application layer Database layer SAPGui Application server ABAP Source code ABAP Compilation Database server Calls program for the first time Program has to be compiled Short message about compiling Compiling Return compilation Runs program Source: Following SAP AG SAPGui Application server Database server SAPGui Application server Database server Compiling ABAP

5  Every tool can be accessed in Object Navigator Development tools in SAP

6 Tools (1) Menu path Tools ABAP workbench Development ABAP Editor Transaction code: SE38 Run, view, edit, activate, check ABAP code Integrated into Object Navigator

7 Tools (2) Function Builder: Menu path Tools ABAP workbench Development Function Builder (SE37) Create and edit function modules / groups Class Builder: Menu path Tools ABAP workbench Development Class Builder (SE24) Create and edit new global classes

8 Tools (3) Screen Painter: Menu path Tools ABAP workbench Development User Interface Screen Painter (SE51) Create and edit DynPro’s Separate programs which is only installed when using SAPGui for Windows Menu Painter: Menu path Tools ABAP workbench Development User Interface Menu Painter (SE41) Create, edit menu’s, header's and toolbar’s in ABAP programs

9 Tools (4) Debugger: Execution of ABAP program step-by-step Variable values during runtime Breakpoint: program execution will be paused when getting to breakpoint Watchpoint: program execution is paused only when variable has defined value Debugging can be activated by suffix /h Test program: Program Test Debugging

10 Tools (5) Dictionary : SE11

11 ABAP System Fields : Structure SYST (SE11)

12 Tools (6) Object Navigator integrates all development tools (SE80) Navigation tree (independent) Browsers (independent) Toolbar (context sensitive) Working space (context sensitive)

13 editor Transaction Code : SE38

14 Editor (1)

15 Editor (2)

16 Editor (3)

17 Editor (4)

18 Editor (5)

19 Editor (6) Ctrl + F2 = Check Syntax F8 = Direct Processing Ctrl + S = Save Ctrl + F3 = Activate Ctrl + F1 = Change Display

20 Editor (7) Long running programs may decrease the system performance Sometimes infinite loops Termination of long running dialog program: Click on SAP icon Choose ‘Stop Transaction’

21 Debugging

22

23 Debugging (2) Single step F5 Execute F6 Return F7 Run F8

24 Structure of Language Each statement must end with a period DATA tmp TYPE I. WRITE ‘Hello World’. WRITE ‘OK’.

25 Structure of Language (2) Successive statements that have the same string segment can be combined to form a single chained statement To do so, you specify the identical starting segment once and conclude it with a colon (:), the remaining segments are then listed, separated by commas (,) and concluded with a period (.)

26 Structure of Language (3) Comments Full Line Comment (*) Partial Line Comment (“) ABAP command is not case sensitive SAP Program Name must use Y or Z at thefirst character.

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

28 Variable Variables can be declared at any point in a program Variables can be up to 30 characters in length REPORT ZTEST. DATA firstname TYPE STRING. firstname = ‘John’.

29 Predefined ABAP Data Types TypeDescriptionInitial Value C D F I N P T X String xstring Character Date Floating Point Integer Numeric Text Packed Decimal Time Hexadecimal Variable-length Variable-length Hexadecimal Space ‘00000000’ 0.0 0 ‘0’ 0 ‘000000’ ’00’ Space Blank string Length 1 – 65535 8 characters 8 bytes 4 bytes 1 – 65535 1 – 16 bytes 6 characters 1 – 65535 Variable

30 Defining Variable with DATA Statement * Syntax DATA var[(length)] [Type type] [Decimals number]. DATA var LIKE Table-Field [VALUE initial value].

31 Defining Variable with DATA Statement * Data Declaration DATA: tmp(10) TYPE C, tmp1 TYPE I, tmp2(8) TYPE P DECIMALS 2 VALUE ‘1.50’. DATA: tmp3(5) TYPE N, tmp4.

32 Defining Variable with DATA Statement DATA customerno TYPE customers-id. DATA matnr TYPE mara-matnr. * Data Declaration DATA customerno LIKE customers-id. DATA matnr LIKE mara-matnr.

33 Variable Data Type C,N and X length between 1 – 65535 (Default 1) Data Type P length between 1 – 16 (Default 8) and decimals length between 0 – 31 Data Type I value between – 2 31 to 2 31 – 1 or –2,147,483,648 to 2,147,483,647

34 Data Declaration & Value Assignment DATA tmp(5) TYPE C value 'Hello World'. DATA tmp2 TYPE P DECIMALS 4. DATA tmp3 TYPE I. : tmp2 = '12345.45345', tmp3 = 30. write tmp. write: / 'tmp2', tmp2, tmp2 DECIMALS 2. Write: / 'tmp3 ',tmp3. data tmp4(5) type N. tmp4 = 'Xca9yy23K6'. Write / tmp4.

35 TYPES tname(10) TYPE c. DATA: fname TYPE tname, lname TYPE tname. DATA: tmp1 TYPE i, tmp2 TYPE i. tmp1 = tmp2 = 10. : fname = 'uraiporn', lname = 'jettanachai'. write : / 'firstname => ', fname. write : 'lastname =>',lname. write : /'tmp1 => ', tmp1,'tmp2 => ',tmp2. Data Declaration & Value Assignment

36 DATE * Fixed Length 8 * Include Representation ‘YYYYMMDD’ DATA today TYPE D. today = sy-datum. WRITE today. today = ‘19991231’. WRITE today.

37 TIME * Fixed Length 6 * Format ‘HHMMSS’ DATA times TYPE T. times = sy-uzeit. WRITE times.

38 xx * Value assignment DATA: name1(30), first_num TYPE I, next_num TYPE I. MOVE ‘XXXX’ TO name1. MOVE 5 TO first_num. COMPUTE next_num = first_num + 5. name1 = ‘SAP’. ADD 1 TO next_num.

39 Structure * Syntax DATA BEGIN OF. DATA field1. DATA field2. … DATA END OF.

40 * Syntax DATA BEGIN OF wa. DATA id LIKE customers-id. DATA name LIKE customers-name. DATA city LIKE customers-city. DATA END OF wa. MOVE 9 TO wa-id. WRITE wa-id. idcity wa name Structure

41 Defining Structure (Include Structure) * Include Structure DATA BEGIN OF wa. INCLUDE STRUCTURE customers. DATA tel(7). DATA END OF wa. wa-id = 10. wa-tel = 1234567. write : / wa-id, wa-tel. idcity wa name tel

42 Defining Structure * LIKE option DATA wa LIKE customers. wa-id = 1. wa-name = ‘John’. WRITE: wa-id, wa-name.

43 Constants * Constant variable CONSTANTS max_no TYPE I VALUE 999. DATA counter TYPE I VALUE max_no. WRITE: max_no, counter.

44 Constants Using Example * Constant variable CONSTANTS ctext(11)TYPE C VALUE ‘Hello World’. WRITE ctext.

45 System Fields The system fields (structure syst) are filled by the runtime environment. You can use them to query the system status in an ABAP program You should access them only for reading sy-datum = Current date of application server sy-uzeit = Current time of application server sy-datlo = Current date of SAP GUI sy-timlo = Current time of SAP GUI sy-mandt = Current client logon sy-subrc = Return value of ABAP statement

46 DATA wa LIKE customers. DATA vender LIKE customers. wa-id = ‘1234’. wa-name = ‘Test#1’. MOVE wa TO vender. WRITE: vender-id, vender-name. MOVE Statement “ vender = wa.

47 MOVE-CORRESPONDING Statement DATA: begin of wa1, f1,f2,f4, end of wa1. DATA: begin of wa2, f2,f1,f3, end of wa2. wa1-f1 = 'A'. wa1-f2 = 'B'. wa1-f4 = 'C'. wa2-f2 = 'D'. wa2-f1 = 'E'. wa2-f3 = 'F'. MOVE-CORRESPONDING wa1 TO wa2. WRITE : wa2-f2,wa2-f3.

48 Field-symbols Data: name(4) Value 'Test', num Type I Value 10, today Type D Value '19980429'. Field-symbols. Assign name To. Write. Assign num To. Write. Assign today To. Write.

49 Field-symbols : UNASSIGN data: name(4) Value ‘Test’, field-symbols. assign name To. write. unassign. write.

50 CLEAR Statement CLEAR. DATA tmp type i value 9. tmp = 10. CLEAR tmp.

51 CLEAR Structure DATA wa like customers. … CLEAR wa.

52 Report Statement * Syntax REPORT [NO STANDARD PAGE HEADING] [LINE-SIZE no of columns] [LINE-COUNT no of lines[(no of footer)]]. REPORT ztest1 NO STANDARD PAGE HEADING. REPORT ztest LINE-SIZE 132 LINE-COUNT 65(2). sy-linsz

53 Text Element : Title&Headers  Text Element  Title and Headers List Header Column Header This is test program by uraiporn Column #1 #2 Report ztest. Write ‘ Hello World ’.

54 Text Symbol  Text Element  Text Symbols  Text Symbol Text Text 2 Text 1 Report ztest. Write: Text-001, Text-002. 001 002

55 write: / Text-001. Text Symbol

56 Creating Lists ABAP statement that create list WRITE SKIP ULINE The complete report list will appears automatically at the end of the processing block

57 List Buffer Dialog WP TaskHandler Dynpro Processor ABAP Processor Local Memory Memory Space DB Interface List Buffer WRITE,SKIP,ULINE

58 WRITE Statement * Write data WRITE ‘Hello World’. WRITE: ‘OK’, ‘Test data’. WRITE: /15(10) ‘ABCDEFGHIJKLMNOPQ’. WRITE /20 ‘Test data’. WRITE / text-001.

59 Breaking to a New Line * Write data WRITE: / ‘First Line’, ‘Data 1’, / ‘Second Line’, ‘Data 2’, /(20) ‘Third Line’, ‘Data 3’, /35 ‘Fourth Line’, ‘Data 4’.

60 Column Position DATA colno type I value 10. write: /5 ‘Hello’, at colno ‘World’. write: at /colno ‘OK’.

61 Options of the WRITE Statement * Write Syntax WRITE var [NO-ZERO] [NO-SIGN] [NO-GROUPING] [NO-GAP] [DECIMALS no of decimals]

62 Suppressing Blanks(NO- ZERO) * No Zero DATA: number(10) TYPE N VALUE 23. WRITE: number, number NO-ZERO.

63 Suppressing Number(+ / - ) Sign * No Sign DATA: v_integer TYPE I VALUE -1. WRITE: v_integer, v_integer NO-SIGN.

64 NO-GROUPING * No grouping DATA: v_integer TYPE I VALUE 120000. WRITE: v_integer, v_integer NO-GROUPING.

65 NO-GAP * No gap WRITE: ‘Hello’ NO-GAP, ‘World’.

66 Formatting Options * Format options of WRITE statement * LEFT-JUSTIFIED for Integer data * RIGHT-JUSTIFIED for Character data * CENTERED Data tmp1(20) value ‘test’. WRITE: tmp1 CENTERED.

67 Inserting Blank Lines(SKIP) *Skip Statement SKIP. WRITE: ‘Hello World’, sy-linno. SKIP. WRITE: ‘Test 1’. SKIP 5. WRITE: ‘Test 2’. SKIP TO LINE 20. WRITE ‘This is line 20’.

68 Inserting Horizontal Lines(ULINE) * Uline WRITE: ‘Hello World’. WRITE: /5(35) sy-uline, sy-vline. ULINE /5(35). ULINE. WRITE: / ‘This is an underline’. ULINE /(18).

69 Frame uline: /(45). write: /1 sy-vline, 'Column #1', 15 sy-vline, 'Column #2', 30 sy-vline, 'Column #3', 45 sy-vline. uline: /(45).

70 Exercise I sy-datum sy-uzeit

71 FORMAT Statement FORMAT [INTENSIFIED] [INTENSIFIED OFF] [COLOR ] [COLOR OFF] [HOTSPOT ON] [HOTSPOT OFF] [RESET]

72 FORMAT COLOR FORMAT COLOR col_heading. “color 1 FORMAT COLOR col_normal. “color 2 FORMAT COLOR col_total. “color 3 FORMAT COLOR col_key. “color 4 FORMAT COLOR col_positive. “color 5 FORMAT COLOR col_negative. “color 6 FORMAT COLOR col_group. “color 7 FORMAT COLOR col_background. “color off

73 FORMAT COLOR (2) format color col_positive intensified on. write : / ' SAP '. format color col_positive intensified off. write : / ' SAP '. FORMAT HOTSPOT ON. WRITE: / 'Hello ABAP', 'Hi!'. FORMAT HOTSPOT OFF. format color 3. write : / ' SAP '. format color off. write : / ' SAP '.

74 Flow Control in ABAP  Branching ==> IF, CASE.  Looping ==> DO, WHILE.

75 IF Statement IF. ELSEIF. ELSEIF. ELSE. ENDIF.

76 IF Statement IF sy-mandt = ‘100’. WRITE: / ‘This is Production Client’. ELSEIF sy-mandt = ‘800’. WRITE: / ‘This is Development Client’. ELSE. WRITE: / ‘This is Test Client’. ENDIF.

77 Invalid Date DATA: today TYPE D. today = ‘20061321’. today = today + 0. if today is initial. write: / ‘invalid date’. else. write: / today. endif.

78 CASE Statement CASE. WHEN. WHEN.... WHEN OTHERS. ENDCASE.

79 CASE Statement CASE sy-mandt. WHEN ‘100’. WRITE: / ‘Production Client’. WHEN ‘800’. WRITE: / ‘Development Client’. WHEN OTHERS. WRITE: / ‘Test Client’. ENDCASE.

80 DO Statement DO. WRITE sy-index. IF sy-index = 3. EXIT. ENDIF. WRITE: sy-index. ENDDO.

81 CONTINUE Statement DO 5 TIMES. IF sy-index = 3. CONTINUE. ENDIF. WRITE: sy-index. ENDDO.

82 CHECK Statement DO 4 TIMES. CHECK sy-index BETWEEN 2 AND 3. WRITE: sy-index. ENDDO.

83 WHILE Statement DATA: count TYPE I value 1. WHILE count <> 4. WRITE: sy-index. count = count + 1. ENDWHILE.

84 Logical Expressions Operator Meaning =,EQ EQUAL <>,NE NOT EQUAL <, LT LESS THAN <=, LE LESS THAN OR EQUAL >, GT GREATER THAN >=, GE GREATER THAN OR EQUAL BETWEEN … AND …. check whether the value of a field lies within a particular range. IS INITIAL check whether the value of a field is initial: IS ASSIGNED check a field is assigned to a field symbol

85 Operators/FunctionMeaning + Addition - Subtraction * Multiplication / Division ** Powers DIV Integer division MOD Remainder of integer division SQRT Square root. Arithmetic Expressions

86 Character String Operator CO (contains only) CN (contains not only) CA (contains any) NA (contains not any) CS (contains string) NS(contains no string) CP (convers pattern) NP (no pattern) Case-sensitive Not case-sensitive

87 Character String Operator (2) if 'ABCE' cN 'ABCDE'. write : / 'true'. else. write : / 'false'. endif.

88 if ‘AABB’ co ‘AB’. if ‘ABCD’ co ‘ABC’. if ‘AXCZ’ ca ‘AB’. if ‘ABCD’ ca ‘XYZ’. if ‘ABCD’ cp ‘+B*’. Character String Operator (3) T F T F T

89 Character String Operator (4) 'BD ' CO 'ABCD ‘ 'BD ' CO 'ABCDE' 'ABC12' CN 'ABCD ‘ 'ABABC' CN 'ABCD ' 'ABcde' CA 'Bd ' 'ABcde' CA 'bD ' 'ABAB ' NA 'AB ' 'ababa' NA 'AB ' T F T F T F F T

90 Character String Operator (5) 'ABcde' CS 'bC ' 'ABcde' CS 'ce ‘ 'ABcde' NS 'bC ' 'ABcde' NS 'ce ' 'ABcde' CP '*b*' 'ABcde' CP '*#b*' 'ABcde' NP '*b*' 'ABcde' NP '*#b*‘ T F F T T F F T

91 * Substrings with offsets DATA tmp(10) VALUE ‘ABCDEFGHIJ’. DATA tmp1(2). WRITE: tmp+3(7), tmp+1(4), tmp+0(8), tmp+7(3). MOVE tmp+4(2) TO tmp1. WRITE: tmp1 Manipulating Character Data DEFGHIJ BCDE ABCDEFGH HIJ EF

92 SHIFT Statement * SHIFT Statement DATA tmp(8) VALUE ‘12345’. SHIFT tmp. SHIFT tmp BY 2 PLACES. SHIFT tmp BY 2 PLACES CIRCULAR. SHIFT tmp UP TO ‘3’. SHIFT tmp UP TO ‘3’ RIGHT. SHIFT tmp UP TO ‘3’ RIGHT CIRCULAR. SHIFT tmp RIGHT DELETING TRAILING SPACE. SHIFT tmp LEFT DELETING LEADING SPACE. 2345____ 345_____ 345___12 _____123 345_____ 45___123 ___12345 12345___ กว้าง 8

93 * Shift DATA name(30) VALUE ‘Alexander Bill Charles’. SHIFT name UP TO ‘Bill’. WRITE: / name. Bill Charles SHIFT Statement (2)

94 * Search DATA tmp(5) VALUE ‘ABCDE’. SEARCH tmp FOR ‘C’. DATA tmp1(10) VALUE ‘Till Bill’. SEARCH tmp1 FOR ‘Bill’. IF SY-SUBRC = 0. WRITE: / SY-FDPOS. ENDIF. SEARCH(Non Case- sensitive) 5

95 TRANSLATE * Translate DATA tmp(5) VALUE ‘abc ‘. TRANSLATE tmp TO UPPER CASE. TRANSLATE tmp TO LOWER CASE. TRANSLATE tmp USING ‘ 0’. TRANSLATE tmp USING ‘ 0aA’.

96 REPLACE * Replace DATA tmp(20) VALUE ‘I was a boy’. REPLACE ‘was’ WITH ‘am’ INTO tmp. IF sy-subrc = 0. write ‘Replace OK’. ELSE. write ‘Cannot find data to be replaced’. ENDIF.

97 * Condense DATA: tmp(20) VALUE ‘I am a boy’. CONDENSE tmp. CONDENSE tmp NO-GAPS. Removing Spaces(CONDENSE) I am a boy

98 * Concatenate DATA: tmp1(2) VALUE ‘AB’, tmp2(3) VALUE ‘CDE’, tmp3(10). CONCATENATE tmp1 tmp2 INTO tmp3. CONCATENATE tmp1 tmp2 INTO tmp3 SEPARATED BY ‘ ‘. Concatenation String(CONCATENATE) ABCDE

99 * Split DATA: name(30) value ‘David, John, Peter’, one(10), two(10), three(30). split name at ‘,’ into one two three. Split David_John_Peter

100 Working with Date Variables * Date DATA today TYPE D. today = sy-datum. WRITE: today, ‘Year :’, today+0(4), ‘Month :’, today+4(2), ‘Day :’, today+6(2).

101 WRITE … TO … DATA: today TYPE D, tmp(10). today = sy-datum. tmp = today. WRITE tmp. WRITE today TO tmp. WRITE tmp. CLEAR today. WRITE today NO-ZERO TO tmp. WRITE tmp.

102 Built-in Functions ABAP provides a lot of built-in functions A Built-in function calculates a return value from an argument abs = Absolute value of argument sign = +/- sign of argument sqrt = Square root strlen = Number of characters in arg

103 STRLEN Built-in Function DATA: tmp(20) VALUE ‘Test String’, count TYPE I. count = strlen( tmp ). WRITE count.

104 STRLEN Built-in Function Example DATA: tmp(20) VALUE ‘xxax’, cntlen TYPE I. cntlen = strlen( tmp ). cntlen = cntlen – 2. if tmp+cntlen(1) = ‘a’. “cntlen >= 0 write: / ‘OK’. endif.

105 WRITE ‘ *If we need the word like this I’m a boy WRITE: ‘I’’m a boy’.

106 * Display Icon or Symbol in List INCLUDE. WRITE: / ‘Phone :’, SYM_PHONE AS SYMBOL. WRITE: / ‘Alarm :’, ICON_ALARM AS ICON. WRITE: / ‘Green Light :’, ICON_GREEN_LIGHT AS ICON HOTSPOT. FORMAT HOTSPOT ON. WRITE: / ‘Hello ABAP’, ’Hi!’. FORMAT HOTSPOT OFF. Symbols and Icons

107 Pattern Calling big functions with a lot of importing and exporting parameters may be fault-prone Procedure: Navigate to the position where the function should be called Click the ‘Pattern’ button Choose the ABAP instruction Choose parameters

108 Pattern กดปุ่ม Continue เลือกคำสั่ง write

109 เลือก pattern ที่ต้องการกดปุ่ม Copy

110 Include Program You can create a program with program type include program in the program attribute Include program do not have to have an introductory statement During the syntax check and during program generation by the ABAP compiler, the INCLUDE statement is replaced by the source text of the defined include program

111 Include Program (2) REPORT ztest1. Write :/ ‘test1’. INCLUDE zinclude1. Write :/ ‘test2’. Ztest1 Write : / ‘test3’. Write : / ‘test4’. Write : / ‘test5’. Include Program : Zinclude1 Include Program : 1. Type => Include Program 2. Activate program

112 Pretty Printer Pretty Printer formats your source code Settings can be adjusted via Utilities Settings ABAP Editor tab Pretty Printer tab Pretty Printer supports indent, keyword conversion upper- /lowercase

113 Exercise II


Download ppt "ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)"

Similar presentations


Ads by Google