New Keyword in ABAP--REF VALUE BASE FOR
阿新 • • 發佈:2022-04-05
轉自 (10條訊息) ABAP--新語法--New Keyword in ABAP--第二天--REF VALUE BASE FOR_尤其是十月的風的部落格-CSDN部落格
CL_DEMO_OUTPUT=>DISPLAY
PS:自己測試是DISPLAY後的內表不能帶表頭。
REF 使用 REF 定義引用變數,用來代替 CREATE DATA
1 *---------------------------------------------------------------------* 2 * 在使用 REF時,不需要提前宣告變數,也不用指定型別, 3 * 型別預設會與被指向的變數保持一致4 *---------------------------------------------------------------------* 5 TYPES: BEGIN OF TY_MARA, 6 MATNR TYPE MARA-MATNR, 7 MTART TYPE MARA-MTART, 8 MATKL TYPE MARA-MATKL, 9 TEXT1 TYPE CHAR50, 10 END OF TY_MARA. 11 12 DATA(LW_MARA) = VALUE TY_MARA( MATNR = 'MATERIAL-001' 13 MTART = 'FOOD' 14 MATKL = '1010' 15 TEXT1 = 'FIRST' ). 16 17 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ). 18 DATA(LV_REF) = REF #( LW_MARA ). 19 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ).20 LV_REF->* = VALUE #( MATNR = 'MATERIAL-002' 21 MTART = 'WATR' 22 MATKL = '1020' ). 23 24 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ). 25 LV_REF->TEXT1 = 'SECONDE'. 26 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ).
VALUE
新語法中,可以使用 VALUE 作為賦值語句,主要用來為內表、結構、變數等物件賦值
1 *---------------------------------------------------------------------* 2 * 引數型別引用同NEW關鍵字,在VALUE子句中,欄位可以分開賦值, 3 * 也可以使用結構整體賦值,為內表賦值時, 4 * 需要用小括號將一行的資料包在一起 5 *---------------------------------------------------------------------* 6 TYPES: BEGIN OF TY_MARA, 7 MATNR TYPE MARA-MATNR, 8 MTART TYPE MARA-MTART, 9 MATKL TYPE MARA-MATKL, 10 TEXT1 TYPE CHAR50, 11 END OF TY_MARA. 12 13 DATA: LT_MARA TYPE TABLE OF TY_MARA. 14 DATA(LW_MARA) = VALUE TY_MARA( MATNR = 'MATERIAL-001' 15 MTART = 'FOOD' 16 MATKL = '1020' 17 TEXT1 = 'FIRST material'). 18 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ). 19 LT_MARA = VALUE #( ( LW_MARA ) 20 ( MATNR = 'MATERIAL-002' 21 MTART = 'FOOD' 22 MATKL = '1020' 23 TEXT1 = 'SECOND material') 24 ). 25 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_MARA ).
1 *---------------------------------------------------------------------* 2 * 此外,VALUE語句作為結構時,可以直接在特定語句中使用 3 *---------------------------------------------------------------------* 4 TYPES: BEGIN OF TY_DATA, 5 CHECKBOX TYPE C, 6 VALUE(4) TYPE C, 7 END OF TY_DATA. 8 DATA LT_DATA TYPE TABLE OF TY_DATA. 9 APPEND VALUE #( VALUE = 'TEST' ) TO LT_DATA. 10 11 MODIFY LT_DATA FROM VALUE #( CHECKBOX = 'X' ) TRANSPORTING CHECKBOX WHERE CHECKBOX IS INITIAL. 12 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_DATA ).
1 *---------------------------------------------------------------------* 2 * 在內表賦值語句中使用 BASE 時,內表原有的資料會被保留, 3 * 新增條目會被追加到內表中,效果同 Append Line 4 *---------------------------------------------------------------------* 5 DATA LT_MARA TYPE TABLE OF TY_MARA. 6 LT_MARA = VALUE #( ( LW_MARA ) ). 7 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_MARA ). 8 LT_MARA = VALUE #( BASE LT_MARA 9 ( MATNR = 'MATERIAL-002' 10 MTART = 'FOOD' 11 MATKL = '1030' 12 TEXT1 = 'SECOND material' ) 13 ). 14 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_MARA ).
BASE
在使用 VALUE 作為賦值語句時,預設會覆蓋原有的資料,通過BASE子句基於原有資料進行賦值
1 *---------------------------------------------------------------------* 2 * 在結構賦值語句中使用 BASE 時,原有欄位的資料會被保留, 3 * 但是當在VALUE語句中對同一欄位再次賦值時,該欄位資料會被覆蓋 4 *---------------------------------------------------------------------* 5 TYPES: BEGIN OF TY_MARA, 6 MATNR TYPE MARA-MATNR, 7 MTART TYPE MARA-MTART, 8 MATKL TYPE MARA-MATKL, 9 TEXT1 TYPE CHAR50, 10 END OF TY_MARA. 11 12 DATA(LW_MARA) = VALUE TY_MARA( MATNR = 'MATERIAL-001' 13 MTART = 'FOOD' ). 14 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ). 15 LW_MARA = VALUE #( BASE LW_MARA 16 MTART = 'WATR' 17 MATKL = '1020' 18 TEXT1 = 'FIRST material'). 19 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LW_MARA ).
1 *---------------------------------------------------------------------* 2 * 在內表賦值語句中使用 BASE 時,內表原有的資料會被保留, 3 * 新增條目會被追加到內表中,效果同 Append Line 4 *---------------------------------------------------------------------* 5 DATA LT_MARA TYPE TABLE OF TY_MARA. 6 LT_MARA = VALUE #( ( LW_MARA ) ). 7 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_MARA ). 8 LT_MARA = VALUE #( BASE LT_MARA 9 ( MATNR = 'MATERIAL-002' 10 MTART = 'FOOD' 11 MATKL = '1030' 12 TEXT1 = 'SECOND material' ) 13 ). 14 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_MARA ).
*---------------------------------------------------------------------* * 使用 BASE 語句時,儘量保持前後結構一致, * 在使用不同的結構時,可能不報錯但資料會錯位 *---------------------------------------------------------------------*
FOR 在內表賦值語句中,可以使用FOR語句從其他內表中批量引入資料並處理
1 *---------------------------------------------------------------------* 2 * 在FOR語句中允許將結構作為整體直接進行賦值,但是結構必須與錶行相容, 3 * 可以用於從內表中獲取特定條件的資料 4 *---------------------------------------------------------------------* 5 TYPES: BEGIN OF TY_DATA, 6 MATNR TYPE MARA-MATNR, 7 MTART TYPE MARA-MTART, 8 MATKL TYPE MARA-MATKL, 9 TEXT1 TYPE CHAR50, 10 END OF TY_DATA. 11 12 DATA LT_DATA TYPE TABLE OF TY_DATA. 13 14 TYPES: TY_TABLE TYPE TABLE OF TY_DATA WITH DEFAULT KEY. 15 16 LT_DATA = VALUE #( 17 ( MATNR = 'MATRIAL-001' 18 MTART = 'WATR' 19 MATKL = '1020' 20 TEXT1 = 'FIRST material' ) 21 ( MATNR = 'MATRIAL-002' 22 MTART = 'FOOD' 23 MATKL = '1030' 24 TEXT1 = 'SECOND material' ) 25 ( MATNR = 'MATRIAL-003' 26 MTART = 'WATR' 27 MATKL = '1040' 28 TEXT1 = 'THIRD material' ) 29 ). 30 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_DATA ). 31 32 DATA(LT_FOR) = VALUE TY_TABLE( 33 FOR LW_DATA IN LT_DATA 34 WHERE ( MTART = 'WATR' ) 35 ( LW_DATA ) 36 ). 37 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_FOR ).
1 *---------------------------------------------------------------------* 2 * 如果結構與錶行不相容,可以巢狀使用CORRESPONDING語句進行賦值, 3 * 如下例( CORRESPONDING具體用法可參考後續示例 ) 4 *---------------------------------------------------------------------* 5 TYPES: BEGIN OF TY_DATA, 6 MATNR TYPE MARA-MATNR, 7 MTART TYPE MARA-MTART, 8 MATKL TYPE MARA-MATKL, 9 TEXT1 TYPE CHAR50, 10 END OF TY_DATA. 11 12 DATA LT_DATA TYPE TABLE OF TY_DATA. 13 14 LT_DATA = VALUE #( 15 ( MATNR = 'MATRIAL-001' 16 MTART = 'WATR' 17 MATKL = '1020' 18 TEXT1 = 'FIRST material' ) 19 ( MATNR = 'MATRIAL-002' 20 MTART = 'FOOD' 21 MATKL = '1030' 22 TEXT1 = 'SECOND material' ) 23 ( MATNR = 'MATRIAL-003' 24 MTART = 'WATR' 25 MATKL = '1040' 26 TEXT1 = 'THIRD material' ) 27 ). 28 29 TYPES: BEGIN OF TY_DATA_NEW, 30 MATNR TYPE MARA-MATNR, 31 MATKL TYPE MARA-MATKL, 32 END OF TY_DATA_NEW. 33 34 TYPES: TY_TABLE_NEW TYPE TABLE OF TY_DATA_NEW WITH DEFAULT KEY. 35 36 DATA(LT_FOR) = VALUE TY_TABLE_NEW( 37 FOR LW_DATA IN LT_DATA 38 WHERE ( MTART = 'WATR' ) 39 ( CORRESPONDING #( LW_DATA ) ) 40 ). 41 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_FOR ).
1 *---------------------------------------------------------------------* 2 * 結構欄位過多時,一般建議使用整體賦值,如果少部分欄位存在特殊的賦值邏輯 3 * 可巢狀使用VALUE以及BASE語句進行處理,將例1的邏輯轉換如下: 4 *---------------------------------------------------------------------* 5 TYPES: BEGIN OF TY_DATA, 6 MATNR TYPE MARA-MATNR, 7 MTART TYPE MARA-MTART, 8 MATKL TYPE MARA-MATKL, 9 TEXT1 TYPE CHAR50, 10 END OF TY_DATA. 11 12 DATA LT_DATA TYPE TABLE OF TY_DATA. 13 14 TYPES: TY_TABLE TYPE TABLE OF TY_DATA WITH DEFAULT KEY. 15 16 LT_DATA = VALUE #( 17 ( MATNR = 'MATRIAL-001' 18 MTART = 'WATR' 19 MATKL = '1020' 20 TEXT1 = 'FIRST material' ) 21 ( MATNR = 'MATRIAL-002' 22 MTART = 'FOOD' 23 MATKL = '1030' 24 TEXT1 = 'SECOND material' ) 25 ( MATNR = 'MATRIAL-003' 26 MTART = 'WATR' 27 MATKL = '1040' 28 TEXT1 = 'THIRD material' ) 29 ). 30 31 DATA(LT_FOR) = VALUE TY_TABLE( 32 FOR LW_DATA IN LT_DATA 33 INDEX INTO LV_INDEX 34 WHERE ( MTART = 'WATR' ) 35 ( VALUE #( BASE LW_DATA 36 MATKL = CONV #( LV_INDEX ) ) ) 37 ). 38 CALL METHOD CL_DEMO_OUTPUT=>DISPLAY( LT_FOR ).
CL_DEMO_OUTPUT=>DISPLAY
PS:自己測試是DISPLAY後的內表不能帶表頭。
REF
使用 REF 定義引用變數,用來代替 CREATE DATA