READ TABLE - result

来源:互联网 发布:sql外键约束语句 编辑:程序博客网 时间:2024/06/06 08:42
READ TABLE - result


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



Alternatives:
1. ... INTO wa [transport_options]

2. ... ASSIGNING <fs> [CASTING]

3. ... REFERENCE INTO dref

4. ... TRANSPORTING NO FIELDS



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 <fs>.


The addition REFERENCE INTO creates a reference to the found line in a reference table (as of release 6.10).


The addition TRANSPORTING NO FIELDS specifies that only the relevant system fields are filled.



Note:
Outside of classes, the addition INTO can also be specified together with TRANSPORTING NO FIELDS, but this leads to a warning in the syntax check.



Alternative 1
... INTO wa [transport_options]




Effect:
The content of the found line is assigned to the work area wa. If the work area wa is incompatible with the line type of the internal table, the content of the table line is converted to the data type of the work area according to the conversion rules. If no line is found, wa remains unchanged.

If the additions transport_options are used, the work area wa must be compatible with the line type of the internal table.



Note:
Outside of classes, the specification of INTO wa can be omitted if the internal table has a header line itab with the same name. The statement is then implicitly enhanced by the addition of INTO itab.



Example:
The READ statement uses a WHILE loop to read all lines of the table sflight_tab one after the other using the table index in the work area sflight_wa. Only fields that are also output are transported. Using the COMPARING addition, all flights are selected in which no seats have yet been booked.

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 = 5.
      subrc = 0.
    WHEN 4 OR 8.
      EXIT.
  ENDCASE.
ENDWHILE.



Alternative 2
... ASSIGNING <fs> [CASTING]




Effect:
The found table line is assigned to the field symbol <fs>. After the statement READ TABLE, the field symbol points to the table line in the memory. If no table line is found, <fs> 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.

The optional CASTING addition has the same meaning as if it were specified in the ASSIGN statement without any further additions (since release 7.0). The field symbol must be either fully typed, or typed with one of the generic built-in ABAP types c, n, p or x. The assigned table row is cast to the type of the field symbol. The same exceptions can occur here as with ASSIGN.



Note:
The typing of the field symbol must be compatible with the line type of the internal table.



Example
:Selection of a particular line in the internal table sflight_tab and assigning it to a field symbol <sflight>. After the line has been successfully assigned, the content of a component of the line is changed in the internal table.

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.



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.



Note:
If the static type of the data reference variables is not the generic type DATA, it must be compatible with the internal table.



Example:
Selecting a particular line of the internal table sflight_tab and assigning a reference to the found line to the data reference variable sflight_ref.After the reference has been successfully assigned, the content of a component of the line is changed in the internal table.

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.



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.



Example:
Checking whether a particular line exists in the internal table sflight_carr, and assigning the table index of the found line in sy-tabix to idx.

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.










原创粉丝点击