主要内容
-
内联声明
-
构造表达式
-
内表操作
-
Open SQL
-
其他
本文列出了ABAP新语法的一些使用方式,供大家学习参考。
内联声明
代码实现:
*&---------------------------------------------------------------------- * 主题一:内联声明 * 语法:DATA(...) ,FILED-SYMBOL(…) * 1. 定义变量 * 2. 定义结构 * 3. 定义内表 * 4. 定义指针 *&---------------------------------------------------------------------* * 记录时间:23.03.2019 记录人: YALUOO *&---------------------------------------------------------------------* *&*********取数 "客户的标签信息表 SELECT * FROM ztcust_tag INTO TABLE @DATA(gt_data) UP TO 5 ROWS. cl_demo_output=>write( gt_data ). *&*********定义变量 DATA(lv_card_no) = '1000023312'. "会员号 cl_demo_output=>write( lv_card_no ). *&*********定义结构 READ TABLE gt_data INTO DATA(gs_data) INDEX 1. IF sy-subrc EQ 0. DATA(ls_data) = gs_data. cl_demo_output=>write( ls_data ). ENDIF. *&*********定义内表 DATA(lt_data) = gt_data. cl_demo_output=>write( lt_data ). *&*********定义指针 LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<fs_data>) WHERE card_no EQ lv_card_no. <fs_data>-create_user = 'YALUOO'. "修改创建人 ENDLOOP. cl_demo_output=>write( lt_data ). cl_demo_output=>display( ).
运行结果:
构造表达式
代码实现:
*&---------------------------------------------------------------------- * 主题二:构造表达式 * 1. 实现构造: NWE -创建数据对象或类的实现 * 1.1 构造单值 * 1.2 构造结构 * 1.3 构造内表 * 1.4 构造类 * 2. 值构造: VALUE - 创建一个类型为dypee的数据 * 2.1 构造结构 * 语法: ... VALUE dtype | #( [BASE dobj] comp1 = dobj1 comp2 = dobj2 ... ) ... * 2.2 构造内表 : * 语法: ... VALUE dtype | #( [BASE itab] ( (line1-com1 = dobj1) ( line2 ..) ... ) ... * note: dytpe可接具体类型或者# ,接#数据类型必须确定 * 可以嵌套使用; * 内表赋值不能带表头; * 3. 组件构造: CORRESPONDING * 语法:... CORRESPONDING dtype | #( [BASE dobj] comp1 = dobj1 comp2 = dobj2 ... ) ... *&---------------------------------------------------------------------* * 记录时间:23.03.2019 记录人: YALUOO *&---------------------------------------------------------------------* " 自定义类型 TYPES: BEGIN OF ty_man, name TYPE char10, " 姓名 sex TYPE char1, " 性别 age TYPE p DECIMALS 2," 年龄 school TYPE char20, " 学校 END OF ty_man. DATA: gt_man TYPE TABLE OF ty_man. *&*********结构赋值 DATA(gs_man) = VALUE ty_man( name = 'Tom' sex = 'B' ). cl_demo_output=>write( gs_man ). "附加年龄信息 gs_man = VALUE #( name = 'Tom' sex = 'B' age = 18 ). *DATA(gs_man_02) = VALUE #( name = 'Tom' sex = 'B' age = 18 ) . "错误,未明确类型 cl_demo_output=>write( gs_man ). "附加学校信息 gs_man = VALUE ty_man( BASE gs_man school = 'A SCHOOL' ). cl_demo_output=>write( gs_man ). "调整学校信息 gs_man = VALUE #( BASE gs_man school = 'B SCHOOL' ). cl_demo_output=>write( gs_man ). *&*********内表赋值 gt_man = VALUE #( ( name = 'Anna' sex = 'G' age = 17 ) ( name = 'Ann' sex = 'G' age = 16 ) ). cl_demo_output=>write( gt_man ). "内表基础上附加额外数据 gt_man = VALUE #( BASE gt_man ( name = 'Xiaohong' sex = 'G' age = 20 school = 'C SCHOOL' ) ( name = 'Xiaoming' sex = 'B' age = 21 school = 'D SCHOOL' ) ). cl_demo_output=>write( gt_man ). cl_demo_output=>display( ). &*********Range 表赋值 DATA: r_data TYPE RANGE OF ztcust_tag-data_type. "内表不带表头 RANGES: r_data_01 FOR ztcust_tag-data_type. "内表带表头-不支持 "逐步往下填充内表数据 r_data = VALUE #( sign = 'I' option = 'BT' ( low = 10 high = 20 ) ( low = 100 high = 150 ) option = 'GT' ( low = 180 ) option = 'LT' ( low = 200 ) option = 'EQ' ( low = 8 ) sign = 'E' option = 'BT' ( low = 15 high = 18 ) ). cl_demo_output=>write( r_data ). cl_demo_output=>display( ).
运行结果:
代码实现:
TYPES: BEGIN OF ty_data. INCLUDE TYPE ztcust_tag. TYPES: flag TYPE char1, END OF ty_data. TYPES: BEGIN OF ty_data_t. INCLUDE TYPE ztcust_tag. TYPES: flag_t TYPE char1, END OF ty_data_t. DATA: gs_data_02 TYPE ty_data, gs_data_03 TYPE ty_data_t. *&*********取数 SELECT SINGLE * FROM ztcust_tag INTO @DATA(gs_data). *&*********对应字段赋值 gs_data_02 = CORRESPONDING #( gs_data ). cl_demo_output=>write( gs_data_02 ). gs_data_03-flag_t = abap_true. gs_data_03 = CORRESPONDING #( BASE ( gs_data_03 ) gs_data_02 )."不指定BASE 初始值会丢失 cl_demo_output=>write( gs_data_03 ). gs_data_03 = CORRESPONDING #( gs_data_02 )."初始值丢失 cl_demo_output=>write( gs_data_03 ). cl_demo_output=>display( ).
运行结果:
内表操作
代码实现:
*&---------------------------------------------------------------------- * 主题三:内表操作 * 1. 内表表达式- 相当于READ TABLE * 语法:… itab[ … ] … * note: 如果未找到对应的记录就会抛出CX_SY_ITAB_LINE_NOT_FOUND异常,SY-SUBRC不会记录 * 可以通过line_exists预定义函数改进 * 2. 內表预定义函数 * 2.1 line_exists( ) - 判断记录是否存在 * 2.2 line_index( ) - 获取符合记录的索引值 * 3. 內表推导 - FOR 理解为LOOP,是对实现操作符 NEW 和值操作符VALUE的一种增强,作用是构造內表内容 * 语法1 : …FOR i = ... [THEN expr] UNTIL | WHILE log_exp ... * 语法2 : …FOR wa|<fs> IN itab [INDEX INTO idx][cond][let_exp]... * 4. 內表筛选-FILTER -筛选内表中的数据 * 语法: FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname ] * WHERE c1 op f1 [AND c2 op f2 [...] ] ) ... * note: WHERE对应过滤的条件,是必须要指定的,注意有些操作符是不能在WHERE中使用的,如:OR , NOT 等 * EXCEPT如果不指定则表示满足条件的找出来,如果指定则表示不满足条件的找出来 * 5. 內表缩减 * 语法: ... REDUCE type( * [let_exp] * INIT {x1 = rhs1}|{<x1> = wrexpr1}|{x1|<x1> TYPE dtype1} * {x2 = rhs2}|{<x2> = wrexpr2}|{x2|<x2> TYPE dtype2} * ... * FOR for_exp1 * FOR for_exp2 * ... * NEXT ... * {x1 = rhs1}|{<x1> = wrexpr1} * {x2 = rhs2}|{<x2> = wrexpr2} * ... ) ... * 6. 内表分组 *&---------------------------------------------------------------------* * 记录时间:23.03.2019 记录人: YALUOO *&---------------------------------------------------------------------* *&*********取数 SELECT * FROM ztcust_tag INTO TABLE @DATA(gt_data) UP TO 5 ROWS. *&*********定义变量 DATA(lv_card_no) = '1000023312'. "会员号 cl_demo_output=>write( gt_data ). "通过索引值判断某一行记录是否存在,也可通过条件判断 IF line_exists( gt_data[ 4 ] ). "获取第4行记录 DATA(ls_data) = gt_data[ 4 ]. "获取第4行记录中的标签类型 DATA(lv_classify) = gt_data[ 4 ]-classify."标签类型 cl_demo_output=>write( ls_data ). cl_demo_output=>write( lv_classify ). ENDIF. "获取符合条件的索引值,未找到LV_INDEX为0 DATA(lv_index) = line_index( gt_data[ card_no = lv_card_no classify = lv_classify ] ). IF lv_index NE 0 AND line_exists( gt_data[ lv_index + 1 ] ) . CLEAR ls_data. "获取下一行记录 ls_data = gt_data[ lv_index + 1 ]. cl_demo_output=>write( ls_data ). ENDIF. cl_demo_output=>display( ).
运行结果:
代码实现:
TYPES: BEGIN OF ty_line, col1 TYPE i, col2 TYPE i, col3 TYPE i, END OF ty_line, "结构体 ty_tab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY. *&*********通过语法1给新內表赋值 - 类似于JAVA中的FOR循环 "for每次遍历一次都将结果,通过value赋值给内表gt_itab DATA(gt_itab) = VALUE ty_tab( FOR j = 11 THEN j + 10 UNTIL j > 40 "初始值,递增量,结束条件 " 结构中的字段赋值-参考类型ty_tab ( col1 = j col2 = j + 1 col3 = j + 2 ) "11 12 13 - value 到 gt_itab "21 22 23 - value 到 gt_itab "31 32 33 - value 到 gt_itab "41 - 结束循环 ). cl_demo_output=>display( gt_itab ).
运行结果:
代码实现:
*&*********同过语法2给新內表赋值 *&*********取数 "客户标签信息 SELECT * FROM ztcust_tag INTO TABLE @DATA(gt_data) UP TO 5 ROWS. IF gt_data IS NOT INITIAL. "标签日期日志表 SELECT * FROM ztcust_tag_log INTO TABLE @DATA(gt_data_t) FOR ALL ENTRIES IN @gt_data WHERE tag_id = @gt_data-tag_id. SORT gt_data_t BY tag_id. DELETE ADJACENT DUPLICATES FROM gt_data_t COMPARING tag_id. ENDIF. cl_demo_output=>write( gt_data ). cl_demo_output=>write( gt_data_t ). TYPES: BEGIN OF ty_tag_line, "标签表 tag_id TYPE ztcust_tag-tag_id, "标签ID card_no TYPE ztcust_tag-card_no, "会员号 tag_name TYPE ztcust_tag-tag_name, "标签值 "日志表 sernumber TYPE ztcust_tag_log-sernumber, "流水号 uname TYPE ztcust_tag_log-uname, "用户名 log_date TYPE ztcust_tag_log-log_date, "备份日期 log_time TYPE ztcust_tag_log-log_time, "备份时间 message_type TYPE ztcust_tag_log-message_type,"消息类型 message TYPE ztcust_tag_log-message, "消息文本 END OF ty_tag_line, ty_tag_tab TYPE STANDARD TABLE OF ty_tag_line WITH EMPTY KEY. *&*********将标签表和日志表的数据整合在一起,构建新的内表 DATA(gt_itab) = VALUE ty_tag_tab( FOR ls_itab IN gt_data WHERE ( classify = 'brands' )"遍历标签类型为brands; ls_itab 为隐形声明或者<fs> ( tag_id = ls_itab-tag_id card_no = ls_itab-card_no tag_name = ls_itab-tag_name sernumber = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-sernumber ) "通过VALUE语句和内表表达式赋值 uname = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-uname ) log_date = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-log_date ) log_time = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-log_time ) message_type = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-message_type ) message = VALUE #( gt_data_t[ tag_id = ls_itab-tag_id ]-message ) ) ). cl_demo_output=>write( gt_itab ). *&*********使用操作符FILTER过滤 DATA: gt_filter TYPE HASHED TABLE OF ty_tag_line WITH UNIQUE KEY uname. ***INitialize filter Table gt_filter = VALUE #( ( uname = 'XUWENPAN' ) ). "找出满足条件的数据 DATA(gt_out) = FILTER #( gt_itab IN gt_filter WHERE uname = uname ) . cl_demo_output=>write( gt_out ). "找出不满足条件的数据 DATA(gt_out_t) = FILTER #( gt_itab EXCEPT IN gt_filter WHERE uname = uname ) . cl_demo_output=>write( gt_out_t ). cl_demo_output=>display( ).
运行结果:
代码实现:
*&*********取数 TYPES: BEGIN OF ty_man, name TYPE char10, " 姓名 sex TYPE char1, " 性别 age TYPE p DECIMALS 2," 年龄 school TYPE char20, " 学校 END OF ty_man. DATA: gt_man TYPE TABLE OF ty_man. gt_man = VALUE #( ( name = 'Anna' sex = 'G' age = 17 ) ( name = 'Ann' sex = 'G' age = 16 ) ). cl_demo_output=>write( gt_man ). "内表基础上附加额外数据 gt_man = VALUE #( BASE gt_man ( name = 'Xiaohong' sex = 'G' age = 20 school = 'C SCHOOL' ) ( name = 'Xiaoming' sex = 'B' age = 21 school = 'D SCHOOL' ) ). cl_demo_output=>write( gt_man ). "内表行数 DATA(lv_lines) = lines( gt_man ). "内表中符合条件的数据有几条 DATA(lv_lines_g) = REDUCE i( INIT x = 0 FOR ls_man IN gt_man WHERE ( sex = 'G' ) NEXT x = x + 1 ). cl_demo_output=>write( lv_lines ). cl_demo_output=>write( lv_lines_g ). "累计内表中符合条件的年龄之和 TYPES: ty_age TYPE p DECIMALS 2. DATA(lv_sum_age) = REDUCE ty_age( INIT dage = VALUE ty_age( ) FOR wa IN gt_man WHERE ( sex = 'G' ) NEXT dage = dage + wa-age ). cl_demo_output=>write( lv_sum_age ). "综合例子 TYPES:BEGIN OF ty_result, sum TYPE p DECIMALS 2, "总和 max TYPE p DECIMALS 2, "最大值 avg TYPE p DECIMALS 2, "平均 cunt TYPE i, "记录数 END OF ty_result. DATA(ls_result) = REDUCE ty_result( INIT res = VALUE ty_result( ) "可以默认值:ty_result( min = 0 max = 0 ) FOR <fs_man> IN gt_man WHERE ( sex = 'G' ) "性别为G NEXT res-sum = res-sum + <fs_man>-age "年龄总和 res-max = nmax( val1 = res-max val2 = <fs_man>-age )"最大年龄 res-cunt = res-cunt + 1 "满足条件的条目数 ). ls_result-avg = ls_result-sum / ls_result-cunt. "平均值 cl_demo_output=>write( ls_result ). cl_demo_output=>display( ).
运行结果:
Open SQL
代码实现:
*&---------------------------------------------------------------------- * 主题四:Open SQL * *&---------------------------------------------------------------------* * 记录时间:23.03.2019 记录人: YALUOO *&---------------------------------------------------------------------* DATA(lv_card_no) = '1000023083'. SELECT a~tag_id, "标签ID card_no, "会员号 tag_name, "会员值 sernumber, "流水号 uname, "用户名 log_date, "备份日期 log_time, "备份时间 message_type, "消息类型 message "消息文本 FROM ztcust_tag AS a INNER JOIN ztcust_tag_log AS b ON a~tag_id = b~tag_id INTO TABLE @DATA(gt_tag) WHERE a~card_no = '1000023312' OR a~card_no = @lv_card_no AND classify = 'brands' . cl_demo_output=>display( gt_tag ).
运行结果: