本文列出了ABAP新語法的一些使用方式,供你們學習參考。html
*&---------------------------------------------------------------------- * 主題一:內聯聲明 * 語法: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 * *&---------------------------------------------------------------------* * 記錄時間: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 ).