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 read a table dynamically

Former Member
0 Kudos
158

Hi,

I need to read the table which i will get that dynamically in the program.

some times the table name like * and some times it's like

a 4 digit name.

I have to read the 4 digit table name only.

How can i read taht table only......

Thanks,

srikanth.

12 REPLIES 12

Former Member
0 Kudos
102

How come your table name can change in a program?

Are you creating customised table or using standard SAP table?

Try to use if/else statement for different selecting of tables, but you must rectify the tables name at first hand.

0 Kudos
102

same thread.

0 Kudos
102

For dynamic table name....

REPORT demo_select_dynamic_database .

DATA wa TYPE scarr.

DATA name(10) TYPE c VALUE 'SCARR'.

SELECT *

INTO wa

FROM (name) CLIENT SPECIFIED

WHERE mandt = '000'.

WRITE: / wa-carrid, wa-carrname.

ENDSELECT.

0 Kudos
102

HI kishan,

Thanks...

Regards,

srikanth

0 Kudos
102

Thanks rajesh..

i will explain you clearly..

iam getting table names in itab-checktable.

It has multiple entries like * ,vbuk etc....

i want to read the table name which is in itab-checktable.

Regards,

srikanth

0 Kudos
102

Hi ong rukawaw,

My requirement is:

iam getting table names in itab-checktable.

It has multiple entries like *,vbuk,tvak etc....

i want to read the table name which is in itab-checktable.

Regards,

srik

0 Kudos
102

Hi Jonna,

I am afraid that when you read the table name from itab-checktable, you may need to store in a variable. The problem is we cant use select command to select record from variable.

Message was edited by: ong rukawaw

0 Kudos
102

My propose solution for you will be as below:

Loop at itab-checktable.

if itab-checktable-tablename = 'vbuk'.

Select * from vbuk...

elseif itab-checktable-tablename = 'tvak'.

Select * from tvak...

endif.

endloop.

Hope it helps you.

0 Kudos
102

Hi u just combine my previous solution wid this 1.

using earlier solution u r creating a dynaminc databse tab name.

now to create dynamic internal table name.........plz hv a look at http://www.sap-img.com/ab030.htm

I dont hv muc htime ...otherwise i culd hv combined both da solution for u.

so just do.

data : name (10) type c.

name = wa_itab-tabname.

select * into table (dynamic internal table)

from (name).

0 Kudos
102

Hi Ong rukawaw,

Thanks for your view. It's really very helpful for me.

Regards,

srikanth

0 Kudos
102

Hi rajesh,

Thanks for ur code.

It helped me alot.

Thanks,

srikanth.J

Former Member
0 Kudos
102

Hi srikanth,

Since you want to read table dynamically, thus i would suggeest to use most dynamic method:

 DATA ds_table TYPE REF TO data.
 DATA descr_struct_ref TYPE REF TO cl_abap_structdescr.
 DATA it_fieldcatalog TYPE lvc_t_fcat.
 DATA wa_fcat TYPE lvc_s_fcat.

 DATA ep_table TYPE REF TO data.
 FIELD-SYMBOLS: <my_table> TYPE ANY TABLE,
                <LINE_TABLE> TYPE ANY.
 FIELD-SYMBOLS: <fs> TYPE ANY.
 FIELD-SYMBOLS: <components> TYPE abap_compdescr.

PARAMETERS: p_table TYPE tabname.
PARAMETERS: p_maxrow type i.

* Create reference for structure of the table
* because statement CREATE DATA ref TYPE TABLE OF
* is not allowed in 4.6
 CREATE DATA ds_table TYPE (p_table).
 ASSIGN ds_table->* TO <fs>.

* cast down
 descr_struct_ref ?= cl_abap_typedescr=>describe_by_data( <fs> ).

* build fieldcatalog
 LOOP AT descr_struct_ref->components ASSIGNING <components>.
   wa_fcat-fieldname     = <components>-name.
   wa_fcat-ref_table     = p_table.
   wa_fcat-ref_field     = <components>-name.
   APPEND wa_fcat TO it_fieldcatalog.
 ENDLOOP.

* build a reference for internal table
 CALL METHOD cl_alv_table_create=>create_dynamic_table
   EXPORTING
     it_fieldcatalog           = it_fieldcatalog
   IMPORTING
     ep_table                  = ep_table
   EXCEPTIONS
     generate_subpool_dir_full = 1
     OTHERS                    = 2.

* assign refrence to field-symbol
 ASSIGN ep_table->* TO <my_table>.

* select dynamically in your table
 SELECT * FROM (p_table) INTO TABLE <my_table> UP TO p_maxrow ROWS.
  LOOP AT <MY_TABLE> ASSIGNING <LINE_TABLE>.
    WRITE:/ <LINE_TABLE>.
  ENDLOOP.