25
ABAP Internal table declaration - Various methods of creating internal data structures and tables Declaring internal tables is an essential part of writing ABAP code as this is where most of the data retrieved from database tables will be stored. During the select statement you retrieve data from a database table into an internal table (multiple rows) or a work area or header line (single row). When building an internal table you first need a row/structure type definition which defines all the field types of the table. For this you could use any table or structure already created within the data dictionary (SE11). For example if you wanted to create a table with a row structure like EKPO you would simply use the following ABAP code: data: it_ekpo TYPE STANDARD TABLE OF EKKO INITIAL SIZE 0. This creates an internal table called it_data with the same structure as EKKO. The initial size 0 simple tells the program not to reserve any space for it upfront and allocate it as and when filled. Also if you wanted an internal 1 line work area with this structure you could also reference the dictionary object using the following statement: “Creates a local work area called wa_ekpo data: wa_ekpo TYPE EKPO. “or this code creates a local work based on an existing itab wa_ekpo1 like line of it_ekko. "or the TABLES statement creates a local work area called ekpo TABLES: EKPO. Define structure or table using declarations within your ABAP What if you don’t want your table to include all the fields from EKPO but just 2 such as EBELN and EBELP or if you wanted fields from different dictionary tables. Well you then have 2 choices you either create a new dictionary object with the fields you want with SE11 and then reference it as above or you build the table using ABAP declarations within your report. Firstly you need to define the structure of the table which is

ABAP Internal Table Declaration

Embed Size (px)

DESCRIPTION

ABAP Internal Table Declaration

Citation preview

Page 1: ABAP Internal Table Declaration

ABAP Internal table declaration - Various methods of creating internal data structures and tables

Declaring internal tables is an essential part of writing ABAP code as this is where most of the data retrieved from database tables will be stored. During the selectstatement you retrieve data from a database table into an internal table (multiple rows) or a work area or header line (single row). 

When building an internal table you first need a row/structure type definition which defines all the field types of the table. For this you could use any table or structure already created within the data dictionary (SE11). For example if you wanted to create a table with a row structure like EKPO you would simply use the following ABAP code:

data: it_ekpo TYPE STANDARD TABLE OF EKKO INITIAL SIZE 0.

This creates an internal table called it_data with the same structure as EKKO. The initial size 0 simple tells the program not to reserve any space for it upfront and allocate it as and when filled. Also if you wanted an internal 1 line work area with this structure you could also reference the dictionary object using the following statement:

“Creates a local work area called wa_ekpodata: wa_ekpo TYPE EKPO.“or this code creates a local work based on an existing itab

wa_ekpo1 like line of it_ekko."or the TABLES statement creates a local work area called ekpo

TABLES: EKPO.

Define structure or table using declarations within your ABAP What if you don’t want your table to include all the fields from EKPO but just 2 such as EBELN and EBELP or if you wanted fields from different dictionary tables. Well you then have 2 choices you either create a new dictionary object with the fields you want with SE11 and then reference it as above or you build the table using ABAP declarations within your report. 

Firstly you need to define the structure of the table which is basically just a list of fields and their type. To do this use the following ABAP code:

TYPES: BEGIN OF t_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, “more fields can be added here

END OF t_ekpo.

The 'TYPES: BEGIN OF' statement defines the start of this declaration and the t_ekpo is the name of the structure and can be anything you want. The 'END OF t_ekpo.' statement defines the end of the structure definition. You now have a

Page 2: ABAP Internal Table Declaration

structure called t_ekpo that can be used in the same way as the dictionary object EKKO was used above:

data: it_ekpo TYPE STANDARD TABLE OF t_ekpo, wa_ekpo TYPE t_ekpo.

Build internal table and work area from existing structure and adding additional fields A structure can be built based on an existing dictionary structure such as EKKO with a few extra fields being added to it using the following ABAP syntax.

TYPES: BEGIN OF t_repdata.INCLUDE STRUCTURE ekko.

TYPES: ebelp TYPE ekpo-ebelp, werks TYPE ekpo-werks.

TYPES: END OF t_repdata.TYPES: END OF t_repdata.DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0,

