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

HELP! Dynamic Tables.. Loop and Header issue

Former Member
0 Likes
614

Hi I have this code for dynamic table

*----


types: begin of y_tablenames,

tabnames(10) type c,

end of y_tabnames.

data: i_tablenames TYPE STANDARD TABLE OF y_tablenames,

w_tabname type i_tablenames-tabnames.

field-symbols: <fs_tabnames> type y_tablenames,

<fs_stag_tab> type standard table,

<fs_stag_header> type any.

select tablename

from tables

into i_tablenames.

LOOP AT i_tablenames ASSIGNING <fs_tabnames>.

move: <fs_tabnames>-tabnames TO w_tabname

CREATE DATA w_stag_ref TYPE STANDARD TABLE OF (w_tabname).

ASSIGN w_stag_ref->* TO <fs_stag_tab>.

CREATE DATA w_sref LIKE LINE OF <fs_stag_tab>.

ASSIGN w_sref->* TO <fs_stag_header>.

LOOP AT <fs_stag_tab> ASSIGNING <fs_stag_header>.

write: / <fs_stag_header>-tabnames. " SYNTAX ERROR OCCURS HERE

ENDLOOP.

ENDLOOP.

*----


The error is The data object <fs_stag_header> has no structure and therefore nocomponent called "tabnames"

However, if I run the code and check the values of the <fs_stag_tab> and <fs_stag_tab> at runtime, I get the data I need.

I really need to LOOP the table and get the fields because I need to do a SELECT statement using the values of the fields.

Please help me!!

5 REPLIES 5
Read only

matt
Active Contributor
0 Likes
579

Read F1 help on ASSIGN COMPONENT

I also notice that at no point does <fs_stag_tab> have any values. also, with LOOP AT ... ASSIGNING <fs_stag_header>, you don't need the earlier assign for <fs_stag_header>

Read only

Former Member
0 Likes
579

I have lots of tables and the fields are also dynamic so I can't use ASSIGN COMPONENT. I need to access the fields separately.

Read only

Former Member
0 Likes
579

Hi Frn ,

read the below link it will help you ...

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/frameset.htm

becaue filed symbol take memory at run time ...so you can not acess field symbols statically .

for your requirment you should code like below.

FIELD-SYMBOLS: <f1> type any.

LOOP AT i_tablenames ASSIGNING <fs_tabnames>.

move: <fs_tabnames>-tabnames TO w_tabname

CREATE DATA w_stag_ref TYPE STANDARD TABLE OF (w_tabname).

ASSIGN w_stag_ref->* TO <fs_stag_tab>.

CREATE DATA w_sref LIKE LINE OF <fs_stag_tab>.

ASSIGN w_sref->* TO <fs_stag_header>.

LOOP AT <fs_stag_tab> ASSIGNING <fs_stag_header>.

DO N TIMES. " N is the no of coloum in you internal table structure

ASSIGN COMPONENT SY-INDEX OF STRUCTURE <fs_stag_header> to <f1>.

WRITE <f1>.

ENDDO.

ENDLOOP.

ENDLOOP.

Thanks and Regards..

Priyank

Read only

0 Likes
579

oh i see that the link above has the same problem as mine. I guess I have to find another way of extracting the fields for the dynamic table.

Thanks for the replies.

Read only

0 Likes
579

Hi,

you can extract the fields of any structure or internal table using this code

DATA:
    lt_comp                               TYPE abap_compdescr_tab,
    lr_dat                                TYPE REF TO data,
    lr_typedescr                          TYPE REF TO cl_abap_typedescr,
    lr_structdescr                        TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS:
    <fs>                                  TYPE ANY,
    <ft>                                  TYPE ANY TABLE,
    <comp>                                TYPE LINE OF abap_compdescr_tab.
  lr_typedescr                            = cl_abap_typedescr=>describe_by_data( px_data ).
  CASE lr_typedescr->kind.
    WHEN 'S'.
      lr_structdescr ?= lr_typedescr.
      lt_comp                             = lr_structdescr->components.
    WHEN 'T'.
      ASSIGN px_data TO <ft>.
      CREATE DATA lr_dat                  LIKE LINE OF <ft>.
      ASSIGN lr_dat->* TO <fs>.
      lr_structdescr ?= cl_abap_structdescr=>describe_by_data( <fs> ).
      lt_comp                             = lr_structdescr->components.
    WHEN OTHERS.
      MESSAGE e241(00).
*   Function is invalid                   in this environment
  ENDCASE.

px_data is a parameter where you pass your header line or internal table. Internal table lt_comp has the components with <comp>-name as the fieldname.

Regards,

Clemens