Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to define internal table using types

Former Member
0 Likes
6,035

Hi good morning,

plz explain me how to define internal table using types.

Thanks

venkat

11 REPLIES 11
Read only

Former Member
0 Likes
2,554

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb366d358411d1829f0000e829fbfe/frameset.htm

The TYPES statement introduces user-defined data types . As with standard data types, you can use them when creating data objects and when assigning types to formal parameters and field symbols. User-defined data types are an essential component of the ABAP/4 type concept .

TYPES:

BEGIN OF TY_MARA,

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MATKL TYPE MARA-MATKL,

END OF TY_MARA.

DATA ITAB TYPE STANDARD TABLE OF TY_MARA WITH HEADER LINE.

To know more about TYPES, Just see the URL.

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/types.htm

Read only

0 Likes
2,554

Hello,

TYPES itabtype {TYPE tabkind OF linetype|

LIKE tabkind OF lineobj}

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

Effect

Defines the type itabtype for an internal table without header line in a program with table type tabkind and line type linetype (if you use a TYPE reference) or the type of the referred object lineobj (if you use a LIKE reference). Internal tables without a header line consist of any number of table lines, each of which has the structure defined by the line type.

You may also define a table key. If you do not, the system creates a generic table type with any key. You can use generic types to specify the type of generic subroutine parameters.

The UNIQUE and NON-UNIQUE additions allow you to specify whether a table with type itabtype may contain two or more records with the same key or not. The following rules apply:

STANDARD TABLE:

The key is always NON-UNIQUE by default. You cannot use the UNIQUE addition with a standard table.

SORTED TABLE:

There is no default setting for sorted tables. If you do not specify UNIQUE or NON-UNIQUE, the system creates a generic table type without a particular uniqueness attribute. You can use generic types to specify the types of generic subroutine parameters.

HASHED TABLE:

There is no default setting for hashed tables. However, you must define a UNIQUE key. The NON-UNIQUE addition is not permitted.

The optional INITIAL SIZE addition allows you to specify how much memory should be allocated to the table when you create it. This corresponds to the OCCURS specification in variant 2 (see also Performance Notes for Internal Tables). The value n is not taken into consideration in the type check.

Example

The following type definitions define tables using the line type STRUC and the key NAME:

TYPES: BEGIN OF STRUC,

NAME(10) TYPE C,

AGE TYPE I,

END OF STRUC.

TYPES: TAB1 TYPE STANDARD TABLE OF STRUC WITH DEFAULT KEY,

TAB2 TYPE SORTED TABLE OF STRUC

WITH NON-UNIQUE KEY NAME,

TAB3 TYPE HASHED TABLE OF STRUC WITH UNIQUE KEY NAME.

Unlike the above types, the following types are generic. This means that you can use them to specify the type of a generic subroutine parameter, but not to create a table object uisng the DATA statement. The only exception to this is that the system allows you to use a generic standard table type in a DATA statement - the type description is completed automatically by the system according to the rules described under DATA.

TYPES: GEN_TAB1 TYPE STANDARD TABLE OF STRUC,

GEN_TAB2 TYPE SORTED TABLE OF STRUC WITH KEY NAME,

GEN_TAB3 TYPE HASHED TABLE OF STRUC.

The following example shows the definition of a sorted table using a LIKE reference to the ABAP Dictionary structure SFLIGHT:

TYPES: FLTAB LIKE SORTED TABLE OF SFLIGHT

WITH NON-UNIQUE KEY CARRID CONNID FLDATE.

Thanks,

Read only

Former Member
0 Likes
2,554

TYPES : begin of t_mara,

matnr type matnr,

end of t_mara.

DATA : i_mara type standard table of t_mara.

or in case of header line

DATA : i_mara type standard table of t_mara with header line.

if you want to define work area.

DATA : wa_mara type t_mara.

Read only

abdul_hakim
Active Contributor
0 Likes
2,554

hi

here is an example.

*defining table type

types ty_mara type standard table of mara.

*defining internal table based on the above type

data itab type ty_mara.

i think you are brand new to ABAP.

Plz go thru the ABAP Documentatin under the below link.

http://help.sap.com

Cheers,

Abdul Hakim

Mark all useful answers..

Read only

sridharreddy_kondam
Active Contributor
0 Likes
2,554

Hi Venkat,

-->First decalre Types which holds all required fileds

  • Declaration of type for final data

TYPES :

BEGIN OF typ_final,

matnr TYPE matnr, " Material number

meins TYPE meins, " Unit of measure

ferth TYPE ferth, " Production/Inspection Memo(Can Code)

werks TYPE werks_d, " Plant

