天天看點

使用DATA -定義一個Internal Table DATA - Defining an Internal Table

DATA - Defining an Internal Table

Variants:

1.

DATA itab TYPE itabtype [WITH HEADER LINE].

2.

DATA itab {TYPE tabkind OF linetype|

              LIKE tabkind OF lineobj}

          WITH [UNIQUE|NON-UNIQUE] keydef           [INITIAL SIZE n] [WITH HEADER LINE].

3.

DATA itab {TYPE TABLE OF linetype|LIKE TABLE OF lineobj}.

4.

DATA itab TYPE RANGE OF type. DATA itab LIKE RANGE OF f.

5.

DATA itab [TYPE linetype|LIKE lineobj] OCCURS n           [WITH HEADER LINE].

6.

DATA: BEGIN OF itab OCCURS n,         ...       END   OF itab [VALID BETWEEN f1 AND f2].

In an ABAP Objects context, a more severe syntax check is performed that in other ABAP areas. See New naming conventions and

LIKE

references to Dictionary Types not allowed.

Effect

Defines an internal table.

To fill and process internal tables, use the statements

INSERT

,

APPEND

,

READ TABLE

,

LOOP

,

SORT

, and so on.

The

OCCURS

or

INITIAL SIZE

parameter (

OCCURS

value) determines the number of lines that are created when the table itself is created. However, the table is extended dynamically on demand. For details, refer to Performance Notes for Internal Tables. The

OCCURS

value, has no other semantic meaning (apart from one exception in the

APPEND SORTED BY

statement). If you do not specify an

INIT IAL SIZE

, the system uses the default value 0.

If you specify

WITH HEADER LINE

, the table is created with a header line, that is, a field with the same name. It has the same type as the line type of the table.

This addition is not allowed in an ABAP Objects context. See Tables with header line not allowed.

Variant 1

DATA itab TYPE itabtype [WITH HEADER LINE].

Effect

itabtype

must be an internal table type that you have already defined using

TYPES

. The statement creates an internal table in the program with this type.

In general, the type specification for the table object must be complete. The exception to this is a standard table, in which the key definition may be missing. In this case, the system automatically uses a default key.

Example

Creating a hashed table by referring to an existing table type:

TYPES: BEGIN OF STRUC, NAME(10), AGE TYPE I, END OF STRUC,

       HTAB TYPE HASHED TABLE OF STRUC WITH UNIQUE KEY NAME.

DATA : PERSONS TYPE HTAB.

Variant 2

DATA itab {TYPE tabkind OF linetype|LIKE tabkind OF lineobj}           WITH [UNIQUE|NON-UNIQUE] keydef

          [INITIAL SIZE n] [WITH HEADER LINE].

Effect

Creates an internal table in the program with the type

tabkind

. Since there are no generic field definitions, you cannot use the table types

ANY TABLE

or

INDEX TABLE

.

The structure of the table lines is defined by the type

linetype

if you use a

TYPE

reference) or by the type of the referred object

lineobj

(when you use a

LIKE

reference).

The same rules apply to the

UNIQUE

and

NON-UNIQUE

additions in the

DATA

statement as in a

TYPES

definition. You may only omit the definition when defining a standard table.

If you do not specify the

INITIAL SIZE

the system uses a default initial size of 0.

Variant 3

DATA itab {TYPE TABLE OF linetype|LIKE TABLE OF lineobj}.

Effect

This is a shortened form of the definition of a standard table. It corresponds to

   DATA itab {TYPE STANDARD TABLE OF linetype|

              LIKE STANDARD TABLE OF lineobj} WITH DEFAULT KEY.

or the old definition (compare variant 4)

   DATA itab {TYPE linetype|LIKE lineobj} OCCURS 0.

Variant 4

DATA itab TYPE RANGE OF type. DATA itab LIKE RANGE OF f.

Additions:

1.

... INITIAL SIZE n

2.

... WITH HEADER LINE

Effect

Creates an internal table

itab

with table type

STANDARD

. The line type is a structure with the following components:

  SIGN(1)   TYPE C

  OPTION(2) TYPE C

  LOW       TYPE type bzw. LIKE f

  HIGH      TYPE type bzw. LIKE f

Addition 1

...INITIAL SIZE n

Effect

The

INITIAL SIZE

specification determines how many table lines are created when the table itself is created. The table is also dynamically expanded as required. For further information, refer to Performance Notes for Internal Tables. The

INITIAL SIZE

value has no semantic meaning (apart from one exception in the ei

APPEND SORTED BY

statement). If you do not specify the

INITIAL SIZE

, the system uses the default value 0.

Addition 2

... WITH HEADER LINE

This addition is not allowed in an ABAP Objects context. See Tables with Header Lines Not Allowed.

Effect

Creates an internal table and a header line for it, that is, a field with the same name as the internal table and the same type as the line type of the internal table.

Variant 5

DATA itab [TYPE linetype|LIKE lineobj] OCCURS n                                        [WITH HEADER LINE].

This variant is not allowed in an ABAP Objects context. See Declaration with

OCCURS

not allowed.

Effect

This variant exists to ensure compatibility with Release 3.x. If you do not specify a line type, the system uses type

C

with length 1. Otherwise, the variant is the same as

   DATA itab {TYPE STANDARD TABLE OF linetype|

              LIKE STANDARD TABLE OF lineobj}

             INITIAL SIZE n [WITH HEADER LINE].

Example

TYPES: BEGIN OF LINE_TYPE,

         NAME(20) TYPE C,

         AGE      TYPE I,

       END   OF LINE_TYPE.

DATA:  PERSONS    TYPE LINE_TYPE OCCURS 20,

       PERSONS_WA TYPE LINE_TYPE.

PERSONS_WA-NAME = 'Michael'.  PERSONS_WA-AGE  = 25.

APPEND PERSONS_WA TO PERSONS.

PERSONS_WA-NAME = 'Gabriela'. PERSONS_WA-AGE  = 22.

APPEND PERSONS_WA TO PERSONS.

The internal table

PERSONS

now contains two entries.

Variant 6

DATA: BEGIN OF itab OCCURS n,         ...

      END   OF itab [VALID BETWEEN f1 AND f2].

This variant is not allowed in an ABAP Objects context. See Declaration with

OCCURS

not allowed.

Effect

Creates an internal table

itab

with type

STANDARD

and a header line. The line type consists of the fields between "

BEGIN OF itab OCCURS n

" and "

END OF itab

".

Use the

VALID BETWEEN f1 AND f2

addition to specify that the components

f1

and

f2

of the internal table

itab

contain a line-based validity interval. You can only use this addition in conjunction with the

PROVIDE

statement.

Example

DATA: BEGIN OF PERSONS OCCURS 20,

        NAME(20),

        AGE TYPE I,

      END   OF PERSONS.

PERSONS-NAME = 'Michael'.

PERSONS-AGE  = 25.

APPEND PERSONS.

PERSONS-NAME = 'Gabriela'.

PERSONS-AGE  = 22.

APPEND PERSONS.

The internal table consists of two entries.

PERSONS

also has a header line (work area), which is an interface between the program and the actual table contents.

繼續閱讀