SAP基礎程序
- 教學登入client: 810
- User: lwl101
- Password: passw0rd
ABAP/4
Advanced Business Application Programming (Allgemeiner Berichts-Aufbereitungs-Prozessor) is a programming language developed by SAP for the interactive development of application programming
- ABAP is tightly integrated across most of SAP modules (like SD, MM) and transactions (like screens)
- All ABAP application, along with parts of SAP NetWeaver Application Server ABAP, are written in the ABAP Workbench development environment using ABAP.
Tools
Object Navigator (SE80)
- A central point of entry into the ABAP Workbench
- Organize your programming in an integrated development environment
ABAP Dictionary (SE11)
- Describes and manages all the data definitions used in the system centrally
ABAP Editor (SE38)
- Create and edit your programs and their components
Function Builder (SE37)
- Allows you to create, test, and administer function modules in an integrated environment (Procedural)
Class Builder (SE24)
- allows you to create, define, change and test global ABAP classes and interfaces (Object-Oriented)
- 表格查看(SE16)
Creating ABAP program
- Customized Reports must start with Y or Z
- Do not use any special characters except underscore (_)
ABAP基礎
- Each statement begins with a keyword and ends with a period (.)
The first word of a statement which determines the use of the entire statement
- Declarative keywords: Types, Data, Tables
Modularization keywords:
- Event Keyword:
Defining Keyword:
- FORM...ENDFORM,
- FUNCTION...ENDFUNCTION
- MODULE...ENDMODULE
Control Keywords
- IF
- WHILE
- DO
- FOR
- CASE
Operational Keywords
- Write
- MOVE
- Add
Comments
- *
- "
Data Declearation
Statement:
* Syntax
DATA var(length) [TYPE type | LIKE Table-Field] LENGTH [length]
[Decimals number] [VALUE initial value].
* eg
DATA I(12) Type I Value 5.
Predifined Types
Data Type | Initial Field Length | Valid Field Length | Initial Value | Meaning |
---|---|---|---|---|
I | 4 | 4 | 0 | Integer |
F | 8 | 8 | 0 | Float |
P | 8 | 1-16 | 0 | Packed number |
C | 1 | 1-65535 | '...' | Text |
D | 8 | 8 | '00000000' | Date(Format YYYYMMDD) |
N | 1 | 1-65535 | '0...0' | Numeric Text |
T | 6 | 6 | '000000' | Time(HHMMSS) |
X | 1 | 1-65535 | X'0...0' | Hex |
Local Data Type
Example:
TYPES fnumber TYPE I.
DATA flightANum TYPE fnumber.
* The type is a record of table ZZZ_SBOOK01
DATA: I_sbook01 TYPE ZZZ_SBOOK01.
* '-' connect the tablename and field, to declear type.
PARAMETERS: Sbookid TYPE ZZZ_SBOOK01-BOOKID,
Sfldate TYPE ZZZ_SBOOK01-FLDATE,
Sflid TYPE ZZZ_SBOOK01-FLID,
Scustid TYPE ZZZ_SBOOK01-CUSTID,
Sflclas TYPE ZZZ_SBOOK01-FLCLASS,
Slgweig TYPE ZZZ_SBOOK01-LUGGWEIGHT.
WRITE Position
Write 'Hello'. " Line 1
Write 8 'World' " Line 1
Write /(5) 'xyz'. " Line 2
Write 'abc'. " Line 2
Data num1 Type p Decimals 1 VALUE '-1234.5'.
Write /(8) num1. " Line 3
Data time1 Type T.
Write / time1. " Line 4
Write / time1 no-zero. " Line 5
Write / SY-DATUM. " Line 6
Write / SY-DATUM yymmdd. " Line 7
Write /(10) 'xyz' Centered. " Line 8
Write /(10) 'xyz' Right-Justieied. " Line 9
Write / 'def' under 'xyz'. " Line 10
Data num2 Type p Decimals 3 Value '3.141'
Write / num2 Decimals 1. " Line 11
Line | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | H | e | l | l | o | W | o | r | l | d | ||||
2 | x | y | z | a | b | c | ||||||||
3 | 1 | . | 2 | 3 | 4 | , | 5 | - | ||||||
4 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||
5 | ||||||||||||||
6 | 0 | 3 | . | 1 | 2 | . | 2 | 0 | 2 | 4 | ||||
7 | 2 | 4 | 1 | 2 | 0 | 3 | ||||||||
8 | x | y | z | |||||||||||
9 | x | y | z | |||||||||||
10 | d | e | f | |||||||||||
11 | 3 | , | 1 |
- 打印的字符串前面的數字:括號是佔位,無括號是直接由第幾位開始
- 數字的符號永遠在最後面
Write Options
- No-Zero
- No-Sign
- Centered
- Right-justified, Left-justified
- No-gap
- Under
- Decimals
- SKIP 創建新行
Common System Fields
- SY-DATUM 系統日期
- SY-UZEIT 系統時間
- SY-UNAME User Name
- SY-SUBRC 返回值
Arithmetic Operations
符號
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
DIV | 無餘數除法 |
MOD | 取餘數 |
** | 冪 |
Keyword
Operation | Meaning | Keyword |
---|---|---|
加 | m=m+n | ADD n TO m. |
減 | m=m-n | SUBTRACT n FROM m. |
乘 | m=m*n | MULTIPLY m BY n |
除 | m=m/n | DIVIDE m BY n |
無餘數 | p = m DIV n | |
取餘數 | p = m MOD n | |
冪 | p = m ** n | |
平方根 | p = SQRT(m) |
Bool
Operators | Operations |
---|---|
m EQ n | Equal to |
m NE n | Not Equal to |
m GT n | Greater than |
m GE n | Greater than or equal to |
m LT n | Smaller than |
m LE n | Smaller than or equal to |
m BETWEEN f1 and f2 | Interval |
m IS INITIAL | Initial Value |
b1 AND b2 | And |
b1 OR b2 | OR |
String Bool
Operators | Operation | Example |
---|---|---|
m CO n | Contains only: m只包括n中字符串構成的字符 | 'SE2A' CO '012345' return false |
m CO n | Contains any: m包括任意n中字符串 | '432' CA '012345' return true |
m CS n | Contains: m包括在n中 | '432' CA '012345' return false |
m CP n | 類似正則,+表示任意字符、*表示任意字符串、#是轉移符號 | 'ABXDE' CP '*B+D*' return true |
Loop
DO
DO 3 Times.
WRITE /(2) SY-INDEX. "SY-INDEX是循環的次數
WRITE 'Hello'.
ENDDO.
Line | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
1 | 1 | H | e | l | l | o | |
2 | 2 | H | e | l | l | o | |
3 | 3 | H | e | l | l | o |
CONTINUE Statement
跳過循環中接下來的代碼,進入下一個循環
DATA rem TYPE i.
DO 20 TIMES.
rem = SY-INDEX MOD 5.
IF rem EQ 0.
CONTINUE.
ENDIF.
WRITE (2) SY-INDEX.
ENDDO.
* 當數值為5的倍數,取模後為0,則跳過WRITE
Output
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
Check Statement
檢查條件,不符合則進入下一個循環而不執行下面代碼
DATA rem TYPE i.
DO 20 TIMES.
rem = SY-INDEX MOD 5.
CHECK rem EQ 0.
WRITE (2) SY-INDEX.
ENDDO.
* 當數值為5的倍數,取模後為0,則才能執行下面的WRITE
Output
5 10 15 20
EXIT
終止循環
DATA rem TYPE i.
DO 20 TIMES.
rem = SY-INDEX MOD 5.
IF rem EQ 0.
EXIT.
ENDIF.
WRITE (2) SY-INDEX.
ENDDO.
Output
1 2 3 4
WHILE
DATA cnt TYPE i VALUE 3.
WHILE cnt >= 1.
WRITE /(2) SY-INDEX.
WRITE 'Hello'.
cnt = cnt - 1.
ENDWHILE.
Line | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
1 | 1 | H | e | l | l | o | |
2 | 2 | H | e | l | l | o | |
3 | 3 | H | e | l | l | o |
IF
DATA mark TYPE i VALUE 50.
IF mark <= 40.
WRITE 'F'.
ELSEIF mark <= 60.
WRITE 'B'.
ELSE.
WRITE 'A'.
ENDIF.
CASE
DATA grade TYPE c VALUE 'A'.
CASE grade.
WHEN 'A'.
WRITE 'Excellent'.
WHEN 'B'.
WRITE 'Good'.
WHEN OTHERS.
WRITE 'Bad'.
ENDCASE.