Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

create dynamic internal table

Former Member
0 Kudos

Hi All,

my requirement is to create a dynamic internal table resembling the structure of a infotype. The user enters a infotype number on the selection-screen. i've to create a internal table which can hold the data pertaining to that infotype.

can anyone help me in this regard??

4 REPLIES 4

FredericGirod
Active Contributor
0 Kudos

Hi Vijay,

please use the search option of the forum.

for ex:

Rgd

Frédéric

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Try something like this.



report zrich_0002.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.

data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.

type-pools : abap.

data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.

selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.


* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].

loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-inttype = wa_details-type_kind.
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.

* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.

assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.

* Select Data from table.
select * into table <dyn_table>
           from (p_table).

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

Regards,

Rich Heilman

Former Member
0 Kudos

Hii Vijay

here is the code to create a Dynamic internal Table.....

Report Dynamic_itab.

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.

Regards

-


Sachin Dhingra

Former Member
0 Kudos

Hi vijay,

1. EXACTLY LIKE U SAID.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

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

to generate any kind of internal table

by specifying some inputs (ie. field list)

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 : infty(4) TYPE c OBLIGATORY.

DATA : iname LIKE dd02l-tabname.

START-OF-SELECTION.

*----


GET INFO

CONCATENATE 'P' infty INTO iname.

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.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING lt TYPE lvc_t_fcat .

*----


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.

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.