Application Development and Automation 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: 
Read only

How to Read data base table at runtime

Former Member
0 Likes
914

Hi,

i have an internal table with two fields they are

tablename and field name.

itab has some entries.

like :

itab-table name itab-fieldname

kna1 kunnr

mara matnr

vbuk vbeln

tvak auart

i want to read these tables with field names.

like :

loop at itab

select singe * from <itab-tablename> into <jtab>

where <itab-fieldname > = <dyanmic value from prog>.

endloop.

Is it possible to read the db table in the above scenario.

Please try to give me the solution.

Thanks,

srik

7 REPLIES 7
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
827

Yes, it is. Need an example?

Regards,

Rich Heilman

Read only

0 Likes
827

HI Rich,

I need an example..

Thanks in advance.

srik

Read only

0 Likes
827

Hi, try this example program.



report zrich_0002 .

data: begin of itab occurs 0,
      tabname(20) type c,
      fldname(20) type c,
      end of itab.

data: ref_descr type ref to cl_abap_structdescr.
data: where_clause(100) type c occurs 0 with header line.

data: it_details type abap_compdescr_tab,
      wa_details type abap_compdescr.

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

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

start-of-selection.

  itab-tabname = 'T000'.
  itab-fldname = 'MANDT'.
  append itab.

  itab-tabname = 'T001'.
  itab-fldname = 'BUKRS'.
  append itab.


  loop at itab.


* Get the structure of the table.
    refresh it_fldcat.
    refresh where_clause.

    ref_descr ?= cl_abap_typedescr=>describe_by_name( itab-tabname ).
    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.

    if <dyn_table> is assigned.
      refresh <dyn_table>.
      unassign <dyn_table>.
    endif.

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



    concatenate itab-fldname '<> ''' '''' into where_clause
                    separated by space.
    append where_clause.

    select * from (itab-tabname) into table <dyn_table>
                  where (where_clause).



  endloop.


Regards,

Rich Heilman

Read only

0 Likes
827

Thans Rich

Thanks,

srikanth

Read only

Former Member
0 Likes
827

hi,

it is possible. u just define a variable

<b>DATA : VAR1 LIKE DD02L-TABNAME.</b>

now in select query u can write

select singe * from <b>(VAR1)</b> into <jtab>

where <itab-fieldname > = <dyanmic value from prog>.

endloop.

same way u can do for field name also.

Read only

0 Likes
827

pawan,

i tried with ur logic. But iam getting short dump at

var2.

check the following statments.

var2 like dd03l-fieldname.

loop at itab.

var1 = itab-tablename.

var2 = itab-fieldnaem.

select * from (var1) into table <jtab> for all entries

in <ktab> where (var2) = ktab-field.

append jtab.

endselect.

endloop.

Thanks,

srik

Read only

Former Member
0 Likes
827

Hi

Can you make it a bit clear.

if you have itab1 and itab2.

then you can loop itab1 and

read itab2 with key = itab-field.

youcan also do select single * .

but performance wise reading is better inside the loop.endloop