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: 

FM to populate all records in dbtable to an internal table

Former Member
0 Kudos
108

Hi,

i'm searching for a function module which accepts a given db table as input and populates all the records in an internal table. can anyone suggest an idea to do that.

thanks,

ravi.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
80

Hi Ravi;

Check out this blog, it may point you in the right direction...

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

-John

2 REPLIES 2

Former Member
0 Kudos
81

Hi Ravi;

Check out this blog, it may point you in the right direction...

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

-John

0 Kudos
80

Here is a sample program.



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