ABAP 7.4 及更高版本 [11]:: Concatenation, ALPHA and ITA

2021-11-07 21:05发布


          点击此处--->   EasySAP.com 群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)

In this post, you will learn about below ABAP statements.

  1. ALPHA

  2. Concatenation

  3. LINE_EXISTS

  4. LINES

ALPHA

ALPHA 向数字字符串添加前导零或删除它们。这仅适用于字符串、c 或 n 数据类型。通常,SAP 为许多字段(如客户编号、订单编号、发票编号)存储带有前导零的数据,但在输出屏幕上打印数据时,前导零会被删除。

用于去除前导零

For Removing Leading Zeros

DATA : var_int TYPE char10 VALUE '0000098765'.
DATA(var_ext) = |{ var_int ALPHA = OUT }|.

"This code replaces use of CONVERSION_EXIT
DATA var_ext TYPE char10.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' 
  EXPORTING
    input  = var_int
  IMPORTING
    output = var_ext.


For Adding Leading Zeros

DATA : var_ext TYPE char10 VALUE '12345'.
DATA(var_int) = |{ var_ext ALPHA = IN }|.

"This code replaces use of CONVERSION_EXIT
DATA var_int TYPE char10.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' 
  EXPORTING
    input  = var_ext
  IMPORTING
    output = var_int.

Note that the variables var_int and var_ext can be the same variable.

var_num = |{ var_num ALPHA = OUT }|.   "Remove leading zeros
var_num = |{ var_num ALPHA = IN }|.    "Add leading zeros

This can also be used as an operand.

WRITE : |{ var_int ALPHA = OUT }|.

Note that this syntax only works for conversion routine ALPHA.

Concatenation

We all know concatenation. So let us just look at examples.

"Single Variable 
CONCATENATE 'The order' order_num 'is created successfully'
  INTO DATA(msg)
  SEPARATED BY space.
  
"Multiple Variables
CONCATENATE 'The order' order_num 'is created on' sy-datum 'at' sy-uzeit 'by' sy-uname
  INTO DATA(msg)
  SEPARATED BY space.

This can now be written in a way which is more readable as below.

"Single Variable 
DATA(msg) = |The order { order_num } is created successfully|.

"Multiple Variables
DATA(msg) = |The order { order_num } is created on { sy-datum } at { sy-uzeit } by { sy-uname } |.

As we are talking about concatenation, you should know below method as well.

DATA(created_at) = sy-datum && ':' && sy-uzeit.


LINE_EXISTS

LINE_EXISTS is used to check whether a record exists in internal table or not.

This can also be done using READ TABLE as below.

"Select data from sbook table
SELECT * FROM sbook INTO TABLE @DATA(it_sbook).

"Code in focus
READ TABLE it_sbook TRANSPORTING NO FIELDS WITH KEY carrid = 'AA'.
IF sy-subrc EQ 0.
  WRITE: 'Booking found for flight AA'.
ELSE.
  WRITE: 'Booking not found for flight AA'.
ENDIF.

The same can be written using the new read syntax as below.

TRY.
    DATA(ls_sook) = it_sbook[ carrid = 'AA' ].
    WRITE: 'Booking found for flight AA'.
  CATCH cx_sy_itab_line_not_found.
    WRITE: 'Booking not found for flight AA'.
ENDTRY.

But the best way when you only have to check for record existence is to use LINE_EXISTS.

IF LINE_EXISTS( it_sbook[ carrid = 'AA' ] ).
  WRITE: 'Booking found for flight AA'.
ELSE.
  WRITE: 'Booking not found for flight AA'.
ENDIF.

LINES


This needs no explaination.

"Old way
DESCRIBE TABLE it_sbook LINES DATA(lv_count).

"New way
DATA(lv_count) = LINES( it_sbook ). 

"Use in operand positions
WRITE: / 'No. of bookings: ', LINES( it_sbook ).

As you can notice, advantage of using these expressions is that the code becomes more readable and we can avoid using some of the 

helper variables.

赞赏支持