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: 

Reg. reading database tables.

Former Member
0 Kudos

Hi,

I have stored some database table names in a custom table. In my custom report, I have to read those database tables depending on certain criteria.

For ex: I have the table names 'MARA' , 'MARC' stored in my custom table ztables.

In my report, if a flag is set to 'X', I have to read MARA otherwise MARC...(these entries in ztables can change in future..so i can not read directly from MARA or MARC)..How to achieve this? Once the table names are stored in a variable, how to read data from this table name? if i say 'read' it will try to read this variable and not the table.

Thanks in advance for your input.

3 REPLIES 3

Former Member
0 Kudos

Hello,

You can pass a table name dynamically to a select query.

example :

data tb_name(4) type c.

"select your table name.....

"lets say that tb_name now has 'MARA'.

so the select query will be

select * from tb_name

....and then the where conditions

For further info, refer to ABAPDOCU transaction in that look for Database Access and dynamically specifying table name. or write SELECT in abap editor and press F1.

regards,

Advait.

former_member195698
Active Contributor
0 Kudos

you can try this approach


data : ref_table_des type ref to cl_abap_structdescr.

data:tabname like dd02l-tabname.

data : idetails type abap_compdescr_tab,
       xdetails type abap_compdescr,
    xfc type lvc_s_fcat,
    ifc type lvc_t_fcat.

data: dy_table type ref to data,
    dy_line  type ref to data,

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


  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( tabname ).

  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.

  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 data dy_line like line of <dyn_table>.
  assign dy_line->* to <dyn_wa>.

select *
into table <dyn_table>
from  (tabname).

where variable tabname stores the name of the table stored in your custom table.

0 Kudos

I tried Advait's answer with a slight modification - added ( ) for the variable tab_name. It's working. Thank you.

I have n't tried Abhishek's answer but thanks for your reply to this question.