ABAP 指针基础入门例子
程序员文章站
2022-05-13 22:48:17
...
先放源代码 大家可以运行下例子 后续我再补充完善。
*一般类型
FIELD-SYMBOLS <fs_field> TYPE any.
FIELD-SYMBOLS <fs_field_02> ."以上两个是效果一样
FIELD-SYMBOLS <fs_table> TYPE ANY TABLE." 接受任意表的
*完整类型
TYPES gty_makt TYPE TABLE OF makt.
TYPES gs_makt TYPE makt.
FIELD-SYMBOLS <fs_char> TYPE c.
FIELD-SYMBOLS <fs_makt> TYPE makt.
FIELD-SYMBOLS <ft_makt> TYPE gty_makt. "只能接受makt的表类型
DATA gv_char TYPE c LENGTH 20 VALUE '北京'.
DATA gs_makt TYPE makt.
DATA gv_numc TYPE n LENGTH 20 VALUE '123456'.
*分配
ASSIGN gv_char TO <fs_field>.
WRITE:/ gv_char.
WRITE:/ <fs_field>.
*分配结构体
SELECT SINGLE *
FROM makt
INTO CORRESPONDING FIELDS OF gs_makt.
ASSIGN gs_makt TO <fs_field>.
WRITE:/ <fs_field>.
ASSIGN gv_char TO <fs_char>.
WRITE:/ <fs_char>.
*分配结构
ASSIGN gs_makt TO <fs_makt>.
WRITE:/ <fs_makt>-matnr , <fs_makt>-maktx.
*动态指定
DATA:gv_char01 TYPE c LENGTH 5 VALUE 'A',
gv_char02 TYPE c LENGTH 5 VALUE 'B',
gv_char03 TYPE c LENGTH 5 VALUE 'C',
gv_char04 TYPE c LENGTH 5 VALUE 'D',
gv_char05 TYPE c LENGTH 5 VALUE 'E',
gv_char06 TYPE c LENGTH 5 VALUE 'F'.
DATA gv_index TYPE n LENGTH 2. "为什么是两位 因为是前导0
DATA gv_fieldname TYPE c LENGTH 30. "变量的名称
gv_fieldname = 'GV_CHAR'.
ASSIGN (gv_fieldname) TO <fs_field>. "动态指定,用小括号 小括号里面是值所代表的变量.
WRITE:/ <fs_field>."输出的是北京
ASSIGN gv_fieldname TO <fs_field>.
WRITE:/ <fs_field>."输出的是gv_char
DO 6 TIMES.
gv_index = sy-index.
CONCATENATE 'GV_CHAR' gv_index INTO gv_fieldname.
ASSIGN (gv_fieldname) TO <fs_field>.
WRITE:/ gv_index , ':', <fs_field>.
ENDDO.
*结构体gs_makt 通过FS进行输出
*ASSIGN gs_makt to <fs_field>.
ASSIGN COMPONENT 'matnr' OF STRUCTURE gs_makt TO <fs_field>. "把结构体的这个字段赋给结构体
WRITE:/ <fs_field>.
ASSIGN COMPONENT 1 OF STRUCTURE gs_makt TO <fs_field>. "通过字段名的序号
WRITE:/ <fs_field>.
DO 5 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE gs_makt TO <fs_field>. "通过字段名的序号
WRITE:/ <fs_field>.
ENDDO.
*结构体GS_makt 字段 动态分配
gv_fieldname = 'MATNR'.
ASSIGN COMPONENT gv_fieldname OF STRUCTURE gs_makt TO <fs_field>. "gv_fieldname 必须是结构gs_makt的一个字段
IF <fs_field> IS ASSIGNED.
WRITE: / '动态分配:', <fs_field>.
ENDIF.
*fs_field 一般类型
*fs_makt makt 完整类型
ASSIGN gs_makt TO <fs_makt>.
WRITE:/ <fs_makt>-matnr.
*非完整类型定义 不能够直接使用组件
ASSIGN gs_makt TO <fs_field>."
*WRITE:/ <fs_field>-matnr.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_field> TO <fs_field_02>."02也是any类型
WRITE:/ '测试1:',<fs_field_02>.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE gs_makt TO <fs_field_02>.
WRITE:/ '测试2:',<fs_field_02>.
gv_fieldname = 'MATNR'. "这样可以任意的变量进行动态分配.
ASSIGN COMPONENT gv_fieldname OF STRUCTURE <fs_field> TO <fs_field_02>.
WRITE:/ '测试3:',<fs_field_02>.
*强制类型更变
ASSIGN gv_numc TO <fs_char> CASTING."如果指针有完整类型的定义 就不需要在CASTING 后面添加类型
TYPES:BEGIN OF gty_score,
name TYPE c LENGTH 6,
score TYPE n LENGTH 3,
END OF gty_score.
gv_char = '张三123100'."按数量去截分配对应的字段
ASSIGN gv_char TO <fs_field_02> CASTING TYPE gty_score. "因为<fs_field_02> 为any类型 所以后面要加具体类型
"<fs_field_02> 就有了gty_score 里面的字段
DO 2 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_field_02> TO <fs_field>.
WRITE:/ sy-index,<fs_field>.
ENDDO.
FIELD-SYMBOLS <fs_datum>.
DATA gv_datum TYPE d.
ASSIGN gv_datum TO <fs_datum>.
gv_datum = sy-datum.
WRITE:/ '<fs_datum>日期:',<fs_datum>.
*<fs_datum> = '20200208'."1
gv_datum = '20200208'."2 12这两种方式效果一样
WRITE:/'gv_datum日期:',gv_datum.
WRITE:/ '<fs_datum>日期:',<fs_datum>.
DATA: gv_1 TYPE c VALUE 'A',
gv_2 TYPE c VALUE 'B',
gv_3 TYPE c VALUE 'C',
gv_4 TYPE c VALUE 'D',
gv_5 TYPE c VALUE 'E',
gv_6 TYPE c VALUE 'F',
gv_7 TYPE c VALUE 'G'.
DATA gv_fieldname_02(4) TYPE c.
DATA gv_index_02 TYPE c.
FIELD-SYMBOLS <fs_data>.
DO 7 TIMES.
CLEAR gv_fieldname_02.
gv_index_02 = sy-index.
CONCATENATE 'GV_' gv_index_02 INTO gv_fieldname_02.
ASSIGN (gv_fieldname_02) TO <fs_data>.
WRITE:/ <fs_data>.
ENDDO.
DATA:BEGIN OF gs_str,
col1(5) VALUE 'china',
col2(10) VALUE 'BEIJING',
col3(15) VALUE 'GREAT WALL',
END OF gs_str.
FIELD-SYMBOLS <fs_fieldname_03>.
DATA:gv_index2(1) TYPE n.
DO 3 TIMES.
CLEAR: gv_fieldname_02,gv_index2.
gv_index2 = sy-index.
CONCATENATE 'COL' gv_index2 INTO gv_fieldname_02.
ASSIGN COMPONENT gv_fieldname_02 OF STRUCTURE gs_str TO <fs_fieldname_03>.
WRITE: <fs_fieldname_03>.
ENDDO.
*表类型
DATA gt_makt TYPE STANDARD TABLE OF makt.
SELECT *
FROM makt
INTO CORRESPONDING FIELDS OF TABLE gt_makt
UP TO 10 ROWS
WHERE spras = sy-langu.
FIELD-SYMBOLS <fs_wa> TYPE any.
*向一般类型字段符号分配.
ASSIGN gt_makt TO <fs_table>.
*ASSIGN gt_makt TO <fs_field>. "表类型都要参照any table
*LOOP AT <fs_table> ASSIGNING <fs_field_02>.
*
*ENDLOOP.
*向特定类型
ASSIGN gt_makt to <ft_makt>.
*内表的读取
READ TABLE <ft_makt> ASSIGNING <fs_wa> INDEX 1. "第一种读取方式
IF sy-subrc eq 0.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_field>.
WRITE :/ <fs_field>.
ENDIF.
READ TABLE <ft_makt> ASSIGNING <fs_wa> WITH KEY matnr = '23'. "第二种读取方式
IF sy-subrc eq 0.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa> TO <fs_field>.
WRITE :/ <fs_field>.
ENDIF.
*内表的读取 一般类型
*READ TABLE <fs_table> ASSIGNING <fs_wa> WITH KEY matnr = '23'. "不能这样使用 <fs_table>为any 不知道里面的字段是什么
*READ TABLE <fs_table> ASSIGNING <fs_wa> index 1. "any table不能这样用
gv_fieldname = 'MATNR'.
*动态指定字段
READ TABLE <fs_table> ASSIGNING <fs_wa> WITH KEY (gv_fieldname) = '23' .
*一般loop 循环
LOOP AT gt_makt INTO gs_makt."这样效率比较慢
IF gs_makt-matnr = '23'.
gs_makt-maktx = '修改物料描述'.
MODIFY gt_makt FROM gs_makt.
ENDIF.
ENDLOOP.
*快一点的用指针效率更高 占用内存更少
LOOP AT gt_makt ASSIGNING FIELD-SYMBOL(<fs_makt_02>).
IF <fs_makt_02> = '23'.
<fs_makt_02>-maktx = '修改物料描述'.
* MODIFY gt_makt FROM <fs_makt_02>. "这个语句不需要 他可以直接修改
ENDIF.
ENDLOOP.
上一篇: FLEX 数据绑定专题三(转)
下一篇: android 延时跳转页面的两种方法