matinal:SAP ABAP 7.40及以上新语法LINES&DATA&VALUE&CONV用法总结

2023-10-14 15:46:37 浏览数 (1)

1. LINE_EXISTS( )

代码语言:javascript复制
DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT.

SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT UP TO 10 ROWS.

IF LINE_EXISTS( GT_SFLIGHT[ FLDATE = '20200423' ] ).
  WRITE:/ 'Line is Exists'.
ENDIF.

2. CORRESPONDING #( )

代码语言:javascript复制
DATA: BEGIN OF WA_STRUCTURE1,
        COL1 TYPE CHAR20 VALUE'COL1',
        COL2 TYPE CHAR20 VALUE'COL2',
      END OF WA_STRUCTURE1.

DATA: BEGIN OF WA_STRUCTURE2,
        COL2 TYPE CHAR20,
        COL3 TYPE CHAR20,
      END OF WA_STRUCTURE2.

WA_STRUCTURE2 = CORRESPONDING #( WA_STRUCTURE1 ).   "从工作区1移动字段名相同的值到工作区2

CL_DEMO_OUTPUT=>DISPLAY( WA_STRUCTURE2 ).

DATA: GT_TABLE1 LIKE TABLE OF WA_STRUCTURE1,
      GT_TABLE2 LIKE TABLE OF WA_STRUCTURE2.

GT_TABLE1 = VALUE #(                                    "GT_TABLE1插入两条数据
                         ( COL1 = 'S1R1C1' COL2 = 'S1R1C2')
                         ( COL1 = 'S1R2C1' COL2 = 'S1R2C2')
                        ).

GT_TABLE2 = VALUE #( ( COL2 = 'S2R1C1' COL3 = 'S2R1C2') ).  ""GT_TABLE2插入的第一条数据会覆盖第二条
GT_TABLE2 = VALUE #( ( COL2 = 'S2R2C1' COL3 = 'S2R2C2') ).

GT_TABLE2 = CORRESPONDING #( GT_TABLE1 ).     "从GT_TABLE1移动字段名相同的列到GT_TABLE2,会覆盖GT_TABLE2的旧数据

CL_DEMO_OUTPUT=>DISPLAY( GT_TABLE2 ).

3. VALUE #( )

代码语言:javascript复制
"变量看ABAP Keyword Documentation

"结构
DATA: BEGIN OF WA_STRUCTURE,
        COL1 TYPE CHAR20,
        COL2 TYPE CHAR20,
      END OF WA_STRUCTURE.

WA_STRUCTURE = VALUE #( COL1 = 'COL1' COL2 = 'COL2' ).

CL_DEMO_OUTPUT=>DISPLAY( WA_STRUCTURE ).

"表
DATA: GT_TABLE LIKE TABLE OF WA_STRUCTURE.

GT_TABLE = VALUE #(
                    ( COL1 = 'ROW1COL1' COL2 = 'ROW1COL2')
                    ( COL1 = 'ROW2COL1' COL2 = 'ROW2COL2')
                  ).

CL_DEMO_OUTPUT=>DISPLAY( GT_TABLE ).

4. DATA()

代码语言:javascript复制
"变量
DATA: LV_VAR1 TYPE CHAR20 VALUE 'Variable'.

DATA(LV_VAR2) = LV_VAR1.
CL_DEMO_OUTPUT=>DISPLAY( LV_VAR2 ).

"结构
"方法一:
DATA: BEGIN OF WA_STRUCTURE1,
        COL1 TYPE CHAR20 VALUE'COL1',
        COL2 TYPE CHAR20 VALUE'COL2',
      END OF WA_STRUCTURE1.

DATA(WA_STRUCTURE2) = WA_STRUCTURE1.
CL_DEMO_OUTPUT=>DISPLAY( WA_STRUCTURE2 ).

"方法二:
SELECT SINGLE * FROM SFLIGHT INTO @DATA(WA_SFLIGHT).
CL_DEMO_OUTPUT=>DISPLAY( WA_SFLIGHT ).

"表
"方法一:
DATA: GT_TABLE1 LIKE TABLE OF WA_STRUCTURE1.

GT_TABLE1 = VALUE #(
                    ( COL1 = 'ROW1COL1' COL2 = 'ROW1COL2')
                    ( COL1 = 'ROW2COL1' COL2 = 'ROW2COL2')
                  ).

DATA(GT_TABLE2) = GT_TABLE1.
CL_DEMO_OUTPUT=>DISPLAY( GT_TABLE2 ).

"方法二:
SELECT * FROM SFLIGHT INTO TABLE @DATA(GT_SFLIGHT) UP TO 20 ROWS.
CL_DEMO_OUTPUT=>DISPLAY( GT_SFLIGHT ).

5. ||

代码语言:javascript复制
DATA: LV_VAR    TYPE CHAR20 VALUE 'Variable',
      LV_STRING TYPE STRING.

LV_STRING = |{ LV_VAR } is Same as { LV_VAR }|.

WRITE:/ LV_STRING.

6. LINES()

代码语言:javascript复制
SELECT * FROM SFLIGHT INTO TABLE @DATA(GT_SFLIGHT).

DATA(LV_ROWCOUNT) = LINES( GT_SFLIGHT ).

WRITE:/ LV_ROWCOUNT.

7. CONV #()

代码语言:javascript复制
DATA LV_TEXT TYPE C LENGTH 200 VALUE'1111'.

"方法一:
DATA(LV_STRING1) = CONV STRING( LV_TEXT ).

"方法二:
DATA: LV_STRING2 TYPE STRING.
LV_STRING2 = CONV #( LV_TEXT ).

WRITE:/ LV_STRING1,
      / LV_STRING2.

"Other
DATA: LV_NUM TYPE N LENGTH 20.
DATA(LV_INT) = CONV I( LV_TEXT ).
DATA(LV_NUMC) = CONV NUM( LV_TEXT ).

WRITE:/ LV_INT,
      / LV_NUMC.

0 人点赞