‎2009 Jun 03 8:58 AM
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
‎2009 Jun 03 9:18 AM
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
‎2009 Jun 03 9:02 AM
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
‎2009 Jun 03 9:06 AM
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
‎2009 Jun 03 9:09 AM
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
‎2009 Jun 03 9:14 AM
Hi Afzal tried it.
Getting the message--- The data object "WA" has no structure and therefore
no component called "FIELD1".
‎2009 Jun 03 9:07 AM
Hi,
LOOP AT <t_itab> ASSIGNING <wa>.
Write:/ <wa>-kunnr, <wa>-name1.
ENDLOOP.
Kiran
‎2009 Jun 03 9:13 AM
Hi,
Please use DD03L table to retrieve fields information of a table.
Aswarth
‎2009 Jun 03 9:13 AM
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
‎2009 Jun 03 9:18 AM
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
‎2009 Jun 03 9:51 AM
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
‎2009 Jun 03 9:21 AM
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
‎2009 Jun 03 9:31 AM
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.
‎2009 Jun 03 9:34 AM
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.