11-06-2007 8:43 AM
Hi everyone,
How to create internal table dynamically? With respect to input it has to create fields.
If i give input as 22, then my internal table should get created with 22 fields.
Point will be rewarded!
Regards,
Stanley
11-06-2007 8:45 AM
HI
REPORT ZCLUST1 .
*
Example: how to create a dynamic internal table
The dynamic internal table stucture
DATA: BEGIN OF STRUCT OCCURS 10,
FILDNAME(8) TYPE C,
ABPTYPE TYPE C,
LENGTH TYPE I,
END OF STRUCT.
The dynamic program source table
DATA: BEGIN OF INCTABL OCCURS 10,
LINE(72),
END OF INCTABL.
DATA: LNG TYPE I, TYPESRTING(6).
Sample dynamic internal table stucture
STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.
APPEND STRUCT. CLEAR STRUCT.
Create the dynamic internal table definition in the dyn. program
INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.
LOOP AT STRUCT.
INCTABL-LINE = STRUCT-FILDNAME.
LNG = STRLEN( STRUCT-FILDNAME ).
IF NOT STRUCT-LENGTH IS INITIAL .
TYPESRTING(1) = '('.
TYPESRTING+1 = STRUCT-LENGTH.
TYPESRTING+5 = ')'.
CONDENSE TYPESRTING NO-GAPS.
INCTABL-LINE+LNG = TYPESRTING.
ENDIF.
INCTABL-LINE+15 = 'type '.
INCTABL-LINE+21 = STRUCT-ABPTYPE.
INCTABL-LINE+22 = ','.
APPEND INCTABL.
ENDLOOP.
INCTABL-LINE = 'end of dyntab. '.
APPEND INCTABL.
Create the code processes the dynamic internal table
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'endloop.'. APPEND INCTABL.
Create and run the dynamic program
INSERT REPORT 'zdynpro'(001) FROM INCTABL.
SUBMIT ZDYNPRO.
or Just try out this simpler dynamic internal tables
DATA: itab TYPE STANDARD TABLE OF spfli,
wa LIKE LINE OF itab.
DATA: line(72) TYPE c,
list LIKE TABLE OF line(72).
START-OF-SELECTION.
*line = ' CITYFROM CITYTO '.
line = ' AIRPTO '.
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.
WRITE 😕 wa-airpto.
ENDLOOP.
ENDIF.
Regards
Pavan
11-06-2007 8:49 AM
Hi,
Dynamic internal table is an extension to internal table concept, used when the number of fields is not known at the design time or until the compile time.
Scenario 1:
Display the table of contents in grid format using the dynamic internal table.
Creating Dynamic internal table
PARAMETERS : p_table(10) TYPE C.
DATA: w_tabname TYPE w_tabname,
w_dref TYPE REF TO data,
w_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.
w_tabname = p_table.
CREATE DATA w_dref TYPE TABLE OF (w_tabname).
ASSIGN w_dref->* TO <t_itab>.
Populating Dynamic internal table
SELECT *
FROM (w_tabname) UP TO 20 ROWS
INTO TABLE <t_itab>.
Displaying dynamic internal table using Grid.
CREATE OBJECT w_grid
EXPORTING i_parent = cl_gui_container=>screen0.
CALL METHOD w_grid->set_table_for_first_display
EXPORTING
i_structure_name = w_tabname
CHANGING
it_outtab = <t_itab>.
CALL SCREEN 100.
Scenario 2:
Create a dynamic internal table with the specified number of columns.
Creating Dynamic internal table
TYPE-POOLS: slis.
FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE, Dynamic internal table name
<fs_dyntable>, Field symbol to create work area
<fs_fldval> type any. Field symbol to assign values
PARAMETERS: p_cols(5) TYPE c. Input number of columns
DATA: t_newtable TYPE REF TO data,
t_newline TYPE REF TO data,
t_fldcat TYPE slis_t_fldcat_alv,
t_fldcat TYPE lvc_t_fcat,
wa_it_fldcat TYPE lvc_s_fcat,
wa_colno(2) TYPE n,
wa_flname(5) TYPE c.
Create fields .
DO p_cols TIMES.
CLEAR wa_it_fldcat.
move sy-index to wa_colno.
concatenate 'COL'
wa_colno
into wa_flname.
wa_it_fldcat-fieldname = wa_flname.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 10.
APPEND wa_it_fldcat TO t_fldcat.
ENDDO.
Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fldcat
IMPORTING
ep_table = t_newtable.
ASSIGN t_newtable->* TO <t_dyntable>.
Create dynamic work area and assign to FS
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
Populating Dynamic internal table
DATA: fieldname(20) TYPE c.
DATA: fieldvalue(10) TYPE c.
DATA: index(3) TYPE c.
DO p_cols TIMES.
index = sy-index.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
Set up fieldvalue
CONCATENATE 'VALUE' index INTO
fieldvalue.
CONDENSE fieldvalue NO-GAPS.
ASSIGN COMPONENT wa_flname
OF STRUCTURE <fs_dyntable> TO <fs_fldval>.
<fs_fldval> = fieldvalue.
ENDDO.
Append to the dynamic internal table
APPEND <fs_dyntable> TO <t_dyntable>.
Displaying dynamic internal table using Grid.
DATA: wa_cat LIKE LINE OF fs_fldcat.
DO p_cols TIMES.
CLEAR wa_cat.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
wa_cat-fieldname = wa_flname.
wa_cat-seltext_s = wa_flname.
wa_cat-outputlen = '10'.
APPEND wa_cat TO fs_fldcat.
ENDDO.
Call ABAP List Viewer (ALV)
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = fs_fldcat
TABLES
t_outtab = <t_dyntable>.