"itab wa_repdata TYPE t_repdata. "work area (header

line)

Old way of declaring internal tables and structures Please note this information is just so you can understand code which was originally written when this was the way to do it. Do not use this method when creating new reports as it does not work in some of the newer technologies such as objects and web dynpro so if you are new to ABAP you might as well get used to the new method from the start, plus the new method is easier to understand anyway. 

In the early days of ABAP development there was a slightly different concept to declaring a multi line table and its single lined header / work area. Basically when you create a table using this old method a header line is automatically created with the same name and depending on how you referenced it, and the statement used would determine the appropriate element to be accessed. For example CLEAR would initialise the header element, REFRESH would initialise the table element, MOVE-CORRESPONDING moved the header element, APPEND adds to the table element etc 

When declaring using this old method it also declares the TYPES and DATA described above in one go just using DATA and with the addition of the OCCURS statement or not to determine if you want a table with a header line or just a header line/ work area.

DATA: BEGIN OF tab_ekpo, "Work area declaration / Header line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp,

Page 3: ABAP Internal Table Declaration

END OF tab_ekpo.DATA: BEGIN OF tab_ekpo OCCURS 0, "Table declaration with header

line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF tab_ekpo.

If you wanted to reference a dictionary object (SE11) such as EKPO then you would use the following syntax:

Data: it_tabekpo LIKE ekpo OCCURS 0, "Table with header line wa_tabekpo LIKE ekpo. “Work area

Example report showing full ABAP syntax and use of table declarations described above*&-------------------------------------------------------------**& Report ZTYPES **& **&-------------------------------------------------------------*REPORT ZTYPES .

*Table declaration (new method) "USE THIS WAY!!!TYPES: BEGIN OF t_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF t_ekpo.DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itab wa_ekpo TYPE t_ekpo, "work area (header line) wa_ekpo1 LIKE LINE OF it_ekpo.

* Build internal table and work area from existing table with* additional fieldsTYPES: BEGIN OF t_repdata. INCLUDE STRUCTURE ekko. "could be an itab such as tab_ekpoTYPES: ebelp TYPE ekpo-ebelp, werks TYPE ekpo-werks.TYPES: END OF t_repdata.DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab wa_repdata TYPE t_repdata. "work area (header line)

* Table declaration (old method)DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header line ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF tab_ekpo.Data: it_tabekpo LIKE ekpo OCCURS 0, "Table with header line wa_tabekpo LIKE ekpo. "Work area

* Build internal table from existing dictionary structure (old method)DATA: it_datatab LIKE ekko OCCURS 0.

***************************************************************************START-OF-SELECTION.

Page 4: ABAP Internal Table Declaration

START-OF-SELECTION.

*Select data in itabSELECT ebeln ebelp from EKPO into table it_ekpo.*Process data within itab using LOOP statement Loop at it_ekpo into wa_ekpo. write: wa_ekpo-ebeln.* processing......endloop.

*Select data in itab declared the old waySELECT ebeln ebelp from EKPO into table tab_ekpo.Loop at tab_ekpo. write: tab_ekpo-ebeln.* processing......endloop.

Page 5: ABAP Internal Table Declaration

Build a Dynamic Internal Table in 1 - 2 - 3  Steps

AUTHOR: SUSMITHA SUSAN THOMASDATE: 11.06.2013

________________________________________________________________________________________________________________

  Creating a dynamic table is not a big deal. Once you understand the concept, it is as simple as 1-2-3. So if your picture about dynamic internal table is something complicated, you need to change that mindset first before proceeding to learn about it. 

Dynamic internal table is an internal table with variable rows and columns which can be defined during run time. The different attributes that can be defined at run time includes the field name, column attributes, data type, width, reference table and reference fields. There are just three main steps involved in it. 

1. Create the structure of the table2. Create the dynamic internal table with this structure3. Populate the dynamic table.

And then you have your dynamic internal table just like any other internal table. 

Pre-requisites 

Before we start to create a dynamic internal table, we must have an idea of field symbols and data references. If you know them, skip this session and go directly to dynamic table creation. 

