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

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

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

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

ITEC3612

Enterprise Architecture and

Resource Planning (Lab)

ABAP Programming (1)

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

What is ABAP ?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

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

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

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

Presentationlayer

Applicationlayer

Databaselayer

SAPGui

Applicationserver

ABAPSource

code

ABAPCompilation

Databaseserver

Calls program for the first time

Program has to be compiled

Short messageabout compiling

Compiling

Return compilation

Runs program

Source: Following SAP AG

SAPGui

Applicationserver

Databaseserver

SAPGui

Applicationserver

Databaseserver

Compiling ABAP

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

Every tool can be accessed in Object Navigator

Development tools in SAP

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

Tools (1)

Menu path Tools • ABAP workbench • Development • ABAP Editor

Transaction code: SE38 Run, view, edit, activate, check ABAP

code Integrated into Object Navigator

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

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

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

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

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

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

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

Tools (5)

Dictionary : SE11

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

ABAP System Fields : Structure SYST (SE11)

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

Tools (6) Object Navigator integrates all

development tools (SE80)

Navigation tree(independent)

Browsers(independent)

Toolbar (context sensitive)

Working space (context sensitive)

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

editor

Transaction Code : SE38

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

Editor (1)

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

Editor (2)

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

Editor (3)

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

Editor (4)

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

Editor (5)

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

Editor (6)

Ctrl + F2 = Check Syntax F8 = Direct Processing Ctrl + S = Save Ctrl + F3 = Activate Ctrl + F1 = Change <->

Display

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

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’

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

Debugging

Page 22: ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)
Page 23: ITEC3612 Enterprise Architecture and Resource Planning (Lab) ABAP Programming (1)

Debugging (2)

Single step F5 Execute F6 Return F7 Run F8

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

Structure of Language Each statement must end with a period

DATA tmp TYPE I.WRITE ‘Hello World’. WRITE ‘OK’.

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

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 (.)

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

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.

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

Data Objects in ABAP

Memory Space

Structure

Table Structure Internal Table

Variable

Constants <Field-symbols>

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

Variable

Variables can be declared at any point in a program

Variables can be up to 30 characters in lengthREPORT ZTEST.

DATA firstname TYPE STRING.firstname = ‘John’.

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

Predefined ABAP Data Types

Type Description Initial 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’

00

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

Variable

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

Defining Variable with DATA Statement

* SyntaxDATA var[(length)] [Type type] [Decimals number].

DATA var LIKE Table-Field [VALUE initial value].

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

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.

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

Defining Variable with DATA Statement

DATA customerno TYPE customers-id.DATA matnr TYPE mara-matnr.

* Data DeclarationDATA customerno LIKE customers-id.DATA matnr LIKE mara-matnr.

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

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 – 231 to 231 – 1

or –2,147,483,648 to 2,147,483,647

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

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.

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

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

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

DATE

* Fixed Length 8

* Include Representation ‘YYYYMMDD’

DATA today TYPE D.

today = sy-datum.

WRITE today.

today = ‘19991231’.

WRITE today.

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

TIME

* Fixed Length 6

* Format ‘HHMMSS’

DATA times TYPE T.

times = sy-uzeit.

WRITE times.

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

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.

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

Structure

* Syntax

DATA BEGIN OF <structure name>.

DATA field1.

DATA field2.

DATA END OF <structure name>.

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

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

id city

wa

name

Structure

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

Defining Structure (Include Structure)

* Include StructureDATA BEGIN OF wa. INCLUDE STRUCTURE customers.DATA tel(7).DATA END OF wa.

wa-id = 10.wa-tel = 1234567.write : / wa-id , wa-tel.

id city

wa

name tel

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

Defining Structure

* LIKE option

DATA wa LIKE customers.

wa-id = 1.

wa-name = ‘John’.

WRITE: wa-id, wa-name.

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

Constants

* Constant variable

CONSTANTS max_no TYPE I VALUE 999.

DATA counter TYPE I VALUE max_no.

WRITE: max_no, counter.

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

Constants Using Example

* Constant variable

CONSTANTS ctext(11)TYPE C VALUE ‘Hello World’.

WRITE ctext.

WRITE ctext.

WRITE ctext.

WRITE ctext.

WRITE ctext.

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

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

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

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.

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

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 .

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

Field-symbols

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

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

Field-symbols : UNASSIGNField-symbols : UNASSIGN

data: name(4) Value ‘Test’,

field-symbols <temp>.

assign name To <temp>.

write <temp>.

unassign <temp>.

write <temp>.

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

CLEAR StatementCLEAR Statement

CLEAR <data object>.

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

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

CLEAR StructureCLEAR Structure

DATA wa like customers.…CLEAR wa.

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

Report Statement

* Syntax

REPORT <report name>