bstrf TYPE bstrf, " Rounding values

maktx TYPE maktx, " Material description

umrez TYPE umrez, " Cans per case

umrez1 TYPE umrez, " Cans per pallet

bmeng TYPE bmeng, " BOM base quantity

stlal TYPE stlal, " alterantive BOM

Pack type KLASSE_D, " Packing Size

Make type KLASSE_D, " Make

flag TYPE char1, " flag for error

END OF typ_final,

-->Then work area

  • Work area to hold all the fields

wa_final TYPE typ_final,

-->Then Internal Table

DATA:

  • Internal table to hold all the required data fields

it_final TYPE STANDARD TABLE OF typ_final

INITIAL SIZE 0.

Regards,

Sridhar

Read only

Former Member
0 Likes
2,554

Hi Venkat,

Please check this out :

TYPES : BEGIN OF ty_itab,

f1,

f2(10),

f3(10),

f4(10),

END OF ty_itab.

DATA : itab TYPE TABLE OF ty_itab WITH HEADER LINE.

Go through this ,

To construct a new structure type in a program, use the following chained TYPES statement:

TYPES: BEGIN OF <structure>,

..............

<ti> ...,

..............

END OF <structure>.

This chained statement creates a structure containing all of the variables between

TYPES BEGIN OF <structure>. a nd TYPES END OF <structure>.

that occur in the data types defined in

TYPES <ti>... .

The components <fi> can be elementary types, reference types, or, if you refer to known complex types, complex themselves. TYPES BEGIN OF... TYPES END OF blocks can also be nested, so you can create deep structure types.

The individual variables within a structure type are addressed in the program with a hyphen between the structure name and component name as follows: <structure>-<fi>.

TYPES: spfli_type TYPE spfli,

surname(20) TYPE c,

BEGIN OF address,

name TYPE surname,

street(30) TYPE c,

city TYPE spfli_type-cityfrom,

END OF address,

town TYPE address-city.

Read only

Former Member
Read only

0 Likes
2,554

Hi,

please look at my codes below.

----


  • Structures and internal tables

----


TYPES: BEGIN OF t_iloa,

tplnr LIKE iloa-tplnr, "Functional location

anlnr LIKE iloa-anlnr, "Main asset number

iloan LIKE iloa-iloan, "Location and account assignment

swerk LIKE iloa-swerk, "Maintenance plant

stort LIKE iloa-stort, "Location of maintenance object

END OF t_iloa.

TYPES: BEGIN OF t_t499s,

werks LIKE t499s-werks, "Plant

stand LIKE t499s-stand, "Location

ktext LIKE t499s-ktext, "Text (40 characters)

END OF t_t499s.

TYPES: BEGIN OF t_equz,

equnr LIKE equz-equnr, "Equipment number

hequi LIKE equz-hequi, "Superior Equipment

iloan LIKE equz-iloan, "Location and account assignment

iwerk LIKE equz-iwerk, "Maintenance Planning Plant

END OF t_equz.

TYPES: BEGIN OF t_equz2,

iwerk LIKE equz-iwerk, "Data origin indicator

END OF t_equz2.

TYPES: BEGIN OF t_anlc,

bukrs LIKE anlc-bukrs, "Company Code

anln1 LIKE anlc-anln1, "Main asset number

anln2 LIKE anlc-anln2, "Asset sub-number

gjahr LIKE anlc-gjahr, "Fiscal year

afabe LIKE anlc-afabe, "Real depreciation area

kansw LIKE anlc-kansw, "Cumulative acquisition

knafa LIKE anlc-knafa, "Accumulated ordinary depreciation

kaafa LIKE anlc-kaafa, "Cumulative unplanned depreciation

answl LIKE anlc-answl, "Transactions for the year

nafav LIKE anlc-nafav, "Proportional accumulated

nafag LIKE anlc-nafag, "Ordinary dep. posted in the cur.year

nafal LIKE anlc-nafal, "Proportional ordinary depreciation

aafav LIKE anlc-aafav, "cumulative unplanned depreciation

aafag LIKE anlc-aafag, "Unplanned depr. posted for the year

nafap LIKE anlc-nafap, "Planned ordinary depr. for the year

aafap LIKE anlc-aafap, "Planned unplanned depr. for the year

END OF t_anlc.

TYPES: BEGIN OF t_iflo,

tplnr LIKE iflo-tplnr, "Functional location

pltxt LIKE iflo-pltxt, "Description of functional location

END OF t_iflo.

TYPES: BEGIN OF t_itob,

anlnr LIKE itob-anlnr, "Main asset number

shtxt LIKE itob-shtxt, "Description of technical object

END OF t_itob.

