READ TABLE - result 用法

1. Introduction

The value is input in the result.

2.Syntax

... { INTO wa [transport_options] } 
  | { ASSIGNING <fs> [CASTING] } 
  | { REFERENCE INTO dref } 
  | { TRANSPORTING NO FIELDS }. 

3 Effect

Effect:
There are four alternatives for the output behaviour:
The addition into assigns the content of the found line to a work area.
The addition assigning assigns the found line to a field symbol .
The addition reference into creates a reference to the found line in a reference table.
The addition transporting no fields specifies that only the relevant system fields are filled.

4 Demo

4.1 Alternative

… INTO wa [transport_options]

DATA: sflight_tab TYPE SORTED TABLE OF sflight
                  WITH UNIQUE KEY carrid connid fldate,
       sflight_wa  LIKE LINE OF sflight_tab.

DATA subrc TYPE sy-subrc.

SELECT *
       FROM sflight
       INTO TABLE sflight_tab
        WHERE carrid = 'LH'.

subrc = sy-subrc.
WHILE subrc = 0.
   sflight_wa-seatsocc = 0.
  READ TABLE sflight_tab
       INDEX sy-index
        INTO sflight_wa COMPARING seatsocc
                       TRANSPORTING carrid
                                     connid
                                     fldate
                                     seatsocc.
   CASE sy-subrc.
    WHEN 0.
      WRITE: / sflight_wa-carrid, sflight_wa-connid,
         sflight_wa-fldate, sflight_wa-seatsocc COLOR = 6.
      subrc = sy-subrc.
     WHEN 2.
      WRITE: / sflight_wa-carrid, sflight_wa-connid,
         sflight_wa-fldate, sflight_wa-seatsocc COLOR = 1.
      subrc = 0.
     WHEN 4 OR 8.
      EXIT.
  ENDCASE.
ENDWHILE.

4.2 Alternative 2

… ASSIGNING [CASTING]

affect
The found table line is assigned to the field symbol . After the statement READ TABLE, the field symbol points to the table line in the memory. If no table line is found, remains unchanged.

As long as the field symbol points to the line, assigning values to the field symbol changes the line in the internal table. The key fields of sorted tables and hashed tables cannot be manipulated. If they are, the internal table administration is invalidated. Attempts to manipulate these fields leads to an unhandlable exception.

PARAMETERS: p_carrid TYPE sflight-carrid,
            p_connid TYPE sflight-connid,
             p_fldate TYPE sflight-fldate.

DATA sflight_tab TYPE SORTED TABLE OF sflight
                 WITH UNIQUE KEY carrid connid fldate.

FIELD-SYMBOLS <sflight> TYPE sflight.

SELECT *
       FROM sflight
        INTO TABLE sflight_tab
       WHERE carrid = p_carrid AND
              connid = p_connid.

IF sy-subrc = 0.
   READ TABLE sflight_tab
       WITH TABLE KEY carrid = p_carrid
                       connid = p_connid
                       fldate = p_fldate
        ASSIGNING <sflight>.

  IF sy-subrc = 0 AND
      <sflight> IS ASSIGNED.
     <sflight>-price = <sflight>-price * '0.9'.
  ENDIF.
ENDIF.

WRITE:<sflight>-price .

4.3 Alternative 3

… REFERENCE INTO dref

Effect:

A reference to the found table line is made in the data reference variable dref. If no line is found, dref remains unchanged.

By “dereferencing” the data reference, the content of the found table line can be evaluated and changed. The key fields of sorted tables and hashed tables cannot be manipulated, as this invalidates the internal table administration. Attempts to manipulate these fields lead to an untreatable exception.

PARAMETERS: p_carrid TYPE sflight-carrid,
            p_connid TYPE sflight-connid,
             p_fldate TYPE sflight-fldate.

DATA sflight_tab TYPE SORTED TABLE OF sflight
                 WITH UNIQUE KEY carrid connid fldate.

DATA sflight_ref TYPE REF TO sflight.

SELECT *
       FROM sflight
        INTO TABLE sflight_tab
       WHERE carrid = p_carrid AND
              connid = p_connid.

IF sy-subrc = 0.
   READ TABLE sflight_tab
       WITH TABLE KEY carrid = p_carrid
                       connid = p_connid
                       fldate = p_fldate
             REFERENCE INTO sflight_ref.

  IF sy-subrc = 0 AND
    sflight_ref IS NOT INITIAL.
    sflight_ref->price = sflight_ref->price * '0.9'.
  ENDIF.
ENDIF.

write:  sflight_ref->price.
4.4 Alternative 4

… TRANSPORTING NO FIELDS

Effect:

If the addition TRANSPORTING NO FIELDS is used, the statement READ TABLE only checks whether the line that is being searched for exists, and fills the system field sy-subrc and sy-tabix. The system can then no longer access the content of the found fields.

PARAMETERS p_carrid TYPE scarr-carrid. 

DATA: scarr_tab TYPE SORTED TABLE OF scarr 
                WITH UNIQUE KEY carrid, 
       idx TYPE i. 

SELECT * 
       FROM scarr 
        INTO TABLE scarr_tab. 

READ TABLE scarr_tab 
     WITH TABLE KEY carrid = p_carrid 
      TRANSPORTING NO FIELDS. 
IF sy-subrc = 0. 
  idx = sy-tabix. 
ENDIF. 

5 Summary

The addition COMPARING compares the specified components comp1 comp2 … (or the subareas or attributes thereof) in a found line before they are transported with the corresponding components of the work area. If ALL FIELDS is specified, all components are compared. If no NO FIELDS is specified, no components are compared. If the content of the compared components is identical, sy-subrc is set to 0. Otherwise it is set to 2. The found line is assigned to the work area independently of the result of the comparison.

If the addition TRANSPORTING is specified, only the specified components comp1 comp2 … (and their subareas) in the found line are assigned to the corresponding components of the work area (or their subareas). If ALL FIELDS is specified, all the components are assigned.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值