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

Post on 14-Dec-2015

220 views 0 download

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

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

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

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

Every tool can be accessed in Object Navigator

Development tools in SAP

Tools (1)

Menu path Tools • ABAP workbench • Development • ABAP Editor

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

code Integrated into Object Navigator

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

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

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

Tools (5)

Dictionary : SE11

ABAP System Fields : Structure SYST (SE11)

Tools (6) Object Navigator integrates all

development tools (SE80)

Navigation tree(independent)

Browsers(independent)

Toolbar (context sensitive)

Working space (context sensitive)

editor

Transaction Code : SE38

Editor (1)

Editor (2)

Editor (3)

Editor (4)

Editor (5)

Editor (6)

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

Display

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’

Debugging

Debugging (2)

Single step F5 Execute F6 Return F7 Run F8

Structure of Language Each statement must end with a period

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

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

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.

Data Objects in ABAP

Memory Space

Structure

Table Structure Internal Table

Variable

Constants <Field-symbols>

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

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

Defining Variable with DATA Statement

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

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

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.

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.

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

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.

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

DATE

* Fixed Length 8

* Include Representation ‘YYYYMMDD’

DATA today TYPE D.

today = sy-datum.

WRITE today.

today = ‘19991231’.

WRITE today.

TIME

* Fixed Length 6

* Format ‘HHMMSS’

DATA times TYPE T.

times = sy-uzeit.

WRITE times.

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.

Structure

* Syntax

DATA BEGIN OF <structure name>.

DATA field1.

DATA field2.

DATA END OF <structure name>.

* 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

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

Defining Structure

* LIKE option

DATA wa LIKE customers.

wa-id = 1.

wa-name = ‘John’.

WRITE: wa-id, wa-name.

Constants

* Constant variable

CONSTANTS max_no TYPE I VALUE 999.

DATA counter TYPE I VALUE max_no.

WRITE: max_no, counter.

Constants Using Example

* Constant variable

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

WRITE ctext.

WRITE ctext.

WRITE ctext.

WRITE ctext.

WRITE ctext.

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

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.

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 .

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

Field-symbols : UNASSIGNField-symbols : UNASSIGN

data: name(4) Value ‘Test’,

field-symbols <temp>.

assign name To <temp>.

write <temp>.

unassign <temp>.

write <temp>.

CLEAR StatementCLEAR Statement

CLEAR <data object>.

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

CLEAR StructureCLEAR Structure

DATA wa like customers.…CLEAR wa.

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

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

Text Symbol

Text Element Text Symbols Text Symbol Text

Text 2

Text 1

Report ztest.

Write: Text-001,

Text-002.

001

002

write: / Text-001.

Text Symbol

Creating Lists ABAP statement that create list

WRITE SKIP ULINE

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

List Buffer

Dialog WP

TaskHandler

Dynpro Processor

ABAP Processor

Local Memory

Memory Space

DB Interface

List BufferWRITE,SKIP,ULINE

WRITE StatementWRITE Statement

* Write data

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

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

Column Position

DATA colno type I value 10.

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

write: at /colno ‘OK’.

Options of the WRITE Statement

* Write Syntax

WRITE var [NO-ZERO]

[NO-SIGN]

[NO-GROUPING]

[NO-GAP]

[DECIMALS no of decimals]

Suppressing Blanks(NO-ZERO)

* No Zero

DATA: number(10) TYPE N VALUE 23.

WRITE: number, number NO-ZERO.

Suppressing Number(+ / -) Sign

* No Sign

DATA: v_integer TYPE I VALUE -1.

WRITE: v_integer, v_integer NO-SIGN.

NO-GROUPING

* No grouping

DATA: v_integer TYPE I VALUE 120000.

WRITE: v_integer, v_integer NO-GROUPING.

NO-GAP

* No gap

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

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.

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

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

Frame

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

Exercise I

sy-datumsy-uzeit

FORMAT Statement

FORMAT [INTENSIFIED]

[INTENSIFIED OFF]

[COLOR <color>]

[COLOR OFF]

[HOTSPOT ON]

[HOTSPOT OFF]

[RESET]

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

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

Flow Control in ABAP

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

IF Statement

IF <Condition>.

<Statement Block>

ELSEIF <Condition>.

<Statement Block>

ELSEIF <Condition>.

<Statement Block>

ELSE.

<Statement Block>

ENDIF.

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.

Invalid Date

DATA: today TYPE D.

today = ‘20061321’.

today = today + 0.

if today is initial.

write: / ‘invalid date’.

else.

write: / today.endif.

CASE Statement

CASE <field>.

WHEN <value1>.

<Statement Block>

WHEN <value2>.

<Statement Block>

...

WHEN OTHERS.

<Statement Block>

ENDCASE.

CASE Statement

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

DO Statement

DO.

WRITE sy-index.

IF sy-index = 3.

EXIT.

ENDIF.

WRITE: sy-index.

ENDDO.

CONTINUE Statement

DO 5 TIMES.

IF sy-index = 3.

CONTINUE.

ENDIF.

WRITE: sy-index.

ENDDO.

CHECK Statement

DO 4 TIMES.

CHECK sy-index BETWEEN 2 AND 3.

WRITE: sy-index.

ENDDO.

WHILE Statement

DATA: count TYPE I value 1.

WHILE count <> 4.

WRITE: sy-index.

count = count + 1.

ENDWHILE.

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

Operators/Function Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

** Powers

DIV Integer division

MODRemainder of integer division

SQRT Square root.

Arithmetic Expressions

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

Character String Operator (2)

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

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

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

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

* 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

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

* Shift

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

SHIFT name UP TO ‘Bill’.

WRITE: / name.

Bill Charles

SHIFT Statement (2)

* 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

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

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.

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

CONDENSE tmp.

CONDENSE tmp NO-GAPS.

Removing Spaces(CONDENSE)

I am a boy

Iamaboy

* 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

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

Split

David _John _Peter

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

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.

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

STRLEN Built-in Function

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

count TYPE I.

count = strlen( tmp ).

WRITE count.

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.

WRITE ‘

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

* 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

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

Pattern

กดปุ่��ม Continue

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

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

ปุ่��ม Copy

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

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

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

Exercise II