Chapter 8 Defining Data in ABAP/4, Part 2
1.定义常量:
Syntax for the CONSTANTS Statement
The following code demonstrates the syntax for defining a constant. It is similar to the data statement; however, the addition value is required. In all other ways, constants conform to the same rules as variables defined using the data statement. See Listing 8.1 for examples of constant definitions.
constants c1[(l)] [type t] [decimals d] value 'xxx'.
or
constants c1 like cv value 'xxx'.
where:
c1 is the name of the constant.
cv is the name of a previously defined constant or variable, or is the name of a field that belongs to a table or structure in the Data Dictionary.
(l) is the internal length specification.
t is the data type.
d is the number of decimal places (used only with type p).
'xxx' is a literal that supplies the value of the constant.
2.定义字符串(Field Strings):
类似于C中的结构。
Two statements are usually used to define field strings in an ABAP/4 program:
(1)data
Syntax for Defining a Field String Using the DATA Statement
The following is the syntax for defining a field string using the data statement.
data: begin of fs1,
f1[(l)] [type t] [decimals d] [value 'xxx'],
f2[(l)] [type t] [decimals d] [value 'xxx'],
...
end of fs1.
or
data begin of fs1.
data f1[(l)] [type t] [decimals d] [value 'xxx'].
data f2[(l)] [type t] [decimals d] [value 'xxx'].
...
[include structure st1.]
data end of fs1.
or
data fs1 like fs2.
where:
fs1 is the field string name.
f1 and f2 are the fields (also called components) of the field string.
fs2 is the name of a previously defined field string, or is the name of a table or structure in the Data Dictionary.
(l) is the internal length specification.
t is the data type.
d is the number of decimal places (used only with type p).
'xxx' is a literal that supplies a default value.
st1 is the name of a structure or table in the Data Dictionary.
(2)tables
Syntax for Defining a Field String Using the TABLES Statement
The following is the syntax for defining a field string using the tables statement.
tables fs1.
where:
fs1 is the field string name. A table or structure of the same name must exist in the Data Dictionary
3.定义类型:
Syntax for the TYPES Statement
The following is the syntax for defining a type using the types statement.
types t1[(l)] [type t] [decimals d].
or
types t1 like v1.
where:
t1 is the type name.
v1 is the name of a variable previously defined in the program, or is the name of a field that belongs to a table or structure in the Data Dictionary.
(l) is the internal length specification.
t is the data type.
d is the number of decimal places (used only with type p).
可以定义Structured Types,方便使用。
Chapter 9 Assignments, Conversions, and Calculations
顾名思义,这一章主要讲赋值语句,类型转换,计算语法。
1.System Variables(系统变量):
在第一章已经列了几个系统变量了。There are 176 system variables available within every ABAP/4 program. To display a list of system variables, display the DDIC structure syst.The alias for syst is sy (pronounced sigh). In your code, you can use either name. For example, you can code either sy-datum or syst-datum; they are exactly equivalent. Most programmers use sy.
Name Description
sy-datum Current date
sy-uzeit Current time
sy-uname Current user id
sy-subrc Last return code
sy-mandt Logon client
sy-pagno Current output page number
sy-colno Current output column number
sy-linno Current output list line number
sy-vline Vertical line
sy-uline Horizontal line
sy-repid Current report name
sy-cprog Main program name
sy-tcode Current transaction code
sy-dbcnt Within a select, contains the current iteration counter. After the endselect, contains number of rows that match the where clause
2.Assignment Statements:
Three assignment statements are commonly used:
(1)clear
Syntax for the clear Statement
The following is the syntax for the clear statement.
clear v1 [with v2 | with 'A' | with NULL]
where:
v1 and v2 are variable or field string names.
'A' is a literal of any length.
The following points apply:
If v1 is a variable of type c without any additions, it is filled with blanks. If v1 is any other data type, it is filled with zeros. If v1 is a field string, its individual components are set to blanks or zeros depending on their individual data types.
Using the addition with v2, the first byte from v2 is used to fill the entire length of v1. If v1 is a field string, it is treated as a variable of type c.
Using the addition with 'A', the entire length of v1 is filled using the first byte from literal 'A'.
Using the addition with NULL, the entire length of v1 is filled with hexadecimal zeros.
(2)move
Syntax for the move Statement
The following is the syntax for the move statement. Operators and operands must be separated by spaces. Multiple assignment occurs from right to left.
move v1 to v2.
or
v2 = v1.
or
v2 = v1 = vm = vn . . ..
or
move v1[+N(L)] to v2[+N(L)].
or
v2[+N(L)] = v1[+N(L)].
where:
v1 is the sending variable or field string.
v2 is the receiving variable or field string.
N is an offset from the beginning of the variable or field string.
L is the number of bytes to move.
(3)move-corresponding
To perform a move from one field string to another where the data types and/or lengths do not match, use the move-corresponding statement. It generates individual move statements for components with matching names.
Syntax for the move-corresponding Statement
The following is the syntax for the move statement. Operators and operands must be separated by spaces. Multiple assignment occurs from right to left.
move-corresponding v1 to v2.
where:
v1 is the sending variable or field string.
v2 is the receiving variable or field string
3.Performing Calculations
You can perform calculations using the following statements:
(1)compute
Valid Operators for the COMPUTE Statement:
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
DIV Integer division
MOD Remainder of integer division
(2)add or add-corresponding
(3)subtract or subtract-corresponding
(4)multiply or multiply-corresponding
(5)divide or divide-corresponding
Use the add statement to add one number to another. Field strings with components having the same name can be added together with add-corresponding.
add f1 to f2.
subtract f1 from f2.
multiply f2 by f1.
divide f2 by f1.
最重要的是注意顺序。
4.Dynamic Assignment
A field-symbol is a pointer you can dynamically assign to a field. After assignment, you can use the field-symbol anywhere in your program in place of the actual field name. Use the field-symbol statement to define a field-symbol, and use assign to assign a field to it. The field-symbol name must begin and end with angle brackets.
example:
1 report ztx0915.
2 data f1(3) value 'ABC'.
3 field-symbols <f>.
4 assign f1 to <f>.
5 write <f>.
6 <f> = 'XYZ'.
7 write / f1.
You can use field-symbols to create very generic programs. Suppose, for example, that you want to create a program that accepts a table name as an input parameter and displays the contents. You could not hard-code the field names on the write statement because the names are different in every table. You could instead use a field-symbol on the write statement to refer to the field of a table.
Chapter 10 Common Control Statements
再次顾名思义,这章主要讲的是程序控制语句。if,case,do,while,continue,check,exit。和C大同小异,主要注意以下语法。
1.Syntax for the if Statement
The following is the syntax for the if statement.
if [not] exp [ and [not] exp ] [ or [not] exp ].
---
[elseif exp.
---]
[else.
---]
endif.
Special Operators for Character Strings
Operator
Means
True When
Case
Sensitive? Trailing
Blanks
Ignored?
v1 CO v2 Contains Only v1 is composed solely of characters in v2 Yes No
v1 CN v2 not v1 CO v2 v1 contains characters that are not in v2 Yes No
v1 CA v2 Contains Any v1 contains at least one character in v2 Yes No
v1 NA v2 not v1 CA v2 v1 does not contain any character in v2 Yes No
v1 CS v2 Contains String v1 contains the character string v2 No Yes
v1 NS v2 not v1 CS v2 v1 does not contain the character string v2 No Yes
v1 CP v2 Contains Pattern v1 contains the pattern in v2 No Yes
v1 NP v2 not v1 CP v2 v1 does not contain the pattern in v2 No Yes
通配符:
* Match any sequence of characters.
+ Match any single character.
# Interpret the next character literally.
2.Syntax for the case Statement
case v1.
when v2 [ or vn ... ].
---
when v3 [ or vn ... ].
---
[ when others.
--- ]
endcase.
3.Syntax for the do Statement
do [ v1 times ] [ varying f1 from F11 next F12 [ varying f2 from F21 next F22 ... ] ].
---
[exit.]
---
enddo.
说明:
(1)如果不使用TIMES选项,则为无限循环,statement block中必须有EXIT,STOP,REJECT来避免死循环;
(2)系统变量SY-INDEX中为已处理过的循环次数;
(3)可以使用多个VARYING选项在每个循环中给变量f重新赋值.F11,F12,F13……必须是内存中类型相同和长度相同的一系列等距字段(经常是file string),应保证循环次数不超过涉及到的变量F11,F12,F13……的数量。
4.Syntax for the while Statement
while exp [ vary f1 from F11 next F12 [ vary f2 from F21 next F22 ... ] ]
---
[ exit. ]
---
endwhile.
5.the exit, continue, and check Statements
exit 终止这个循环的所有剩余循环。
continue 终止本次循环,并直接进入下一次。
check exp 如果条件exp为假,则跳过本次循环,进入下一次。
6.Simple Position and Length Specifications for the write Statement
Syntax for Simple Position and Length Specifications on the write Statement
write [/][P][(L)] v1.
where:
v1 is a variable, literal, or constant.
P is a number indicating a position on the current line of output.
L is a number indicating the number of bytes allocated in the list for outputting v1.
Chapter 11 Internal Tables
Chapter 12 Advanced Internal Tables, Part 1
第十一章主要讲内表基础,包括内表的定义,简单添加数据,读取数据,内表sorting。类似于C中数组的概念,不过差别还是比较大的。
第十二章主要讲内表编辑的各种语句,我把它们放在了一起。
1.内表定义:
(1)通过创建内表TYPE后再创建。
语法:TYPE <t> <type> OCCURS <n>.
或者
types: begin of <t> OCCURS <n>,
a type i,
b type c,
c(8) type c,
end of <t>.
DATA <f> TYPE <type> [WITH HEAD LINE].或
DATA <f> LIKE <s> [WITH HEAD LINE].
如果不加WITH HEAD LINE,则没有HEADLINE。
(2)根据已有TYPE直接创建内表。
语法:DATA <f> TYPE <type> OCCURS <n> [WITH HEAD LINE].或
DATA <f> LIKE <s> OCCURS <n> [WITH HEAD LINE].
如果不加WITH HEAD LINE,则没有HEADLINE。
(3)创建带新结构的内表。
语法(example):DATA:BEGIN OF <f> OCCURS <n>,
NAME(10) TYPE c,
Age TYPE i,
Score TYPE i,
END OF <f>.
不用加WITH HEAD LINE,自动有HEADLINE。
(4)include structure。
data begin of it5 occurs 100.
include structure ztxlfa1.
data end of it5.
data begin of it6 occurs 100.
include structure ztxlfa1.
data: delflag,
rowtotal,
end of it6.
2.编辑内表
A.逐行填充
(1)APPEND
append [wa to] [initial line to] it [sorted by c].
where:
wa is the name of a work area.
it is the name of a previously defined internal table.
sorted by c表示按c降序排列(只能按降序),另外,如果有了sorted by选项,那么如果当填充超过表格的厨师长度时不会往下增加内表大小,只会覆盖掉一部分。如果sort时遇到一样的,插在后面(如果需要舍弃,则舍弃新插的)
(2)INSERT
insert [wa into] it [index n]
注意:
If index is specified, the new row is inserted before row n. Row n then becomes row n+1.
The insert statement can be used inside or outside of loop at it. If it is outside, the index addition must be specified. If it is inside, index is optional. If it is not specified, the current row is assumed.
(3)COLLECT
collect [wa into] it.
注意:他会将内表中的非数字(非i,f,p型)的分类汇总,数字部分作叠加运算。
B.复制内表
(1)=
如果两个内表格式完全一样,可以用:it2[]=it2[].
注意要加[],表示body(与work area区分)。
(2)append lines
append lines of it1 [from nf] [to nt] to it2.
where:
The structures of it1 and it2 must match.
After the append lines statement has executed, sy-tabix contains the number of rows in the table.
(3)insert lines
insert lines of it1 [from nf] [to nt] into it2 [index nb].
区别在于insert将要插入的内容插在it2的第nb行之前。如果没有指定index,那么加在末尾。
C.修改内表
(1)MODIFY
语法:modify it [from wa] [index n] [transporting c1 c2 ... [where exp]]
说明:
modify it can be specified inside or outside of loop at it. If it is outside, index n must be specified(或者不用的话表示全部更改,或者用where语句指定). When inside, index n is optional. If it is not specified, the current row is modified.
transporting specifies which components are to be overwritten. Without it, all are overwritten.
用了index以后就不能再用where。
D.删除内表/行
(1)free
语法:free it.
说明:这里it表示内表body。清空表中内容,并释放存储空间。The header line, if it exists, remains unchanged。
(2)refresh
语法:refresh it.
说明:这里it表示内表body。清空表中内容,但是不释放存储空间。The header line, if it exists, remains unchanged。
If you intend to refill a table immediately after clearing it, refresh is more efficient than free because it avoids unnecessary memory allocations.
(3)clear
语法:clear it | clear it[]
说明:
If it has a header line, clear it[] deletes all rows. clear it clears the header line.
If it does not have a header line, both forms delete all rows and leave the memory allocated.
(4)delete
语法:delete it (a) [index n]
(b) [from i] [to j]
(c) [where exp]
说明:
The expression exp must have a component of it on the left side of each comparison. For example, if it has components f1 and f2, exp could be where f1 = 'A' and f2 = 'B'.
可用来删除表格部分内容。
E.editor-call模式
The editor-call statement displays the contents of an internal table to the user in an editor similar to the ABAP/4 source code editor. It is useful for debugging and as a simple interface for allowing the user to enter and modify data in tabular form.
语法:
editor-call for it [title t] [display mode]
where:
it is the name of an internal table.
t is a literal, constant, or variable.
The following points apply:
注意:
it can only contain type c components.
The maximum length for a row is 72 characters.
t is the text displayed in the title bar of the editor window.
The display mode addition causes the data to be displayed in the editor in display mode.
3.察看内表信息
(1)察看内表是否为空:
语法:if it[] is initial.
例如:
if it[] is initial.
write: / 'it is empty'.
endif.
(2)察看内表总行数:
语法:describe table it [lines i] [occurs j].
说明:用这条语句以后:
sy-tfill 表示表中已填充的行数
sy-tleng 表示一行的长度
sy-toccu 表示表的总大小(包括还没有填充的)
另外,如果加了lines i,则i和sy-tfill的值一样,如果加了occurs j,则j和sy-toccu的值一样。
(3)比较内表是否相同:
语法:if it1[] = it2[].
4.读取内表
(1)loop at
语法:
loop at it [into wa] [from m] [to n] [where exp].
---
endloop.
where:
it is the name of an internal table.
wa is the name of a work area.
m and n are integer literals, constants, or variables representing a relative row number. For example, 1 means the first row in the table, 2 means the second, and so on.
exp is a logical expression restricting the rows that are read
另外,loop中同样可以用exit,continue,check.
(2)read table
语法:
read table it [into wa] [index i | with key keyexp [binary search] ] [comparing cmpexp] [transporting texp].
where:
it is the name of an internal table.
wa is the name of a work area.
i is an integer literal, constant, or variable representing a relative row number. For example, 1 means the first row in the table, 2 means the second, and so on.
keyexp is an expression representing a value to be found.
cmpexp is a comparison expression representing a test to be performed on the found row.
texp is an expression representing the fields to be transported to the work area after a row has been found.
If both comparing and transporting are specified, comparing must come first.
keyexp:
可以为k1=F1 k2=F2,……;
或者(K1)=F1 (K2)=F2,……(用括号括起来表示取K1代表的值,K1要求是变量,且里面的字母要求是大写的uppercase);
或者 =wa ,表示和wa完全一样的行,wa可能是一个file string。
或者 wa, 表示从第一列开始和wa中的一样,wa可能比内表的项少,但要求从前面比起一样。
cmpexp:
comparing detects differences between the contents of the work area and a row that was found before it is placed in the work area. Use it in conjunction with index or with key. If, after a row has been found, the work area contents are the same as the found row, sy-subrc will be 0. If the contents are different, sy-subrc will be 2 and the contents will be overwritten by the found row. If the row is not found, sy-subrc will be > 2 and the contents will remain unchanged.
可以为 f1 f2; 表示只比较找到的row和work area中的f1 和 f2项
可以为 all fields;表示All fields are compared, as described for f1 f2....
可以为 no fields;表示No fields are compared. This is the default.
texp:
transporting affects how fields are moved from the found row to the work area. If it is specified, only the values of the specified components are moved to the work area. If a row is not found, transporting does nothing.
可以为 f1 f2; 表示只替换找到的row和work area中的f1 和 f2项
可以为 all fields;表示All fields are moved, as described for f1 f2.This is the default.
可以为 no fields;表示No fields are moved.
5.排序内表:
Syntax for the sort Statement
sort it [descending] [as text] [by f1 [ascending|descending] [as text]
f2 ...].
注意:
ascending is the default sort order.
If descending appears after sort and before any component names, it becomes the default and applies to all components. This default can be overridden on an individual component by specifying ascending after the component name.
[as text]选项影响字符的排序,若有此选项,可用SETLOCALLANGUAGE设置字符顺序。
Chapter 13 Advanced Internal Tables, Part 2
这一章主要讲从数据库中传输数据到内表中的方法,以及
1.Selecting Multiple Rows Directly into an Internal Table:
To select multiple rows directly into the body of an internal table, use the into table addition of the select statement. It takes the selected rows and places them into the body of an internal table in a single operation known as an array operation. No work areas are used or needed.
语法:
(a) select * from dbtab into [corresponding fields of] table it.
(b) select f1 f2 . . .from dbtab into [corresponding fields of] table it.
说明:
Other additions, such as where and order by, can follow it.
The select into table statement places all selected rows directly into the body of it. Existing internal table contents are first discarded.
注意:
Don't use an endselect with select into table. A syntax error will occur.
将从dbtab select出来的列(或者是*),依次放到内表中,只可以比内表短,后面的将按默认值(0或者空格自动添加)。
如果选择了*或者比内表长的项,加corresponding fields of选项也可以,但是效率低下。
2.Adding Rows one by one Using select
Using select to add rows one at a time requires the use of a work area and a second statement such as append, insert, or collect.
语法:(比上面少一个table)
(a) select * from dbtab into [corresponding fields of] it.
(b) select f1 f2 . . .from dbtab into [corresponding fields of] it.
例如:
select * from ztxlfa1 into it where land1 = 'DE'.
append it.
endselect.
或者
select * from ztxlfa1 where land1 = 'DE'.
append ztxlfa1 to it.
endselect.
3.Control Break Processing
用于在内表loop中,遇到特殊情况时执行一些语句,即控制中断语句。
(1)at first / endat
(2)at last / endat
语法:
loop at it.
---
at first.
---
endat.
---
at last.
---
endat.
---
endloop.
说明:
at first / endat:在loop开始执行前调用。
at last / endat:在loop执行完成后调用。
(3)at new / endat
(4)at end of / endat
语法:
loop at it.
---
at new c.
---
endat.
---
at end of c.
---
endat.
---
endloop.
说明:
at new / endat:c是内表中的一个元素,在c变化到新的元素后执行(包括第一次)。
at end of / endat:c是内表中的一个元素,在c变化到新的元素前执行(包括最后一次)。
(5)sum
语法:
在 at first / endat
at last / endat
at new / endat
at end of / endat
中插入sum.
说明:
统计控制区域内所有的数值的和,在这里面去的相应项的值即为sum的值。
(6)on change of / endon
语法:
on change of v1 [or v2 . . .].
---
[else.
---]
endon.
说明:
它和at new作用差不多,但是也有一些区别:
It can be used in any loop construct, not just loop at. For example, it can be used within select and endselect, do and enddo, or while and endwhile, as well as inside get events.
A single on change of can be triggered by a change within one or more fields named after of and separated by or. These fields can be elementary fields or field strings. If you are within a loop, these fields do not have to belong to the loop.
You can use else between on change of and endon.
You can use it with loop at it where . . ..
You can use sum with on change of. It sums all numeric fields except the one(s) named after of.
Any values changed within on change of remain changed after endon. The contents of the header line are not restored as they are for at and endat.
Chapter 14 The write Statement
这一章主要讲使用write输出内容时的各种语句,以及其中的技巧。
1.ABAP/4 Data Type Determines the Default Output Format and Length
Data Justification Format Sign Dflt Output
Type Length
i Right Zeros suppressed Trailing 11
p Right Zeros suppressed Trailing 2*fldlen or (2*fldlen)+1
f None Scientific notation Leading 22
n Right Leading zeros shown None fldlen
c Left Leading blanks suppressed fldlen
d Left Determined by user defaults 8
t Left HH:MM:SS 6
x Left 2*fldlen
2.the write Statement
语法:
write [at] [/p(l)] v1[+o(sl)]
(1) under v2 | no-gap
(2) using edit mask m | using no edit mask
(3) mm/dd/yy | dd/mm/yy
(4) mm/dd/yyyy | dd/mm/yyyy
(5) mmddyy | ddmmyy | yymmdd
(6) no-zero
(7) no-sign
(8) decimals n
(9) round n
(10) currency c | unit u
(11) left-justified | centered | right-justified
说明:
(1)under v2:v1输出在v2下面对齐(前面要手动换行)。no-gap则把v1与之前的空格全部清空。
(2)mask联想到submask,子网掩码,是的,这里也和子网掩码的概念差不多。例如:write: / f1 using edit mask 'LL_:__'.
m还可以是这种形式:'==XXXX'(XXXX是字母),为Conversion Exits。详细是function modual里面的内容。
(3)-(5)表示日期形式,例如:
write: / f1 mm/dd/yyyy,
/ f1 dd/mm/yyyy,
/ f1 mm/dd/yy.
(6)(7)no-zero把输出的前面的0给去掉,no-sign把输出的符号给去掉。
(8)(9)decimals n表示取小数点的后n位(没有4舍5入的概念),round n表示取小数点的后n位(有4舍5入)。
(10)currency c表示货币单位,其中c为单位缩写(例如:'USD');unit u表示单位,其中c为单位缩写(例如:'%')。
(11)left-justified | centered | right-justified分别表示左对齐,居中,右对齐。
week2完了,其实week1,week2讲的都是基本理论,语句之类,有点无聊。week3主要讲的是reporting和function modual,可视的,有意思一点。