TYPES: BEGIN OF t_anla,

anln1 LIKE anla-anln1, "Main asset number

anln2 LIKE anla-anln2, "Asset sub-number

ord42 LIKE anla-ord42, "Asset Sub-Class

END OF t_anla.

TYPES: BEGIN OF t_finaltab,

funcloc LIKE iloa-tplnr,

asset_dum LIKE iloa-anlnr,

asset LIKE iloa-anlnr,

parent LIKE iloa-anlnr,

asset_subnum(17) TYPE c,

parent_subnum(17) TYPE c,

accq_cost LIKE anlc-kansw,

acc_dep LIKE anlc-kansw,

asset_book_val LIKE anlc-kansw,

location LIKE t499s-ktext,

description LIKE itob-shtxt,

asset_sub LIKE anla-ord42,

END OF t_finaltab.

TYPES: BEGIN OF t_download,

asset LIKE iloa-anlnr,

parent LIKE iloa-anlnr,

description LIKE itob-shtxt,

asset_sub LIKE anla-ord42,

location LIKE t499s-ktext,

accq_cost LIKE anlc-kansw,

acc_dep LIKE anlc-kansw,

asset_book_val LIKE anlc-kansw,

END OF t_download.

TYPES: BEGIN OF t_equz_dum2,

hequi LIKE equz-hequi, "Superior Equipment

END OF t_equz_dum2.

DATA: it_iloa TYPE SORTED TABLE OF t_iloa WITH HEADER LINE

WITH NON-UNIQUE KEY tplnr anlnr iloan,

it_equz TYPE SORTED TABLE OF t_equz WITH HEADER LINE

WITH NON-UNIQUE KEY equnr hequi,

it_equz2 TYPE STANDARD TABLE OF t_equz2 WITH HEADER LINE,

it_t499s TYPE SORTED TABLE OF t_t499s WITH HEADER LINE

WITH NON-UNIQUE KEY werks stand ktext,

it_anlc TYPE STANDARD TABLE OF t_anlc WITH HEADER LINE,

it_finaltab TYPE STANDARD TABLE OF t_finaltab WITH HEADER LINE,

it_download TYPE STANDARD TABLE OF t_download WITH HEADER LINE,

it_iflo TYPE SORTED TABLE OF t_iflo WITH HEADER LINE

WITH NON-UNIQUE KEY tplnr,

it_itob TYPE STANDARD TABLE OF t_itob WITH HEADER LINE,

it_anla TYPE STANDARD TABLE OF t_anla WITH HEADER LINE.

DATA: it_equz_dum1 TYPE SORTED TABLE OF t_equz WITH HEADER LINE

WITH NON-UNIQUE KEY equnr hequi,

it_equz_dum2 TYPE STANDARD TABLE OF t_equz_dum2 WITH HEADER LINE.

Regards!

P.S. Please award points if found useful:)

Read only

Former Member
0 Likes
2,554

Hello Venkat,

In simple words Internal tables are used to hold data at runtime. Consider I want to print a report for Plant data for Materials , this involves two tables MARA(General Material Data) and MARC(Plant Data for Material).

This is how we proceed.

I declare a structure consisting of the fields i require to display form MARA and MARC.

TYPES :  BEGIN OF tp_final,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         werks TYPE marc-werks,
         pstat TYPE marc-pstat,
         END OF tp_final.

Then refering to this structure I declare an Internal Table and a work area.

DATA : t_report  TYPE STANDARD TABLE OF tp_final,
             wa_report TYPE tp_fianl. 

In can now fetch the data from the tables and put it into this Internal table.

START-OF-SELECTION.

  SELECT a~matnr a~mtart b~werks b~pstat
  INTO TABLE t_report
  FROM mara AS a INNER JOIN marc AS b
         ON a~matnr = b~matnr
      WHERE a~matnr IN s_matnr. " My select option for MATNR

Now i can use the data in the Internal table using

LOOP t_report INTO wa_report.
" Process the data in wa_report.
ENDLOOP.

or

Using

READ TABLE t_report INTO wa_report WITK KEY matnr = ...

Regards,

Arun Sambargi.

Read only

Former Member
0 Likes
2,554

Hi Venkat,

If you want to declare the internl table, always first declare the types and then using that declare your internal table.

Types:

======

TYPES: BEGIN OF <ty_itab>,

MATNR LIKE MARA-MATNR,

FLAG TYPE C,

TYPES: END OF<ty_itab>.

Internal table Decl:

===================

DATA: wa_itab type ty_itab,

gt_itab like standard table of wa_itab.

This is the best way.

Regs,

Venkat Ramanan

Read only

