Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

ABAP Data/Internal

Tables
ITP 321
Anthony Borquez & Jim Graver

Examples of Data Types and Objects


This example shows how to declare elementary data
objects with reference to predefined ABAP types.
PROGRAM demo_elementary_data_objects.
DATA text1(20) TYPE c.
DATA text2
TYPE string.
DATA number
TYPE i.
text1 = 'The number'.
number = 100.
text2 = 'is an integer.'.
WRITE: text1, number, text2.

This example shows how to declare local elementary


data types within a program.

REPORT demo_types_statement.
TYPES mytext(10) TYPE c.
TYPES myamount
TYPE p DECIMALS 2.
DATA text
TYPE mytext.
DATA amount
TYPE myamount.
text = ' 4 / 3 = '.
amount = 4 / 3 .
WRITE: text, amount.

This example shows how to declare structures.


REPORT demo_structure.
TYPES: BEGIN OF name,
title(5)
TYPE c,
first_name(10) TYPE c,
last_name(10) TYPE c,
END OF name.
TYPES: BEGIN OF mylist,
client
TYPE name,
number
TYPE i,
END OF mylist.
DATA list TYPE mylist.
list-client-title = 'Lord'.
list-client-first_name = 'Howard'.
list-client-last_name = 'Mac Duff'.
list-number = 1.
WRITE list-client-title.
WRITE list-client-first_name.
WRITE list-client-last_name.
WRITE / 'Number'.
WRITE list-number.

Reading Data
Clause

Description

SELECT <result>

The SELECT clause defines the structure of the data you want to read,
that is, whether one line or several, which columns you want to read,
and whether identical entries are acceptable or not.

INTO <target>

The INTO clause determines the target area <target> into which the
selected data is to be read.

FROM <source>

The FROM clause specifies the database table or view <source> from
which the data is to be selected. It can also be placed before the INTO
clause.

WHERE <cond>

The WHERE clause specifies which lines are to be read by specifying


conditions for the selection.

GROUP BY <fields>

The GROUP-BY clause produces a single line of results from groups


of several lines. A group is a set of lines with identical values for each
column listed in <fields>.

HAVING <cond>

The HAVING clause sets logical conditions for the lines combined
using GROUP BY.

ORDER BY <cond>

The ORDER-BY clause defines a sequence <fields> for the lines


resulting from the selection.

Reading certain columns of a single line:


DATA WA TYPE SPFLI.
SELECT SINGLE CARRID CONNID CITYFROM CITYTO
INTO
CORRESPONDING FIELDS OF WA
FROM
SPFLI
WHERE CARRID EQ 'LH' AND CONNID EQ '0400'.
IF SY-SUBRC EQ 0.
WRITE: / WA-CARRID, WA-CONNID, WACITYFROM, WA-CITYTO.
ENDIF.

Reading particular columns of more than one line:


DATA: ITAB TYPE STANDARD TABLE OF SPFLI,
WA LIKE LINE OF ITAB.
SELECT CARRID CONNID CITYFROM CITYTO
INTO
CORRESPONDING FIELDS OF TABLE ITAB
FROM
SPFLI
WHERE CARRID EQ 'LH'.
IF SY-SUBRC EQ 0.
LOOP AT ITAB INTO WA.
WRITE: / WA-CARRID, WA-CONNID, WACITYFROM, WA-CITYTO.
ENDLOOP.
ENDIF.

Reading all columns of more than one line:

DATA WA TYPE SPFLI.


SELECT *
INTO
CORRESPONDING FIELDS OF WA
FROM
SPFLI
WHERE CARRID EQ 'LH'.
WRITE: / SY-DBCNT,
WA-CARRID, WA-CONNID, WACITYFROM, WA-CITYTO.
ENDSELECT.

Specifying Columns Dynamically


DATA: ITAB TYPE STANDARD TABLE OF SPFLI,
WA LIKE LINE OF ITAB.
DATA: LINE(72) TYPE C,
LIST LIKE TABLE OF LINE(72).
LINE = ' CITYFROM CITYTO '.
APPEND LINE TO LIST.
SELECT DISTINCT (LIST)
INTO CORRESPONDING FIELDS OF TABLE
ITAB
FROM SPFLI.
IF SY-SUBRC EQ 0.
LOOP AT ITAB INTO WA.
WRITE: / WA-CITYFROM, WA-CITYTO.
ENDLOOP.
ENDIF.

Specifying Internal Tables


When you read several lines of a database table, you can place them in an
internal table. To do this, use the following in the INTO clause:
SELECT ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE
<itab>[PACKAGE SIZE <n>] ...
The same applies to the line type of <itab>, the way in which the data for a line
of the database table are assigned to a table line, and the CORRESPONDING
FIELDS addition as for flat work areas (see above).
The internal table is filled with all of the lines of the selection. When you use
INTO, all existing lines in the table are deleted. When you use APPENDING;
the new lines are added to the existing internal table <itab>. With APPENDING,
the system adds the lines to the internal table appropriately for the table type.
Fields in the internal table not affected by the selection are filled with initial
values.

