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

Dynamic Internal table

Former Member
0 Likes
1,376

Hi all,

I have written a very simple program for dynamic internal table

creation.

My internal table is getting populated.

As I am new to dynamic internal tables,

I dont know how to write out the contents of the dynamic internal table.

Appreciate your inputs and guidance.

Warm regards,

Hari Kiran

REPORT zdynamic.

PARAMETERS: p_table(10) TYPE c.

DATA: w_tabname TYPE w_tabname,

w_dref TYPE REF TO data,

dy_wa TYPE REF TO data.

FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE,

<wa>.

w_tabname = p_table.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).

ASSIGN w_dref->* TO <t_itab>.

CREATE DATA dy_wa LIKE LINE OF <t_itab>.

ASSIGN dy_wa->* TO <wa>.

  • populating dynamic Internal table

SELECT *

FROM (w_tabname) UP TO 20 ROWS

INTO TABLE <t_itab>.

LOOP AT <t_itab> ASSIGNING <wa>.

  • I want to write out the contents of the dynamic internal table using the write statement

ENDLOOP.

Edited by: HARI KIRAN REDDY on Jun 3, 2009 1:29 PM

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,294

field-symbols <comp> type any.

LOOP AT <t_itab> ASSIGNING <wa>.
  write: /.  "first new line at each new entry in table
  do.
    assing component sy-index of structure <wa> to <comp>.
    if sy-subrc = 0.
       write: <comp>.  "write component by component
    else.
      exit.
    endif.
  enddo.
ENDLOOP.

Regards

Marcin

PS:

Getting the message--- The data object "WA" has no structure and therefore

no component called "FIELD1".

This is because statically you defined WA as type any , so system doesn't recognize WA components. It is known only during runtime, but you are addressing it statically with


<wa>-field1. "not seen by system during sytax check

"you could address components by name dynamically with
assign coponent 'FIELD1' of structure <WA> to <comp>.  "now during runtime system is intructed to address component FIELD1, but this is dynamically known, not statically

Edited by: Marcin Pciak on Jun 3, 2009 10:19 AM

12 REPLIES 12
Read only

Former Member
0 Likes
1,294

hai hari if ur gettign data in the work area

then u cna use normal write statement as

write : <wa>-fields1, <wa>-field2.

hopes it soves ru problem

m.a

Read only

0 Likes
1,294

Hi,

I don't want to write all the field names explicitly.

Would like to write out the content in the work area.

Thank you for answering.

Warm regards,

Hari Kiran

Read only

0 Likes
1,294

hai hari if u write <wa>-field1 , it will not write field name but it will write contents of work area

just check it

regards

m.a

Read only

0 Likes
1,294

Hi Afzal tried it.

Getting the message--- The data object "WA" has no structure and therefore

no component called "FIELD1".

Read only

Former Member
0 Likes
1,294

Hi,

LOOP AT <t_itab> ASSIGNING <wa>.

Write:/ <wa>-kunnr, <wa>-name1.

ENDLOOP.

Kiran

Read only

Former Member
0 Likes
1,294

Hi,

Please use DD03L table to retrieve fields information of a table.

Aswarth

Read only

Former Member
0 Likes
1,294

Hi Hari,

You want to write out the contents of dynamic internal table

WITHOUT HARDCODING the FIELD NAMES.

Just copy paste this program.

It will have input for TABLE name and will AUTOMATICALLY LIST OUT the contents.




FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.
FIELD-SYMBOLS: <dynline> TYPE ANY.
FIELD-SYMBOLS: <fld> TYPE ANY.

  loop at <dyntable> ASSIGNING <dynline>.
    sy-subrc = 0.
    write :/ .
    WHILE SY-SUBRC = 0.
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <DYNLINE> TO <FLD>.
      WRITE : <FLD>.
    ENDWHILE.
  endloop.


Regards,

Amit Mittal.

Edited by: Amit Mittal on Jun 3, 2009 2:03 PM

Read only

MarcinPciak
Active Contributor
0 Likes
1,295

field-symbols <comp> type any.