Field Symbols 

Field symbols are like pointers in C. (Technically, they are not the same, using this analogy just to get the picture). They just point to fields. They are like place holders or pseudonyms or alias for other fields. 

When you assign a field symbol to a variable, whatever you do to that field symbol, it will be instantly reflected in the variable it points to (or in ABAP Language, the variable it is assigned to) 

 

Syntax 

       Declarations            FIELD-SYMBOLS: <fs1>.

Page 6: ABAP Internal Table Declaration

 

        Assignment 

                        ASSIGN f TO <fs1>. 

       

 

It is like two containers that are connected with a pipe. As long as the field symbol is assigned to the object, whatever you put in the field symbol, it will flow to the data object. And whatever is in the data object can be accessed through the field symbol. 

Any changes you make in <fs1> will be instantly reflected in the data object f. 

<fs1> = 10. 

Now variable f will have the value 10. 

It can point to any object determined at run time. And for the same reason, it can adopt to any data type or size at run time depending on the object it is pointing to. 

Data References 

Data references are used to create data objects dynamically. 

Syntax. 

Declaration.

  DATA <dref> TYPE REF TO DATA.

Creation at run time

CREATE DATA <dref> TYPE <type>|LIKE <obj>.

Page 7: ABAP Internal Table Declaration

  Now the data type of the object <Dref> will be <type> or the data type of object <obj>

To access the contents of the data object to which a data reference is pointing, you must deference it. This is where we need field symbols.

ASSIGN <dref>->* TO <FS>.

Now whatever statements are performed on Field symbols, it will be reflected in the object dref that we created at run time. Accessing field symbol <Fs> is equivalent to accessing data reference object dref. 

Ok. So now we are all set to create dynamic table. 

Dynamic Table Creation 

Let’s take an example to learn the concept. 

Suppose I have an internal table IT_DEMO containing three columns – vendor name (vend), Month(month), Amount Due(amt). 

VENDOR MONTH AMOUNT DUEV100 Jan 100V100 Feb 250V200 Feb 216V300 Feb 550V200 Mar 200V300 Mar 310V100 Apr 145V100 May 350V200 May 600V300 May 200V400 May 800 

 

I need to create something like a transpose for this table dynamically. The output should look like this. 

VENDOR JAN13 FEB13 MAR13 APR13 MAY13V100 100 250 145 350V200 216 200 600V300 550 310 200

Page 8: ABAP Internal Table Declaration

V400 800 

 

Step 1 – Create Structure. 

We create structure using field catalog.If you have used ALV, you must be familiar with field catalog and its fields. 

Some of the components of field catalog structure is field name, table name, column text, output length. These are the attributes that can be defined for each field of the dynamic internal table we are creating. 

Declare a structure of type lvc_s_fcat. 

Declare an internal table of type lvc_t_fcat  (The line type of this internal table is  lvc_s_fcat). 

Field Catalog Declaration. 

  gw_dyn_fcat       TYPE lvc_s_fcat,gt_dyn_fcat         TYPE lvc_t_fcat. 

** This would create structure Vendor Jan13 Feb13 Mar13 ....DATA : gv_pos TYPE i.DATA : fname TYPE string. 

* Declaring the first column - vendorgv_pos = gv_pos + 1.gw_dyn_fcat-fieldname = 'VEND'.          “ Field Namegw_dyn_fcat-outputlen = 5.                     “ Output Lengthgw_dyn_fcat-tabname   = 'IT_DEMO'.    “ Internal Table Name gw_dyn_fcat-coltext   = 'VENDOR'.        “ Header text for the columngw_dyn_fcat-col_pos   = gv_pos.             “ Column position gw_dyn_fcat-key = 'X'.                            “ Key attribute is set for the field vend. APPEND gw_dyn_fcat TO gt_dyn_fcat.clear gw_dyn_fcat.

*Loop through the internal table and creating a column for every distinct month in the internal tableLOOP AT it_zdemo INTO wa_zdemo.gv_pos = gv_pos + 1.CONCATENATE wa_zdemo-month '13' INTO fname.read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.if sy-subrc NE 0.gw_dyn_fcat-fieldname = wa_zdemo-month.gw_dyn_fcat-tabname   = 'IT_DEMO'.

