SELECT - target

SELECT - target

Syntax

... { INTO
      { {[CORRESPONDING FIELDS OF] wa}|(dobj1, dobj2, ...)} }
  | { INTO|APPENDING
         [CORRESPONDING FIELDS OF] TABLE itab [PACKAGE SIZE n] } ... .


Alternatives:

1. ... INTO [CORRESPONDING FIELDS OF] wa

2. ... INTO (dobj1, dobj2 ... )

3. ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab [PACKAGE SIZE n]

Effect

target specifies which data objects the result set of a SELECT or FETCH statement is assigned to. You can either specify a single work area wa or a list of data objects dobj1, dobj2, ... after INTO, or you can specify an internal table itab after INTO or APPENDING. If the result set is empty, the objects remain unchanged.

Note

For the specification of target, there is no dynamic variant that corresponds to the other additions. Instead, you can work with dynamically created data objects (see the example for CREATE DATA).

Alternative 1

... INTO [CORRESPONDING FIELDS OF] wa


Effect

For wa, you can specify a data object that meets the prerequisites. If the result set consists of a single line, this line is assigned to wa. If the result set has multiple lines, SELECT must be followed by an ENDSELECT statement; the result set is assigned to the work area wa line-by-line and can be evaluated in the loop. After ENDSELECT, the work area wa contains the line that was assigned last. If used in the FETCH statement, a line is extracted at the current cursor position.

The lines of the result set are assigned as follows, based on the column specification after SELECT:

Specifying * without the CORRESPONDING FIELDS addition

If all columns are read with * and CORRESPONDING FIELDS is not specified, the line of the result set is assigned left-justified and unconverted according to the structure of the result set. Non-affected parts of wa retain their original content. To enable type-dependent access to the components of the result set after the assignment, the work area wa must be structured like the result set.

All other combinations

If the result set consists of a single column specified explicitly after SELECT or a single aggregate expression, wa can be an elementary data object or a structure. If the result set consists of multiple columns, it must be a structure and the following rules apply:

  • If the CORRESPONDING FIELDS addition is not specified, wa must contain enough components and the contents of the columns are assigned to the components of wa from left to right in the order specified after SELECT.
  • If the CORRESPONDING FIELDS addition is specified, only those contents of columns for which there are identically-named components in wa are assigned to them; alternative column names are taken into account. Columns and aggregate expressions that appear multiple times can only be assigned using alternative column names if CORRESPONDING FIELDS is specified. If a column name appears multiple times and no alternative column name was specified, the last column listed is assigned.

The assignment rules apply to the individual assignments.

Example

In this example, four columns of the result set are read into four correspondingly named components of a work area.

DATA wa TYPE spfli.

SELECT carrid connid cityfrom cityto
       FROM spfli
       INTO CORRESPONDING FIELDS OF wa.
  WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
ENDSELECT.

Alternative 2

... INTO (dobj1, dobj2, ... )


Effect

If the result set consists of several columns or aggregate expressions specified explicitly after SELECT, you can specify a list of elementary data objects dobj1, dobj2, ... (in brackets and separated by commas) after INTO. You must specify the same number of elementary data objects dobj as there are columns in the result set. The contents of the columns in the result set are assigned to the data objects from left to right, according to the order specified after SELECT. The assignment rules apply to the individual assignments.

If the result set consists of one line, the columns are assigned from that line. If the result set contains multiple lines, SELECT must be followed by an ENDSELECT statement; the columns of the result set are assigned to the data objects line-by-line and they can be evaluated in a loop. If used in the FETCH statement, the columns of the line are extracted at the current cursor position.

Example

In this example, four columns of the result set are read into four individually specified columns of a structure. Unlike in the previous example, the runtime environment does not compare names here.

DATA wa TYPE spfli.

SELECT carrid connid cityfrom cityto
       FROM spfli
       INTO (wa-carrid, wa-connid, wa-cityfrom, wa-cityto).
  WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
ENDSELECT.

Alternative 3

... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab [PACKAGE SIZE n]


Effect

If the result set consists of multiple lines, an internal table itab of any table type can be specified after INTO or APPENDING. The row type of the internal table must meet the prerequisites.

The result set is inserted into the internal table itab line-by-line; a sorting process is executed in the case of a sorted table. If INTO is used, the internal table is initialized before the first line is inserted. Previous lines remain intact if APPENDING is used.

Before any assignment of a line of the result set, an initial row of the internal table itab is created and the line of the result set is assigned to this row. When assigning a line of the result set to a row of the internal table with or without CORRESPONDING FIELDS, the same rules apply as when assigning to an individual work area wa (see above).

If the PACKAGE SIZE addition is not used, all lines of the result set are inserted in the internal table itab and the ENDSELECT statement must not be specified after SELECT.

If you specify the PACKAGE SIZE addition, all lines of the result set for SELECT are processed in a loop, which must be closed with ENDSELECT. The lines are inserted in the internal table itab in packages of n lines. n must be a type i data object that contains the number of lines. If the value of n is smaller than 0, an exception that cannot be handled occurs. If n is equal to 0, all lines of the result set are inserted in the internal table itab. If used in the FETCH statement, n lines are extracted from the current cursor position.

If INTO is used, the internal table is initialized before each insertion and, in the SELECT loop, it only contains the lines of the current package. If APPENDING is used, a further package is added to the existing rows of the internal table for each SELECT loop or for each extraction using FETCH.

After ENDSELECT, the content of itab is not defined if INTO is used - that is, the table can either contain the lines of the last package or it can be initial. If APPENDING is used, the content of itab retains the state of the last loop pass.

Notes

In the case of an internal table with a unique key, an exception that cannot be handled occurs if an attempt is made to create a duplicate entry.

  • If the addition PACKAGE SIZE is specified together with FOR ALL ENTRIES, it is not passed to the database system, but is applied to the result set on the application server, after all selected rows have been read.
Example

In this example, all columns of the result set are read into an internal table whose row type is a nested structure with the same construction as the result set. Note that in practice, the column carrid exists twice in the result set with the same content and, after the assignment, this content is stored redundantly in the columns struc1-carrid and struc2-carrid of the internal table.

DATA: BEGIN OF wa,
        struc1 TYPE scarr,
        struc2 TYPE spfli,
      END OF wa.

DATA itab LIKE SORTED TABLE OF wa
          WITH UNIQUE KEY table_line.

SELECT *
       FROM scarr
         INNER JOIN spfli ON scarr~carrid = spfli~carrid
       INTO TABLE itab.

LOOP AT itab INTO wa.
  WRITE: / wa-struc1-carrid,
           wa-struc1-carrname,
           wa-struc2-connid.
ENDLOOP.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值