LOOP AT <t_itab> ASSIGNING <wa>.
  write: /.  "first new line at each new entry in table
  do.
    assing component sy-index of structure <wa> to <comp>.
    if sy-subrc = 0.
       write: <comp>.  "write component by component
    else.
      exit.
    endif.
  enddo.
ENDLOOP.

Regards

Marcin

PS:

Getting the message--- The data object "WA" has no structure and therefore

no component called "FIELD1".

This is because statically you defined WA as type any , so system doesn't recognize WA components. It is known only during runtime, but you are addressing it statically with


<wa>-field1. "not seen by system during sytax check

"you could address components by name dynamically with
assign coponent 'FIELD1' of structure <WA> to <comp>.  "now during runtime system is intructed to address component FIELD1, but this is dynamically known, not statically

Edited by: Marcin Pciak on Jun 3, 2009 10:19 AM

Read only

0 Likes
1,294

Thanks Marcin,

Its working now.

Below is the final section of the code....

  • declared the field symbol <comp> as you mentioned

FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE,

<comp> TYPE ANY,

<wa>.

LOOP AT <t_itab> ASSIGNING <wa>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <comp>.

IF sy-subrc EQ 0.

WRITE:/ <comp>.

write:/ sy-uline.

ELSE.

EXIT.

ENDIF.

ENDDO.

ENDLOOP.

Thank you all for answering and for the inputs.

Warm regards,

Hari Kiran

Edited by: HARI KIRAN REDDY on Jun 3, 2009 2:36 PM

Read only

Former Member
0 Likes
1,294

Hi,

Please make use of this code.

LOOP AT <t_itab> ASSIGNING <wa>.

  • Here is the new cod.

do.

assign component sy-index

of structure <t_itab> to <wa>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <wa>. " This is your first field content.

else.

write: <wa>. " This is the next field contents.

endif.

enddo.

ENDLOOP.

Hope this will help you.

Regards,

Smart Varghese

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,294

REPORT  zdynamic.

PARAMETERS: p_table(10) TYPE c.


DATA: w_tabname TYPE w_tabname,
      w_dref TYPE REF TO data,
      dy_wa TYPE REF TO data.

FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE,
                            <wa>.

w_tabname = p_table.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).
ASSIGN w_dref->* TO <t_itab>.

CREATE DATA dy_wa LIKE LINE OF <t_itab>.
ASSIGN dy_wa->* TO <wa>.

SELECT *
FROM (w_tabname) UP TO 20 ROWS
INTO TABLE <t_itab>.

LOOP AT <t_itab> ASSIGNING <wa>.
* I want to write out the contents of the dynamic internal table using the write statement
ENDLOOP.

You need to get the components of your internal table. therefore you need two variables in addition and two field-symbols in addition:


    desc_struc           type ref to cl_abap_structdescr,
    desc_fields          type ddfields.


    <p_component>        type dfies,
    <p_field>                  type any,

Now you need to read all the componets of your line type:


  desc_struc ?= cl_abap_structdescr=>describe_by_data( <wa> ).
  desc_fields = desc_struc->get_ddic_field_list( ).

now you can loop through your table in an outer loop and loop through the componets in an inner loop:


LOOP AT <t_itab> ASSIGNING <wa>.

    loop at desc_struc->components assigning <p_component>.
        assign component <p_component>-name of structure <p_data> to <p_field>.
        write <p_field>.
  endloop.
ENDLOOP.

You have to refine the write statement to your need. But this will write out all (unknown) components of all lines.

Read only

Former Member
0 Likes
1,294
  • Gets all of the global data types.

CALL FUNCTION 'GET_GLOBAL_SYMBOLS'

EXPORTING

program = sy-repid

TABLES

fieldlist = fieldsym.

  • gets all of the components of a structure

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

program = sy-repid

fieldname = lv_tab_name

TABLES

components = fieldlist.

LOOP AT <table> INTO <wa>.

LOOP AT fieldlist. - - - - - - > All the fields of that structure <wa>

fieldname = fieldlist-compname .

ASSIGN COMPONENT fieldname OF STRUCTURE <wa> TO <wa_1>.

write <wa_1>.

ENDLOOP.

ENDLOOP.