Page 9: ABAP Internal Table Declaration

gw_dyn_fcat-coltext   = fname.gw_dyn_fcat-outputlen = 10.gw_dyn_fcat-col_pos   = gv_pos.APPEND gw_dyn_fcat TO gt_dyn_fcat.endif.clear gw_dyn_fcat.ENDLOOP. 

Now gt_dyn_fcat contains the structure of the table. 

Step 2 – Create Dynamic Table. 

Dynamic internal tables can be created using method CREATE_DYNAMIC_TABLE in class CL_ALV_TABLE_CREATE.Importing parameter is the field catalog created in step 1 and the exporting parameter is the dynamic table. The dynamic table must have been declared as dynamic data using data reference. 

DATA : gt_dyn_table  TYPE REF TO data.,      gw_line          TYPE REF TO data,  gw_line1         TYPE REF TO data, 

* Create a dynamic internal table with this structure.

CALL METHOD cl_alv_table_create=>create_dynamic_tableEXPORTINGi_style_table             = 'X'it_fieldcatalog           = gt_dyn_fcatIMPORTINGep_table                  = gt_dyn_tableEXCEPTIONSgenerate_subpool_dir_full = 1OTHERS                    = 2.

Now we have the dynamic table gt_dyn_table.  To access the data, we use field symbols. 

We shall create two work areas gw_line and gw_line1 like line of gt_dyn_table. (or like line of <gfs_dyn_table> which is the field-symbol assigned to gt_dyn_table). The work area gw_line will be accessed by field-symbol <gfs_line> and gw_line1 will be accessed by field symbol <gfs_line1>. 

IF sy-subrc EQ 0.* Assign the new table to field symbolASSIGN gt_dyn_table->* TO <gfs_dyn_table>.* Create dynamic work area for the dynamic table

Page 10: ABAP Internal Table Declaration

CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.ASSIGN gw_line->* TO <gfs_line>.ASSIGN gw_line1->* TO <gfs_line1>.ENDIF. 

           Note : Field symbols were declared previously with the following statement.

FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,<gfs_dyn_table> TYPE STANDARD TABLE,<fs1>.

 

Step 3 – Populating the dynamic table 

Each cell in the dynamic table is accessed using field symbols. We use the field symbol <fs1> to point to each component of work area <gfs_line> (alias gw_line). The values are moved to the work area, component by component through this field symbol <fs1>. 

LOOP AT it_zdemo INTO wa_zdemo.* Avoid duplicate entries for key field VEND. READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('VEND') = wa_zdemo-vend.IF sy-subrc = 0.CONTINUE.ENDIF.* The component vendor of the workarea is assigned to <fs1>ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.* The value for vendor in the current loop wa_zdemo-vend  flows to the work area through <fs1><fs1> = wa_zdemo-vend.UNASSIGN <fs1>.* Move the amount for that vendor for each month in the dynamic table. Each month in the dynamic table can be looped using the field catalog table.LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.IF gw_dyn_fcat-fieldname = 'VEND'. “ Move amount only for month fields, not vendorCONTINUE.ENDIF.READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTOwa_zdemo1.IF sy-subrc = 0.ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.<fs1> = wa_zdemo1-amt.UNASSIGN <fs1>.ENDIF.

Page 11: ABAP Internal Table Declaration

clear : wa_zdemo1.ENDLOOP.* Append the dynamic work area to the dynamic table. APPEND <gfs_line> TO <gfs_dyn_table>.CLEAR: <gfs_line>.clear: wa_zdemo, wa_zdemo1.ENDLOOP. 

Now the dynamic table has been created and has been populated with the values based on the contents of the initial internal table. 

Drawbacks of Dynamic Internal table Programs with many dynamic internal tables are less readable. They are less secure since errors cannot be detected by syntax check, but

only at runtime Performance is not as good as static internal table.

 

 

Given below is the complete code for the above program. 

REPORT zdynamic_table.

