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: 

How to dynamicaly (with variable) create itab - using field symbols?

Former Member
0 Kudos

Hi,

I need to create internal table using variable - field symbols. I think like this, but...

data: begin of itab occurs 0.

include structure <b>s_itab</b>.

end of itab.

s_itab is variable with structure of dbtab.

I need to include variable structure table.

i. e.

My itab then changes structure according variable s_itab.

First can be s_itab like table of bseg, second can be s_itab like table of hrp1001. And then I get desired itab.

Thanks for any ideas. zd.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Try this log:

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Rob

2 REPLIES 2

Former Member
0 Kudos

Try this log:

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Rob

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is a sample program which should show you everything that you need to know about dynamic internal tables.



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