Skip to Content
author's profile photo Former Member
Former Member

how to create dynamic internal table ?

how to create dynamic internal table ?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2005 at 06:00 AM

    hi,

    try this..

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

    REPORT zmaschl_create_data_dynamic .

    TYPE-POOLS: slis.

    DATA: it_fcat TYPE slis_t_fieldcat_alv,

    is_fcat LIKE LINE OF it_fcat.

    DATA: it_fieldcat TYPE lvc_t_fcat,

    is_fieldcat LIKE LINE OF it_fieldcat.

    DATA: new_table TYPE REF TO data.

    DATA: new_line TYPE REF TO data.

    FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

    <l_line> TYPE ANY,

    <l_field> TYPE ANY.

    • Build fieldcat

    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

    EXPORTING

    i_structure_name = 'SYST'

    CHANGING

    ct_fieldcat = it_fcat[].

    LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

    MOVE-CORRESPONDING is_fcat TO is_fieldcat.

    is_fieldcat-fieldname = is_fcat-fieldname.

    is_fieldcat-ref_field = is_fcat-fieldname.

    is_fieldcat-ref_table = is_fcat-ref_tabname.

    APPEND is_fieldcat TO it_fieldcat.

    ENDLOOP.

    • Create a new Table

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = it_fieldcat

    IMPORTING

    ep_table = new_table.

    • Create a new Line with the same structure of the table.

    ASSIGN new_table->* TO <l_table>.

    CREATE DATA new_line LIKE LINE OF <l_table>.

    ASSIGN new_line->* TO <l_line>.

    • Test it...

    DO 30 TIMES.

    ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

    <l_field> = sy-index.

    INSERT <l_line> INTO TABLE <l_table>.

    ENDDO.

    LOOP AT <l_table> ASSIGNING <l_line>.

    ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

    WRITE <l_field>.

    ENDLOOP.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2005 at 06:01 AM

    try this......

    TABLES: mara, makt.

    TYPE-POOLS: slis.

    DATA: it_fcat

    TYPE slis_t_fieldcat_alv,

    is_fcat LIKE LINE OF it_fcat,

    ls_layout TYPE slis_layout_alv.

    DATA: it_fieldcat TYPE lvc_t_fcat,

    is_fieldcat LIKE LINE OF

    it_fieldcat.

    DATA: new_table TYPE REF TO data,

    new_line TYPE REF TO data,

    ob_cont_alv TYPE REF TO cl_gui_custom_container,

    ob_alv TYPE REF TO cl_gui_alv_grid,

    vg_campos(255) TYPE c,

    i_campos LIKE TABLE OF vg_campos,

    vg_campo(30) TYPE c,

    vg_tables(60) TYPE c.

    *DATA: e_params LIKE zutsvga_alv_01.

    FIELD-SYMBOLS: <l_table> TYPE table,

    <l_line> TYPE ANY,

    <l_field> TYPE ANY.

    PARAMETERS: p_max(2) TYPE n DEFAULT '20' OBLIGATORY.

    is_fcat-fieldname = 'COL01'.

    is_fcat-ref_fieldname = 'MATNR'.

    is_fcat-ref_tabname = 'MARA'.

    APPEND is_fcat TO it_fcat.

    is_fcat-fieldname = 'COL02'.

    is_fcat-ref_fieldname = 'MAKTX'.

    is_fcat-ref_tabname = 'MAKT'.

    APPEND is_fcat TO it_fcat.

    LOOP AT it_fcat INTO is_fcat.

    is_fieldcat-fieldname = is_fcat-fieldname.

    is_fieldcat-ref_field = is_fcat-ref_fieldname.

    is_fieldcat-ref_table = is_fcat-ref_tabname.

    APPEND is_fieldcat TO it_fieldcat.

    CONCATENATE is_fieldcat-ref_table is_fieldcat-ref_field INTO

    vg_campos SEPARATED BY '~'.

    APPEND vg_campos TO i_campos.

    ENDLOOP.

    *... Create the dynamic internal table

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = it_fieldcat

    IMPORTING

    ep_table = new_table.

    *... Create a new line

    ASSIGN new_table->* TO <l_table>.

    CREATE DATA new_line LIKE LINE OF <l_table>.

    ASSIGN new_line->* TO <l_line>.

    SELECT (i_campos) FROM mara INNER JOIN makt ON maramatnr = maktmatnr

    UP TO P_MAX ROWS INTO TABLE <l_table>.

    describe table <l_table>.

    write :/ sy-tfill.

    LOOP AT <l_table> INTO <l_line>.

    LOOP AT it_fcat INTO is_fcat.

    ASSIGN COMPONENT is_fcat-fieldname OF STRUCTURE <l_line> TO <l_field>.

    IF sy-tabix = 1.

    WRITE: /2 <l_field>.

    ELSE.

    WRITE: <l_field>.

    ENDIF.

    ENDLOOP.

    ENDLOOP.

    rgds,

    PJ

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2005 at 06:04 AM

    thnx anid for ur quick response.it is very helpful for me....

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 15, 2006 at 08:27 AM

    Hi Kishan

    I am Sending you few sample codes

    Award points if you u find it helpful

    type-pools : abap.

    field-symbols: <dyn_table> type standard table,

    <dyn_wa>,

    <dyn_field>.

    data: dy_table type ref to data,

    dy_line type ref to data,

    xfc type lvc_s_fcat,

    ifc type lvc_t_fcat.

    selection-screen begin of block b1 with frame.

    parameters: p_table(30) type c default 'T001'.

    selection-screen end of block b1.

    start-of-selection.

    perform get_structure.

    perform create_dynamic_itab. *********Creates a dyanamic internal table*********

    perform get_data.

    perform write_out.

    form get_structure.

    data : idetails type abap_compdescr_tab,

    xdetails type abap_compdescr.

    data : ref_table_des type ref to cl_abap_structdescr.

    • Get the structure of the table.

    ref_table_des ?=

    cl_abap_typedescr=>describe_by_name( p_table ).

    idetails[] = ref_table_des->components[].

    loop at idetails into xdetails.

    clear xfc.

    xfc-fieldname = xdetails-name .

    xfc-datatype = xdetails-type_kind.

    xfc-inttype = xdetails-type_kind.

    xfc-intlen = xdetails-length.

    xfc-decimals = xdetails-decimals.

    append xfc to ifc.

    endloop.

    endform.

    form create_dynamic_itab.

    • Create dynamic internal table and assign to FS

    call method cl_alv_table_create=>create_dynamic_table

    exporting

    it_fieldcatalog = ifc

    importing

    ep_table = dy_table.

    assign dy_table->* to <dyn_table>.

    • Create dynamic work area and assign to FS

    create data dy_line like line of <dyn_table>.

    assign dy_line->* to <dyn_wa>.

    endform.

    form get_data.

    • Select Data from table.

    select * into table <dyn_table>

    from (p_table).

    endform.

    Write out data from table.

    loop at <dyn_table> into <dyn_wa>.

    do.

    assign component sy-index

    of structure <dyn_wa> to <dyn_field>.

    if sy-subrc <> 0.

    exit.

    endif.

    if sy-index = 1.

    write:/ <dyn_field>.

    else.

    write: <dyn_field>.

    endif.

    enddo.

    endloop.

    -


    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 15, 2006 at 08:31 AM

    Hi kishan,

    1.

    For this purpose,

    in my program,

    there is an <b>INDEPENDENT FORM</b>

    <b>whose inputs are

    TABLE NAME / STRUCTURE NAME</b>

    and from those, it consructs dynamic table.

    2. Here is the program.

    the dynamic table name will be

    <b><DYNTABLE>.</b>

    3. U can use this program (FORM in this program)

    to generate any kind of internal table

    by specifying TABLE NAME .

    4.

    REPORT abc.

    *----


    COMPULSORY

    FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

    FIELD-SYMBOLS: <dynline> TYPE ANY.

    DATA: lt TYPE lvc_t_fcat.

    DATA: ls TYPE lvc_s_fcat.

    FIELD-SYMBOLS: <fld> TYPE ANY.

    DATA : fldname(50) TYPE c.

    *----


    parameters : iname LIKE dd02l-tabname.

    *----


    START-OF-SELECTION.

    *----


    PERFORM

    PERFORM mydyntable USING lt.

    BREAK-POINT.

    *----


    • INDEPENDENT FORM

    *----


    FORM mydyntable USING ptabname.

    *----


    Create Dyn Table From FC

    FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

    FIELD-SYMBOLS: <fs_1>.

    FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

    DATA: lt_data TYPE REF TO data.

    data : lt TYPE lvc_t_fcat .

    DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

    *----


    CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

    EXPORTING

    tabname = iname

    TABLES

    ddfields = ddfields.

    .

    *----


    CONSTRUCT FIELD LIST

    LOOP AT ddfields.

    ls-fieldname = ddfields-fieldname.

    APPEND ls TO lt.

    ENDLOOP.

    ASSIGN lt_data TO <fs_data>.

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = lt

    IMPORTING

    ep_table = <fs_data>

    EXCEPTIONS

    generate_subpool_dir_full = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    ENDIF.

    *----


    Assign Dyn Table To Field Sumbol

    ASSIGN <fs_data>->* TO <fs_1>.

    ASSIGN <fs_1> TO <fs_2>.

    ASSIGN <fs_1> TO <dyntable>.

    ENDFORM. "MYDYNTABLE

    regards,

    amit m.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      thnx amit...

      but i was posted this query long back and also close the thread..i don't know why people reply now....

      how to create dynamic internal table ?

      <b>Posted: Oct 18, 2005 1:57 AM </b> Reply E-mail this post

      how to create dynamic internal table ?

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.