*Author ; Susmitha Susan Thomas

TYPES : BEGIN OF gfirst_typ,

vend(6) TYPE c,

month(5) TYPE c,

amt TYPE i.

TYPES : END OF gfirst_typ.

DATA : it_zdemo TYPE TABLE OF gfirst_typ.

DATA : wa_zdemo LIKE LINE OF it_zdemo,

wa_zdemo1 LIKE LINE OF it_zdemo.

DATA : gv_pos TYPE i.

DATA : fname TYPE string.

* Dynamic Table Declarations

DATA : gt_dyn_table  TYPE REF TO data,

gw_line       TYPE REF TO data,

gw_line1       TYPE REF TO data,

gw_dyn_fcat         TYPE lvc_s_fcat,

gt_dyn_fcat         TYPE lvc_t_fcat.

* Field Symbols Declarations

 

Page 12: ABAP Internal Table Declaration

FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,

<gfs_dyn_table> TYPE STANDARD TABLE,

<fs1>.

* Populate the initial input table. Usually this input table contents will be populated at run time, which

raises the requirement of dynamic table. The table contents are filled here for illustration purpose.

wa_zdemo-vend = 'V100'.

wa_zdemo-month = 'JAN'.

wa_zdemo-amt = 100.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.

wa_zdemo-month = 'FEB'.

wa_zdemo-amt = 200.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.

wa_zdemo-month = 'FEB'.

wa_zdemo-amt = 200.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.

wa_zdemo-month = 'FEB'.

wa_zdemo-amt = 150.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.

wa_zdemo-month = 'MAR'.

wa_zdemo-amt = 250.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.

wa_zdemo-month = 'MAR'.

wa_zdemo-amt = 300.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.

wa_zdemo-month = 'APR'.

wa_zdemo-amt = 200.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.

wa_zdemo-month = 'MAY'.

wa_zdemo-amt = 100.

APPEND wa_zdemo TO it_zdemo.

Page 13: ABAP Internal Table Declaration

wa_zdemo-vend = 'V200'.

wa_zdemo-month = 'MAY'.

wa_zdemo-amt = 50.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.

wa_zdemo-month = 'MAY'.

wa_zdemo-amt = 125.

APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V400'.

wa_zdemo-month = 'MAY'.

wa_zdemo-amt = 475.

APPEND wa_zdemo TO it_zdemo.

Write : / 'Initial Internal Table'.

WRITE :/.

write :/(6) 'Vendor'.

write : (12) 'Month' .

write : (3) 'Amt' .

LOOP AT it_zdemo INTO wa_zdemo.

WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.

ENDLOOP.

** This would create structure Vendor Jan13 Feb13 Mar13 etc ....

gv_pos = gv_pos + 1.

gw_dyn_fcat-fieldname = 'VEND'.

gw_dyn_fcat-outputlen = 5.

gw_dyn_fcat-tabname   = 'IT_DEMO'.

gw_dyn_fcat-coltext   = 'VENDOR'.

gw_dyn_fcat-col_pos   = gv_pos.

gw_dyn_fcat-key = 'X'.

gw_dyn_fcat-key_sel = 'X'.

APPEND gw_dyn_fcat TO gt_dyn_fcat.

clear gw_dyn_fcat.

 

* Loop through the internal table creating a column for every distinct month in the internal tableLOOP AT it_zdemo INTO wa_zdemo.

gv_pos = gv_pos + 1.

CONCATENATE wa_zdemo-month '13' INTO fname.

read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.

if sy-subrc NE 0.

gw_dyn_fcat-fieldname = wa_zdemo-month.

gw_dyn_fcat-tabname   = 'IT_DEMO'.

Page 14: ABAP Internal Table Declaration

gw_dyn_fcat-coltext   = fname.

gw_dyn_fcat-outputlen = 10.

gw_dyn_fcat-col_pos   = gv_pos.

APPEND gw_dyn_fcat TO gt_dyn_fcat.

endif.

clear gw_dyn_fcat.

ENDLOOP.

 

** Create a dynamic internal table with this structure.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

i_style_table             = 'X'

