sap 7.40 新特性介绍和运用 -01 Data Declarations

Data Declarations

 

In ABAP you have many operand positions, where the value of the operand is changed by the statement. The most typical of these “write positions” is the left hand side lhs of an assignment.

 

lhs = rhs.

 

But of course there are more. The data objects you can use at these write positions are either writable formal parameters of the procedure you are working in or variables declared with DATA in front of the statement.

 

In many cases the variables filled by a statement are helper variables that you only need close to the statement. For each of  these helper variables you had to write a data declaration with the DATA statement and of course it was your task to give the variable an adequate type.

 

Well, the operand type of most write positions is statically fixed and well known to the compiler. And this is why ABAP can offer inline data declarations with Release 7.40. The ingredients are so called declaration positions (write positions with fully known operand type)  and the new declaration operator DATA(…).

 

Let’s look at some examples.

 

Declaration of a lhs-variable for a simple assignment

 

Before 7.40

 

DATA text TYPE string.
text = `…`.

 

With 7.40

 

DATA(text) = `…`.

DATA(text) = 'adsd'.
WRITE :text.

Declaration of table work areas

 

Before 7.40

 

DATA wa like LINE OF itab.
LOOP AT itab INTO wa.  
  ...
ENDLOOP.

 

With 7.40

 

LOOP AT itab INTO DATA(wa).  
  ...
ENDLOOP.

DATA:itab TYPE TABLE OF ekko.
SELECT * FROM ekko INTO TABLE @ITAB UP TO 10 ROWS.
LOOP AT itab INTO data(wa).
WRITE:/ wa-ebeln.
ENDLOOP.

Declaration of a helper variable

 

Before 7.40

 

DATA cnt TYPE i.
FIND … IN … MATCH COUNT cnt.

 

With 7.40

 

FIND … IN … MATCH COUNT DATA(cnt).

 

Declaration of a result

 

Before 7.40

 

DATA xml TYPE xstring.
CALL TRANSFORMATION … RESULT XML xml.

 

With 7.40

 

CALL TRANSFORMATION … RESULT XML DATA(xml).

 

Declaration of actual parameters

 

Before 7.40

 

DATA a1 TYPE …

DATA a2 TYPE …

oref->meth( IMPORTING p1 = a1

            IMPORTING p2 = a2

            … )

 

With 7.40

 

oref->meth( IMPORTING p1 = DATA(a1)

            IMPORTING p2 = DATA(a2)

            … ).

 

Declaration of reference variables for factory methods

 

Before 7.40

 

DATA ixml           TYPE REF TO if_ixml.
DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document       TYPE REF TO if_ixml_document.

 

ixml           = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document       = ixml->create_document( ).

 

With 7.40

 

DATA(ixml)           = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document)       = ixml->create_document( ).

 

This example is my favorite. When working with class libraries as the iXML-Library you don’t have to care about the data type of the reference variables too much any more. You simply create them inline and use them. As you will see in the 7.40 version of the ABAP Example Library, this feature has facilitated my writings of example programs considerably.

 

 

Field Symbols

 

For field symbols there is the new declaration operator FIELD-SYMBOL(…) that you can use at exactly three declaration positions.

 

ASSIGN … TO FIELD-SYMBOL(<fs>).

 

LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).

ENDLOOP.

 

READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) …

 

I guess it is clear to you what happens here.

 

Outlook

 

In my upcoming blogs I will make use of inline declarations when introducing other new features. Be prepared for code like this:

 

TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.

DATA(itab) = VALUE t_itab( ( 1 ) ( 2 ) ( 3 ) ).

 

Yes, this is ABAP 7.40 …

DATA wa TYPE mara.

SELECT SINGLE *
       FROM mara
       INTO  @wa  .
  cl_demo_output=>display( wa ).

 文章参考:https://blogs.sap.com/2013/05/23/abap-news-for-release-740-inline-declarations/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值