Former Member
0 Likes
2,554

Hai Venkat

see the following Code & Links

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/types.htm

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb366d358411d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb35de358411d1829f0000e829fbfe/frameset.htm

************************************************************************

  • Table Declaration *

************************************************************************

TABLES: mara,

marc,

mard.

************************************************************************

  • Types Declaration *

************************************************************************

TYPES: BEGIN OF typ_mara,

matnr TYPE mara-matnr, "Material Number"

mbrsh TYPE mara-mbrsh, "Industrial Sector"

mtart TYPE mara-mtart, "Material Type"

meins TYPE mara-meins, "Base Unit of Measure"

END OF typ_mara.

TYPES: BEGIN OF typ_makt,

matnr TYPE makt-matnr, "Material Number"

maktx TYPE makt-maktx, "Material Description"

END OF typ_makt.

TYPES: BEGIN OF typ_marc,

matnr TYPE marc-matnr, "Material Number"

werks TYPE marc-werks, "Plant Number"

END OF typ_marc.

TYPES: BEGIN OF typ_mard,

matnr TYPE marc-matnr, "Material Number"

werks TYPE marc-werks, "Plant Number"

lgort TYPE mard-lgort, "Storage Location"

END OF typ_mard.

************************************************************************

  • Intrnal tables Declaration *

************************************************************************

DATA: it_mara TYPE STANDARD TABLE OF typ_mara WITH HEADER LINE.

DATA: it_makt TYPE STANDARD TABLE OF typ_makt WITH HEADER LINE.

DATA: it_marc TYPE STANDARD TABLE OF typ_marc WITH HEADER LINE.

DATA: it_mard TYPE STANDARD TABLE OF typ_mard WITH HEADER LINE.

Have Look Bellow

Variants

1. TYPES typ.

2. TYPES typ(len).

3. TYPES: BEGIN OF rectyp,

...

END OF rectyp.

Effect

The TYPES statement introduces user-defined data types . As with standard data types, you can use them when creating data objects and when assigning types to formal parameters and field symbols. User-defined data types are an essential component of the ABAP/4 type concept .

Variant 1

TYPES f.

Additions

1. ... TYPE typ1

2. ... LIKE f

3. ... TYPE typ1 OCCURS n

4. ... LIKE f OCCURS n

5. ... TYPE LINE OF itabtyp

6. ... LIKE LINE OF itab

7. ... DECIMALS n

Effect

Creates a new type. If the TYPE addition is not used, the new type points to the standard type C .

The type name typ can be up to 30 characters long. Apart from the special characters '(', ')', '+', '.', ',', ':', '-', '<' and '>', you can use any characters. Numbers are allowed, but the name cannot consist of numbers alone.

Recommendations for type names:

Always use a letter as the first character.

Use the underscore as the link in multiple word names (e.g. NEW_PRODUCT ).

Addition 1

... TYPE typ1

Effect

Defines the new type with the type typ1 . typ1 can be one of the predefined types specified below or a type you define yourself with TYPES .

The length (SL) of the type typ is the same as the type typ1 .

Type Description Std len. Initial value

C Text (character) 1 Blank

N Numeric text 1 '00...0'

D Date (YYYYMMDD) 8 '00000000'

T Time (HHMMSS) 6 '000000'

X Hexadecimal 1 X'00'

I Whole number (integer) 4 0

P Packed number 8 0

F Floating point number 8 '0.0'

Example

TYPES NUMBER TYPE I.

This defines the type NUMBER NUMBER with the type I . It can then be used in the program.

Notes

The data type I is the whole number type for the hardware you are using. Its value range is -2*31 to 2*31-1 (-2.147.483.648 to 2.147.483.647).

While type P is used for money amount fields, you should use type I for number fields, index fields, position specifications, etc.

Apart from zero, type F allows you to display positive and negative numbers in the range from 1E-307 to 1E+307 with 15 decimal places. (The ABAP/4 processor uses the floating point operations of the relevant hardware and does not attempt to standardize them.) Floating point literals must be enclosed in quotation marks. The standard output length is 22.

Input in type F fields can be formatted differently:

Decimal number with or without sign, with or without decimal point.

In the form E, where the mantissa is a decimal number and the exponent can be specified with or without a sign. (Examples of floating point literals: '1', '-12.34567', '-765E-04', '1234E5', '12E34', '+12.3E-4', '1E160').

Floating point arithmetic is fast on our hardware platforms. It is ideal when you require a large value range and can take rounding errors into account when making calculations. Such rounding errors can occur when converting from external (decimal) format to internal format (base 2 or 16) or vice-versa (see ABAP/4 number types ).

Addition 2

