1 / 19

ABAP Data/Internal Tables

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.

atalaya
Télécharger la présentation

ABAP Data/Internal Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ABAP Data/Internal Tables ITP 321 Anthony Borquez & Jim Graver

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

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

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

  5. Clause Description Reading Data 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.

  6. Reading certain columns of a single line: • DATA WA TYPE SPFLI. • SELECT SINGLE CARRID CONNID CITYFROM CITYTOINTO   CORRESPONDING FIELDS OF WAFROM   SPFLIWHERE  CARRID EQ 'LH' AND CONNID EQ '0400'. • IF SY-SUBRC EQ 0.  WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.ENDIF.

  7. Reading particular columns of more than one line: • DATA: ITAB TYPE STANDARD TABLE OF SPFLI,      WA LIKE LINE OF ITAB. • SELECT CARRID CONNID CITYFROM CITYTOINTO   CORRESPONDING FIELDS OF TABLE ITABFROM   SPFLIWHERE  CARRID EQ 'LH'. • IF SY-SUBRC EQ 0.  LOOP AT ITAB INTO WA.    WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.  ENDLOOP.ENDIF.

  8. Reading all columns of more than one line: • DATA WA TYPE SPFLI. • SELECT *INTO   CORRESPONDING FIELDS OF WAFROM   SPFLIWHERE  CARRID EQ 'LH'. •   WRITE: / SY-DBCNT,            WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO. • ENDSELECT.

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

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

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

  12. Flat structure as target area • DATA WA TYPE SPFLI. • SELECT * INTO   WAFROM   SPFLI. •   WRITE: / WA-CARRID ... • ENDSELECT.

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

  14. 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 CITYTOINTO   CORRESPONDING FIELDS OF TABLE ITABFROM   SPFLI. • IF SY-SUBRC EQ 0.  WRITE: / SY-DBCNT, 'Connections'.  SKIP.  LOOP AT ITAB INTO WA.    WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.  ENDLOOP.ENDIF.

  15. Reading packets into an internal table • DATA: WA   TYPE SPFLI,      ITAB TYPE SORTED TABLE OF SPFLI                WITH UNIQUE KEY CARRID CONNID. • SELECT CARRID CONNIDFROM   SPFLIINTO   CORRESPONDING FIELDS OF TABLE ITAB       PACKAGE SIZE 3. •   LOOP AT ITAB INTO WA.    WRITE: / WA-CARRID, WA-CONNID.  ENDLOOP. •   SKIP 1. • ENDSELECT.

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

  17. 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 SUMINTO   CORRESPONDING FIELDS OF LUGGAGEFROM   SBOOK. • WRITE: / 'Average:', LUGGAGE-AVERAGE,       / 'Sum    :', LUGGAGE-SUM.

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

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

More Related