it_fieldcatalog           = gt_dyn_fcat

IMPORTING

ep_table                  = gt_dyn_table

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS                    = 2.

IF sy-subrc EQ 0.

* Assign the new table to field symbolASSIGN gt_dyn_table->* TO <gfs_dyn_table>.

* Create dynamic work area for the dynamic tableCREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.

CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.

ASSIGN gw_line->* TO <gfs_line>.

ASSIGN gw_line1->* TO <gfs_line1>.

ENDIF.

* Populate the dynamic table

LOOP AT it_zdemo INTO wa_zdemo.

* Avoid duplicate entries for key field PART.READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('VEND') = wa_zdemo-vend.

IF sy-subrc = 0.

CONTINUE.

ENDIF.

ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.

<fs1> = wa_zdemo-vend.

UNASSIGN <fs1>.

LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.

Page 15: ABAP Internal Table Declaration

IF gw_dyn_fcat-fieldname = 'VEND'.

CONTINUE.

ENDIF.

READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-

fieldname INTOwa_zdemo1.

IF sy-subrc = 0.

ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.

<fs1> = wa_zdemo1-amt.

UNASSIGN <fs1>.

ENDIF.

clear : wa_zdemo1.

ENDLOOP.

APPEND <gfs_line> TO <gfs_dyn_table>.

CLEAR: <gfs_line>.

clear: wa_zdemo, wa_zdemo1.

ENDLOOP.

 

WRITE :/.

Write : / 'Dynamic Internal Table'.

WRITE :/.

LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.

WRITE (10) : gw_dyn_fcat-coltext.

ENDLOOP.

WRITE :/.

LOOP AT <gfs_dyn_table> INTO <gfs_line>.

LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.

ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.

WRITE : <fs1>.

ENDLOOP.

WRITE :/      .

ENDLOOP.                                                                                                                                                                          

                         

 

 

Output 

Page 16: ABAP Internal Table Declaration

 

 

Just a minor addition to the above program. Now if you want to display this as an ALV grid, you need to create

another field catalog. Since the field catalog created above is not compatible with the field catalog passed as

parameter for ALV display.

 

data : gw_alv_fieldcat     type slis_fieldcat_alv,

         gt_alv_fieldcat     type slis_t_fieldcat_alv.

         data:    lv_pos type i.

loop at gt_dyn_fcat into gw_dyn_fcat.

       lv_pos = lv_pos + 1.

       gw_alv_fieldcat-fieldname     = gw_dyn_fcat-fieldname.

       gw_alv_fieldcat-tabname       = gw_dyn_fcat-tabname.

       gw_alv_fieldcat-seltext_l     = gw_dyn_fcat-coltext.

       gw_alv_fieldcat-outputlen     = gw_dyn_fcat-outputlen.

       gw_alv_fieldcat-col_pos       = lv_pos.

       gw_alv_fieldcat-do_sum        = gw_dyn_fcat-do_sum.

       gw_alv_fieldcat-emphasize     = gw_dyn_fcat-emphasize.

       gw_alv_fieldcat-key           = gw_dyn_fcat-key.

       gw_alv_fieldcat-no_out        = gw_dyn_fcat-no_out.

       append gw_alv_fieldcat to gt_alv_fieldcat.

endloop.

   call function 'REUSE_ALV_GRID_DISPLAY'

Page 17: ABAP Internal Table Declaration

     exporting

       i_callback_program = sy-repid

       it_fieldcat        = gt_alv_fieldcat

       i_default          = 'X'

       i_save             = 'A'

     tables

       t_outtab           = <gfs_dyn_table>.

 

Basic rules for programming in ABAP  Categories: SAP 

1. Every statment must end with period(.)

2. ABAP statement are not case sensitive, eg :- data, DATA, Data

3. ABAP is space sensitive.

4. Strings are enclosed in single quotes.

5. For providing comments * or ” is used

* -> Entire Line (This should be provided at start of line)

” -> Middle of line (Can be given at any location on line)

CLRT + < is shortcut used for commenting multiple line at once

CLRT + > to uncommnet set of lines

