1 . FIELD-SYMBOLS:
当不输入
当输入
例如﹕ (说明﹕用户日期格式为dd.mm.yyyy)
FIELD-SYMBOLS:
DATA: dat(8) VALUE '19950916' .
ASSIGN dat TO
WRITE:/
FIELD-SYMBOLS:
DATA: dat(8) VALUE '19950916' .
ASSIGN dat TO
WRITE:/
FIELD-SYMBOLS:
DATA: dat(8) VALUE '19950916' .
ASSIGN dat TO
WRITE:/
输出为﹕16091995
2.定义结构化的field-symbols
FIELD-SYMBOLS:
首先将
注意点: a.
法错误
b.
易出错
例如﹕
DATA: wa(100) VALUE '001LH 123419950627' .
DATA: BEGIN OF line1 ,
col1(6) TYPE c,
col2(4) TYPE c,
col3(8) TYPE c,
END OF line1.
FIELD-SYMBOLS:
FIELD-SYMBOLS:
WRITE:/
WRITE:/
输出结果﹕ 001 LH 1234 27.06.1995
001LH 1234 19950627
如果下列代码就会出错﹕ (原因wa没有line1长)
DATA: wa(17) VALUE '001LH 123419950627' .
DATA: BEGIN OF line1 ,
col1(6) TYPE c,
col2(4) TYPE c,
col3(8) TYPE c,
END OF line1.
FIELD-SYMBOLS:
结构中含有I, F类型的结构例如﹕
DATA: BEGIN OF line1 ,
col1 TYPE i,
col2(4) TYPE c,
END OF line1.
DATA: BEGIN OF line2,
col1 TYPE i,
col2(4) TYPE c ,
END OF line2 .
line2-col1 = 8 . line2-col2 = 'test' .
FIELD-SYMBOLS:
WRITE:/
输出结果﹕ 8 test
如果如下则错误(因为line2长度< line1 )
DATA: BEGIN OF line1 ,
col1 TYPE i,
col2(5) TYPE c,
END OF line1.
DATA: BEGIN OF line2,
col1 TYPE i,
col2(4) TYPE c ,
END OF line2 .
line2-col1 = 8 . line2-col2 = 'test' .
FIELD-SYMBOLS:
WRITE:/
(二) ASSIGN的用法﹕
1.静态分配
a.无偏移量
ASSIGN
例如﹕
FIELD-SYMBOLS:
FIELD-SYMBOLS:
DATA: txt(20) TYPE c VALUE 'Hello, How are you?'.
DATA: num TYPE i VALUE 5 .
ASSIGN txt TO
ASSIGN num TO
DESCRIBE FIELD
WRITE:/
输出结果﹕Hello, How are you? has length 20
如下代码将出错(原因
FIELD-SYMBOLS:
FIELD-SYMBOLS:
DATA: txt(20) TYPE c VALUE 'Hello, How are you?'.
DATA: num TYPE i VALUE 5 .
ASSIGN txt TO
*ASSIGN num TO
DESCRIBE FIELD
WRITE:/
b.有偏移量
ASSIGN
说明﹕(1).
(2).系统不会判断所选择的部分是否在
(3).如果没有指定长度
(4).如果
照
例如﹕
FIELD-SYMBOLS:
DATA: BEGIN OF line1,
col1(10) VALUE '0123456789',
col2(10) VALUE 'abedefghij',
END OF line1.
WRITE:/ line1-col1+5.
ASSIGN line1-col1+5 TO
WRITE:/
ASSIGN line1-col1+5(*) TO
WRITE:/
输出结果﹕56789
56789abede
56789
在内存地址分配时﹐和容易造成内存中的数据更改问题﹐例如﹕
FIELD-SYMBOLS:
DATA: BEGIN OF line ,
a VALUE '1', b VALUE '2',
c VALUE '3', d VALUE '4',
e VALUE '5', f VALUE '6',
g VALUE '7', h VALUE '8',
END OF line .
DATA: off TYPE i .
DATA: len TYPE i VALUE 2 .
ASSIGN line TO
WRITE:/
DO 2 TIMES .
off = sy-index * 3 .
ASSIGN line-a+off(1) TO
ENDDO .
ASSIGN line TO
WRITE:/
输出结果为﹕12345678
123X56X8
可以看出在把
2.动态分配(不是万不得以﹐不要使用这种方式,因其找
a. ASSIGN (
说明﹕ 1.如果分配是在子程序或功能模块中执行﹐程序将在局部的子程序或功能模块中搜索字
段
2.如果分配是在子程序或功能模块之外执行,或未找到字段
字段
3.如在全局变量中都未找到字段
注意﹕如果找到
例如﹕
主程序Report1 .
REPORT1 .
TABLES : SBOOK .
SBOOK-FLDATE = ‘20031219’ .
PERFORM fomr1(Report2) .
子程序Report2 .
REPORT2 .
FORM form1 .
PERFORM form2(Report3) .
ENDFORM .
字程序Report3 .
REPORT3.
FORM form2 .
DATA: name(20) TYPE C VALUE ‘SBOOK-FLDATE’ .
FIELD-SYMBOLS
ASSIGN (name) to
IF SY-SUBRC = 0 .
WRITE:/
ENDIF .
ENDFORM .
输出结果﹕19.12.2003
说明﹕子程序Report3中引用了主程序的工作取区域的字段fldate,如果用静态Assign来无法实现﹐因为在子程序Report3无SBOOK-FLDATE ,语法检查会出现错误
b.表工作区的动态分配
ASSIGN TABLE FIELD (
把主程序Tables处声明的工作区域指派给
例如﹕
TABLES : sbook .
DATA: name1(20) VALUE 'SBOOK-FLDATE' .
DATA: name2(20) VALUE 'name1' .
FIELD-SYMBOLS :
ASSIGN TABLE FIELD (name1) TO
WRITE:/ 'sy-subrc' , sy-subrc .
ASSIGN TABLE FIELD (name2) TO
WRITE:/ 'sy-subrc' , sy-subrc .
输出结果﹕ sy-subrc 0
sy-subrc 4
3.将字段符号分配给其他字段符号
ASSIGN
等同与下列动态分配
ASSING TABLE FIELD (
其中
说明﹕
例如﹕
DATA: BEGIN OF line1 ,
a VALUE '1', b VALUE '2',
c VALUE '3', d VALUE '4',
e VALUE '5', f VALUE '6',
g VALUE '7', h VALUE '8',
END OF line1 .
DATA: off TYPE i .
FIELD-SYMBOLS :
ASSIGN line1-a TO
DO 4 TIMES .
off = sy-index - 1 .
ASSIGN
WRITE:
ENDDO .
输出结果﹕ 1 2 4 7
分析﹕
首先
循环中﹕
off = 0时,
off = 1时,
off = 2时,
off = 3时,
4.分配字段串组件
ASSIGN COMPONENT
系统将字符串
例如﹕
DATA: BEGIN OF line1,
col1 TYPE i VALUE '11',
col2 TYPE i VALUE '22',
col3 TYPE i VALUE '33',
END OF line1.
DATA: comp(10) TYPE c VALUE 'col3' .
FIELD-SYMBOLS :
ASSIGN line1 TO
ASSIGN comp TO
DO 3 TIMES .
ASSIGN COMPONENT sy-index OF STRUCTURE
WRITE:
ENDDO .
ASSIGN COMPONENT
WRITE: /
输出结果﹕ 11 22 33
33
(三) .定义字段符号的格式
1.定义字段符号的类型
ASSIGN …. TO
说明﹕a.如果
b.如果
注意类型转化的可兼容性
例如﹕
DATA: text(8) TYPE c VALUE '20031221' .
DATA: t_type .
DATA: t_len TYPE i .
FIELD-SYMBOLS :
ASSIGN text TO
WRITE:/
DESCRIBE FIELD
LENGTH t_len .
WRITE: t_type , t_len .
ASSIGN text TO
WRITE:/
DESCRIBE FIELD
LENGTH t_len .
WRITE: t_type , t_len .
输出结果﹕20031221 C 8
21122003 D 8
2.更改字段符号的小数位
ASSIGN …. TO
说明﹕
型﹐将导致程序运行错误
例如﹕
DATA: pack1 TYPE p DECIMALS 2 VALUE '400' .
DATA: pack2 TYPE p DECIMALS 2 .
DATA: pack3 TYPE p DECIMALS 2 .
FIELD-SYMBOLS:
WRITE:/1(8) 'Pack1', pack1 .
ASSIGN pack1 TO
WRITE:/1(8) '
pack2 =
WRITE:/1(8) 'Pack2', pack2 .
ASSIGN pack2 TO
WRITE:/1(8) '
pack3 =
WRITE:/1(8) 'Pack3', pack3 .
WRITE:/1(8) '
WRITE:/1(8) 'Pack2', pack2 .
输出结果﹕
Pack1 400.00
Pack2 4,000.00
Pack3 4,040.00
Pack2 123,456.78
(四) .运行检查﹐防止取指派变量定义取之外的数据
注意点﹕1.对于内表﹐存储区的大小由记录的行数的多少﹐这种表行数不固定﹐运行时存储区动态分配,
容易造成取存储区之外的数据﹐应该注意
2.对于DATA定义的变量﹐存储区固定﹐注意不要取存储区之外的数据
例如﹕
DATA: BEGIN OF line1 ,
col1(4) TYPE c VALUE '1234',
col2(2) TYPE c VALUE '56',
END OF line1 .
DATA: text1(1) TYPE c VALUE 'a' .
DATA: text2(2) TYPE c VALUE 'bc' .
DATA: text3(3) TYPE c VALUE 'def'.
FIELD-SYMBOLS :
DO 6 TIMES .
ASSIGN text1+sy-index(1) TO
WRITE:
ENDDO.
程序运行错误,因为在循环中超出存储区域的范围