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 structure Internal table

Former Member
0 Likes
2,359

Hi Friends,

In my report i have a statement like this

SELECT * FROM (wa_report-tabna) INTO TABLE i_tabna.

In this based on the dynamically selected database table the internal table structure will also be changing. Is it possible to have dynamically changing internal table structure? If so can you help on this. Sample code will be very useful.

Thanks in Advance.

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
1,229

Hi,

make it simple:

<pre>

FIELD-SYMBOLS:

<table> TYPE table.

DATA:

lt_ref TYPE REF TO data.

*...

CREATE DATA lt_ref TYPE TABLE OF (wa_report-tabna).

ASSIGN lt_ref->* TO <table>.

SELECT * FROM (wa_report-tabna) INTO TABLE <table> UP TO 10 ROWS.

</pre>

Note that the internal table has no headerline but the desired structure.

Enjoy!

Regards,

Clemens

11 REPLIES 11
Read only

Former Member
0 Likes
1,229

Please look at the following blogs

/people/ravikumar.allampallam/blog/2005/05/31/expand-the-list-of-columns-in-a-report-dynamically

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

regards,

Ravi

Note - Please mark all the helpful answers

Read only

Former Member
0 Likes
1,229

Check

Regards,

Santosh

Read only

Former Member
0 Likes
1,229

Hi shri,

1.

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are

TABLE NAME / STRUCTURE NAME

and from those, it consructs dynamic table.

2. Here is the program.

the dynamic table name will be

<DYNTABLE>.

3. U can use this program (FORM in this program)

to generate any kind of internal table

by specifying TABLE NAME .

4.

REPORT abc.

*----


COMPULSORY

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA : fldname(50) TYPE c.

*----


parameters : iname LIKE dd02l-tabname.

*----


START-OF-SELECTION.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING ptabname.

*----


Create Dyn Table From FC

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

data : lt TYPE lvc_t_fcat .

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

*----


CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = iname

TABLES

ddfields = ddfields.

.

*----


CONSTRUCT FIELD LIST

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

*----


Assign Dyn Table To Field Sumbol

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

regards,

amit m.

Read only

0 Likes
1,229

Thank you Amit, it is really useful but i need some more help like , I need to read some fields from that dynamic internal table and pass those certain field values to a subroutine to update a transaction vk13. Can you help me on this.

Thanks.

Read only

0 Likes
1,229

If you know the name of the fields.

LOOP AT <ITAB> ASSIGNING <FS_ITAB>

ASSIGN COMPONENT 'COLUMN NAME' OF STRUCTURE <FS_ITAB> TO <FS_ANY>.

Now you have the value of the field in the field symbol <fs_any> and the same can be passed to sub routine.

ENDLOOP

Same has to be done for other fields as well.

Regards,

Ravi

Note - Please mark all the helpful answers

Read only

0 Likes
1,229

Hi again,

1. To display data of internal table, using field-symbols

2. just copy paste

Report abc.

*----


data : itab like t001 occurs 0 with header line.

*----


field-symbols : <ft> type table.

FIELD-SYMBOLS: <dynline> TYPE ANY.

FIELD-SYMBOLS: <fld> TYPE ANY.

select * from t001 into table itab.

*----


assign itab[] to <FT>.

LOOP AT <ft> 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 m.

Read only

0 Likes
1,229

In my case the table itab is the dynamic internal table if i give a statement like this SELECT * into table <dyntable>

FROM (iname).

LOOP AT <dyntable> ASSIGNING <dynline>.

sy-subrc = 0.

WHILE sy-subrc = 0.

ASSIGN COMPONENT sy-index OF STRUCTURE <dynline> TO <fld>.

WRITE : <fld>.

ENDWHILE.

ENDLOOP.

it is showing error statement is not accessible. What is wrong with the select statement. Help me.

Thanks.

Read only

0 Likes
1,229

SELECT * into <b>correspodning fields of</b> table <dyntable>

FROM (iname).

Regards,

Ravi

Note - Please mark the helpful answers

Read only

0 Likes
1,229

Hi Amit,

Now my code is

FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.

FIELD-SYMBOLS: <dynline> TYPE ANY.

FIELD-SYMBOLS: <fld> TYPE ANY.

DATA: lt TYPE lvc_t_fcat.

DATA: ls TYPE lvc_s_fcat.

DATA : fldname(50) TYPE c.

PARAMETERS : iname LIKE dd02l-tabname.

START-OF-SELECTION.

PERFORM mydyntable USING lt.

&----


*& Form mydyntable

&----


  • text

----


  • -->PTABNAME text

----


FORM mydyntable USING ptabname.

FIELD-SYMBOLS: <fs_data> TYPE REF TO data.

FIELD-SYMBOLS: <fs_1>.

FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.

DATA: lt_data TYPE REF TO data.

DATA : lt TYPE lvc_t_fcat .

DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'

EXPORTING

tabname = iname

TABLES

ddfields = ddfields.

LOOP AT ddfields.

ls-fieldname = ddfields-fieldname.

APPEND ls TO lt.

ENDLOOP.

ASSIGN lt_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt

IMPORTING

ep_table = <fs_data>

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

ASSIGN <fs_data>->* TO <fs_1>.

ASSIGN <fs_1> TO <fs_2>.

ASSIGN <fs_1> TO <dyntable>.

ENDFORM. "MYDYNTABLE

SELECT * INTO TABLE <dyntable>

FROM (iname).

LOOP AT <dyntable> ASSIGNING <dynline>.

sy-subrc = 0.

WHILE sy-subrc = 0.

ASSIGN COMPONENT sy-index OF STRUCTURE <dynline> TO <fld>.

WRITE : <fld>.

ENDWHILE.

ENDLOOP.

It is giving error in the line of select query. Can you help me out.

Thanks.

Read only

Clemenss
Active Contributor
0 Likes
1,230

Hi,

make it simple:

<pre>

FIELD-SYMBOLS:

<table> TYPE table.

DATA:

lt_ref TYPE REF TO data.

*...

CREATE DATA lt_ref TYPE TABLE OF (wa_report-tabna).

ASSIGN lt_ref->* TO <table>.

SELECT * FROM (wa_report-tabna) INTO TABLE <table> UP TO 10 ROWS.

</pre>

Note that the internal table has no headerline but the desired structure.

Enjoy!

Regards,

Clemens

Read only

Former Member
0 Likes
1,229

Finally If I try an insert with dynamic table with an entry onto the dynamic table, it doesn't insert a record, any ideas?