This example shows how to define an internal table.


PROGRAM demo_internal_table.
TYPES: BEGIN OF mytext,
number TYPE i,
name(10) TYPE c,
END OF mytext.
TYPES mytab TYPE STANDARD TABLE OF mytext WITH
DEFAULT KEY.
DATA text TYPE mytext.
DATA itab TYPE mytab.
text-number = 1. text-name = 'John'.
APPEND text TO itab.
text-number = 2. text-name = 'Paul'.
APPEND text TO itab.
text-number = 3. text-name = 'Ringo'.
APPEND text TO itab.
text-number = 4. text-name = 'George'.
APPEND text TO itab.
LOOP AT itab INTO text.
WRITE: / text-number,text-name.
ENDLOOP.

Flat structure as target area

DATA WA TYPE SPFLI.


SELECT *
INTO
WA
FROM
SPFLI.
WRITE: / WA-CARRID ...
ENDSELECT.

This example uses a flat structure with the same data


type as the database table SPFLI as the target area in a
SELECT loop. Within the loop, it is possible to address
the contents of the individual columns.

DATA SPFLI TYPE SPFLI.


SELECT *
FROM
SPFLI.
WRITE: / SPFLI-CARRID ...
ENDSELECT.

Internal table as target area


DATA: BEGIN OF WA,
CARRID
TYPE SPFLI-CARRID,
CONNID
TYPE SPFLI-CONNID,
CITYFROM TYPE SPFLI-CITYFROM,
CITYTO
TYPE SPFLI-CITYTO,
END OF WA,
ITAB LIKE SORTED TABLE OF WA
WITH NON-UNIQUE KEY CITYFROM CITYTO.
SELECT CARRID CONNID CITYFROM CITYTO
INTO
CORRESPONDING FIELDS OF TABLE ITAB
FROM
SPFLI.
IF SY-SUBRC EQ 0.
WRITE: / SY-DBCNT, 'Connections'.
SKIP.
LOOP AT ITAB INTO WA.
WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WACITYTO.
ENDLOOP.
ENDIF.

Reading packets into an internal table

DATA: WA
TYPE SPFLI,
ITAB TYPE SORTED TABLE OF SPFLI
WITH UNIQUE KEY CARRID CONNID.
SELECT CARRID CONNID
FROM
SPFLI
INTO
CORRESPONDING FIELDS OF TABLE ITAB
PACKAGE SIZE 3.
LOOP AT ITAB INTO WA.
WRITE: / WA-CARRID, WA-CONNID.
ENDLOOP.
SKIP 1.
ENDSELECT.

Single fields as target area:

DATA:

AVERAGE TYPE P DECIMALS 2,


SUM
TYPE P DECIMALS 2.
SELECT AVG( LUGGWEIGHT ) SUM( LUGGWEIGHT )
INTO
(AVERAGE, SUM)
FROM
SBOOK.
WRITE: / 'Average:', AVERAGE,
/ 'Sum
:', SUM.

Using aliases:
DATA: BEGIN OF LUGGAGE,
AVERAGE TYPE P DECIMALS 2,
SUM
TYPE P DECIMALS 2,
END OF LUGGAGE.
SELECT AVG( LUGGWEIGHT ) AS AVERAGE
SUM( LUGGWEIGHT ) AS SUM
INTO
CORRESPONDING FIELDS OF LUGGAGE
FROM
SBOOK.
WRITE: / 'Average:', LUGGAGE-AVERAGE,
/ 'Sum
:', LUGGAGE-SUM.

Adding single lines

TABLES SPFLI.
DATA WA TYPE SPFLI.
WA-CARRID = 'LH'.
WA-CITYFROM = 'WASHINGTON'.
...
INSERT INTO SPFLI VALUES WA.
WA-CARRID = 'UA'.
WA-CITYFROM = 'LONDON'.
...
INSERT SPFLI FROM WA.
SPFLI-CARRID = 'LH'.
SPFLI-CITYFROM = 'BERLIN'.
...
INSERT SPFLI

Udpating Data
TABLES SPFLI.
DATA WA TYPE SPFLI.
MOVE 'AA' TO WA-CARRID.
MOVE '0064' TO WA-CONNID.
MOVE 'WASHINGTON' TO WA-CITYFROM.
...
UPDATE SPFLI FROM WA.
MOVE 'LH' TO SPFLI-CARRID.
MOVE '0017' TO SPFLI-CONNID.
MOVE 'BERLIN' TO SPFLI-CITYFROM.
...
UPDATE SPFLI.

You might also like