... LIKE f

Effect

Defines the type typ with the type of the field f . f may be a database field or an already defined internal field.

Example

TYPES TABLE_INDEX_TYP LIKE SY-TABIX.

The type TABLE_INDEX_TYP now points to the type of the field SY-TABIX (index for internal tables).

Note

This addition is useful in a number of cases, since any field type changes are automatically known to the program. Also, any unnecessary and unwanted conversions are not performed.

Addition 3

... TYPE typ1 OCCURS n

Effect

Defines the type of an internal table without a header line. An internal table without a header line consists of any number of table lines that have the same structure as that specified by TYPE .

You fill and process an internal table with statements such as APPEND , READ TABLE , LOOP and SORT .

The OCCURS parameter n specifies how many table lines of storage is required. This storage reservation process does not happen until the first line is inserted in the table. The value n of the OCCURS specification has no effect on type checking, i.e. data objects which have types with different OCCURS specifications are type-compatible.

Example

TYPES: TAB_TYPE TYPE I OCCURS 20.

DATA: TAB TYPE TAB_TYPE,

TAB_WA TYPE I.

TAB_WA = 1.

APPEND TAB_WA TO TAB.

TAB_WA = 2.

APPEND TAB_WA TO TAB.

The internal table TAB now consists of two table entries.

Addition 4

... LIKE f OCCURS n

Effect

Defines the type of an internal table without a header line. This table consists of any number of table lines which have the structure specified by the data object f . Processing is the same as for addition 3.

Example

DATA: BEGIN OF PERSON,

NAME(20),

AGE TYPE I,

END OF PERSON.

TYPES TYPE_PERSONS LIKE PERSON OCCURS 20.

DATA PERSONS TYPE TYPE_PERSONS.

PERSON-NAME = 'Michael'.

PERSON-AGE = 25.

APPEND PERSON TO PERSONS.

PERSON-NAME = 'Gabriela'.

PERSON-AGE = 22.

APPEND PERSON TO PERSONS.

The internal table PERSONS now consists of two table entries.

Addition 5

... TYPE LINE OF itabtyp

Effect

The specified type itabtyp must be the type of an internal table with or without a header line. The statement creates a type corresponding to the line type of the specified table type.

Example

TYPES TAB_TYP TYPE I OCCURS 10.

TYPES MY_TYPE TYPE LINE OF TAB_TYP.

The type MY_TYPE now has the same attributes as a line of the table type TAB_TYP and is thus type I .

Addition 6

... LIKE LINE OF itab

Effect

The data object itab must be an internal table with or without a header line. The statement defines a type which corresponds to the line type of the specified table.

Example

DATA TAB TYPE I OCCURS 10.

TYPES MY_TYPE LIKE LINE OF TAB.

The type MY_TYPE now has the same attributes as the line type of the table TAB and thus has the type I .

Addition 7

... DECIMALS n

Effect

This addition only makes sense with the field type P . When making calculations and outputting data, a field of this type has n decimal places. n must be a value between 0 and 14.

Normally, the attribute for fixed point arithmetic is set with newly created programms. If you switch this attribute off, the DECIMALS -specification is taken into account on output, but not when making calculations. In this case, the programmer must take care that the decimal point is in the right place by multiplying or dividing (COMPUTE ) by the appropriate power of ten.

When making calculations, you should always have fixed point arithmetic switched on. Then, even intermediate results (division!) are calculated with the greatest possible accuracy (31 decimal places).

To decide whether the fixed point type P or the floating point type F is more suitable, see also "ABAP/4 number types ".

Variant 2

TYPES typ(len).

Additions

Similar to variant 1

Effect

Creates the type typ with the length len .

This variant should only be used with the types C , N , P and X . Other types can only be created in the standard length (see table under effect of variant 1).

The permitted lengths depend on the type being pointed to:

Type Permitted lengths

C 1 - 65535

N 1 - 65535

P 1 - 16

X 1 - 65535

Note

For each byte, you can display one character, two decimal digits or two hexadecimal digits. With P fields, one place is reserved for the leading sign, so that a P field of the length 3 can contain 5 digits, while an X field of the length 3 can contain 6 digits. Both have an output length of 6.

Variant 3

TYPES: BEGIN OF rectyp,

...

END OF rectyp.

Effect

Defines the field string type rectyp by grouping together all fields of the type rectyp defined between " BEGIN OF rectyp " and " END OF rectyp ". Each name is prefixed by " rectyp- ".

Example

TYPES: BEGIN OF PERSON,

NAME(20) TYPE C,

AGE TYPE I,

END OF PERSON.

Thanks & regards

Sreenivasulu P