Page 18: ABAP Internal Table Declaration

Working with INTERNAL TABLES

Append 2 tables with identical structureAll rows: append lines of itab1 to itab2

Subset of rows: append lines of itab1 from <rowno> to <rowno> to itab2

Check if there are any entries in an internal table

If you don't need to know the number of entries in the table, but only wants to know if there are any entries at all 

Page 19: ABAP Internal Table Declaration

use:

if itab[] is initial.........

instead of using describe table.

Copy an internal table to another internal tableNote: The tabels must have exactly the structure

itab2[] = itab2[]

Delete linesDeleting a single line

read table itab with key name = 'My name'into wa_itab.if sy-subrc = 0.delete itab.endif.

Deleting all lines

refresh itab.

If you also want to free the mamory taken up by the table, use FREE instead of REFRESH

Deleting a subset

This can be done in a loop, but it is better to do it this way:

delete itab where name = 'My name'.

Remember that you can also use wildcards. E.g. if you want to delete all name statinmg with 'A':

delete itab where name = 'A*'. 

Delete duplicate entries in internal table after sort

To delete all duplicate entries from a sorted internal table (e.g. just after SORT), you can use the 

Page 20: ABAP Internal Table Declaration

DELETE ADJACENT DUPLICATES FROM itab 

statement. 

You can use the COMPARING adition to limit the fields that are used to test for duplicate entries e.g.

SORT i_tab by matnr werks logort.DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr werks.

All duplicates withe same combination of matnr and werks will be deleted.

Modify line of internal tablemodify itab from wa_itab.

modify itab from wa_itab transporting <field1> <field2> 

Summarize data into an internal tableSyntax: COLLECT [wa INTO] itab. 

Note: You can only use COLLECT if all of the tables non-key fields are numeric ( Type I, P, F) 

The collect command summarizes all numerical fields (Type I, P, F) that are not part of the key into an internal table. The level of summarization is determined by the table key which can be both numerical and non numerical. After using the COLLECT command you will have a table with unique keys

Example:

REPORT zcollect.

TYPES: BEGIN OF t_mytype, key_c(10) TYPE c, key_n(10) TYPE n, key_i TYPE i, number TYPE i, END OF t_mytype.

DATA: gi_mytable TYPE SORTED TABLE OF t_mytype WITH NON-UNIQUE KEY key_c key_n key_i, wa_mytable TYPE t_mytype.

Page 21: ABAP Internal Table Declaration

START-OF-SELECTION.

CLEAR wa_mytable. wa_mytable-key_c = '10'. wa_mytable-key_n = '25'. wa_mytable-key_i = 5. wa_mytable-number = 400. COLLECT wa_mytable INTO gi_mytable.

CLEAR wa_mytable. wa_mytable-key_c = '10'. wa_mytable-key_n = '25'. wa_mytable-key_i = 5. wa_mytable-number = 500. COLLECT wa_mytable INTO gi_mytable.

CLEAR wa_mytable. wa_mytable-key_c = '11'. wa_mytable-key_n = '30'. wa_mytable-key_i = 6. wa_mytable-number = 200. COLLECT wa_mytable INTO gi_mytable.

CLEAR wa_mytable. wa_mytable-key_c = '11'. wa_mytable-key_n = '30'. wa_mytable-key_i = 6. wa_mytable-number = 900. COLLECT wa_mytable INTO gi_mytable.

CLEAR wa_mytable. wa_mytable-key_c = '11'. wa_mytable-key_n = '30'. wa_mytable-key_i = 7. wa_mytable-number = 100. COLLECT wa_mytable INTO gi_mytable.

END-OF-SELECTION.

LOOP AT gi_mytable INTO wa_mytable. WRITE: / wa_mytable-key_c, wa_mytable-key_n, wa_mytable-key_i, wa_mytable-number.

ENDLOOP.

Page 22: ABAP Internal Table Declaration

Result:

10 0000000025 5 900

11 0000000030 6 1.100 11 0000000030 7 100

If you remove key_i from the table key the result will be:

10 0000000025 10 900 11 0000000030 19 1.200