[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

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

Text Element : Title&Headers

Text Element Title and Headers

List Header

Column Header

This is test program by uraiporn

Column Column #1 #2

Report ztest.

Write ‘Hello World’.

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

Text Symbol

Text Element Text Symbols Text Symbol Text

Text 2

Text 1

Report ztest.

Write: Text-001,

Text-002.

001

002

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

write: / Text-001.

Text Symbol

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

Creating Lists ABAP statement that create list

WRITE SKIP ULINE

The complete report list will appears automatically at the end of the processing block

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

List Buffer

Dialog WP

TaskHandler

Dynpro Processor

ABAP Processor

Local Memory

Memory Space

DB Interface

List BufferWRITE,SKIP,ULINE

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

WRITE StatementWRITE Statement

* Write data

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

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

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

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

Column Position

DATA colno type I value 10.

write: /5 ‘Hello’, at colno ‘World’.

write: at /colno ‘OK’.

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

Options of the WRITE Statement

* Write Syntax

WRITE var [NO-ZERO]

[NO-SIGN]

[NO-GROUPING]

[NO-GAP]

[DECIMALS no of decimals]

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

Suppressing Blanks(NO-ZERO)

* No Zero

DATA: number(10) TYPE N VALUE 23.

WRITE: number, number NO-ZERO.

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

Suppressing Number(+ / -) Sign

* No Sign

DATA: v_integer TYPE I VALUE -1.

WRITE: v_integer, v_integer NO-SIGN.

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

NO-GROUPING

* No grouping

DATA: v_integer TYPE I VALUE 120000.

WRITE: v_integer, v_integer NO-GROUPING.

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

NO-GAP

* No gap

WRITE: ‘Hello’ NO-GAP, ‘World’.

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

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.

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

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

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

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

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

Frame

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

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

Exercise I

sy-datumsy-uzeit

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

FORMAT Statement

FORMAT [INTENSIFIED]

[INTENSIFIED OFF]

[COLOR <color>]

[COLOR OFF]

[HOTSPOT ON]

[HOTSPOT OFF]

[RESET]

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

FORMAT COLOR

FORMAT COLOR col_heading. “color 1 FORMAT COLOR col_normal. 22 FORMAT COLOR col_total. 23 FORMAT COLOR col_key. 24 FORMAT COLOR col_positive. 25 FORMAT COLOR col_negative. 26 FORMAT COLOR col_group. 27 FORMAT COLOR col_background. 222222 222

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

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

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

Flow Control in ABAP

Branching ==> IF, CASE. Looping ==> DO, WHILE.

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

IF Statement

IF <Condition>.

<Statement Block>

ELSEIF <Condition>.

<Statement Block>

ELSEIF <Condition>.

<Statement Block>

ELSE.

<Statement Block>

ENDIF.

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

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.

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

Invalid Date

DATA: today TYPE D.

today = ‘20061321’.

today = today + 0.

if today is initial.

write: / ‘invalid date’.

else.

write: / today.endif.

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

CASE Statement

CASE <field>.

WHEN <value1>.

<Statement Block>

WHEN <value2>.

<Statement Block>

...

WHEN OTHERS.

<Statement Block>

ENDCASE.

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

CASE Statement

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

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

DO Statement

DO.

WRITE sy-index.

IF sy-index = 3.

EXIT.

ENDIF.

WRITE: sy-index.

ENDDO.

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

CONTINUE Statement

DO 5 TIMES.

IF sy-index = 3.

CONTINUE.

ENDIF.

WRITE: sy-index.

ENDDO.

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

CHECK Statement

DO 4 TIMES.

CHECK sy-index BETWEEN 2 AND 3.

WRITE: sy-index.

ENDDO.

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

WHILE Statement

DATA: count TYPE I value 1.

WHILE count <> 4.

WRITE: sy-index.

count = count + 1.

ENDWHILE.

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

Logical ExpressionsOperator Meaning

=,EQ EQUAL<>,NE NOT EQUAL<, LT LESS THAN<=, LE LESS THAN OR EQUAL>, GT GREATER THAN

>=, GEGREATER THAN OR EQUAL

BETWEEN … AND ….check whether the value of a field lies within a particular range.

IS INITIALcheck whether the value of a field is initial:

IS ASSIGNEDcheck a field is assigned to a field symbol

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

Operators/Function Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

** Powers

DIV Integer division

MODRemainder of integer division

SQRT Square root.

Arithmetic Expressions

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

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

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

Character String Operator (2)

if 'ABCE' cN 'ABCDE'. write : / 'true'.else. write : / 'false'.endif.

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

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

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

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

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

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

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

* 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

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

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

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

* Shift

DATA name(30) VALUE ‘Alexander Bill Charles’.

SHIFT name UP TO ‘Bill’.

WRITE: / name.

Bill Charles

SHIFT Statement (2)

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

* SearchDATA 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

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

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

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

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.

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

* CondenseDATA: tmp(20) VALUE ‘I am a boy’.

CONDENSE tmp.

CONDENSE tmp NO-GAPS.

Removing Spaces(CONDENSE)

I am a boy

Iamaboy

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

* ConcatenateDATA: 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

AB CDE

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

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

Split

David _John _Peter

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

Working with Date Variables

* DateDATA today TYPE D.today = sy-datum.WRITE: today, ‘Year :’ , today+0(4), ‘Month :’ , today+4(2), ‘Day :’ , today+6(2).

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

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.

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

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

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

STRLEN Built-in Function

DATA: tmp(20) VALUE ‘Test String’,

count TYPE I.

count = strlen( tmp ).

WRITE count.

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

STRLEN Built-in Function ExampleDATA: tmp(20) VALUE ‘xxax’,

cntlen TYPE I.

cntlen = strlen( tmp ).

cntlen = cntlen – 2.

if tmp+cntlen(1) = ‘a’. “cntlen >= 0

write: / ‘OK’.

endif.

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

WRITE ‘

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

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

* Display Icon or Symbol in List 2< LIST>. : / ‘ :’ , SYM_PHONE AS SYMBOL.

WRITE: / ‘Alarm :’, ICON_ALARM AS ICON. WRITE: / ‘Green Light :’,

ICON_GREEN_LIGHT AS ICON 2 2 2222 2. FORMAT HOTSPOT ON.

WRITE: / ‘Hello ABAP’, ’Hi!’. FORMAT HOTSPOT OFF.

Symbols and Icons Symbols and Icons

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

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

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

Pattern

กดปุ่��ม Continue

เลือกคำ �สั่��ง write

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

เลือก pattern ที่��ต้�องก�รกด

ปุ่��ม Copy

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

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

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

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 Program2. Activate program

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

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

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

Exercise II