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

Create dynamic internal table

Former Member
0 Likes
1,317

Hi experts,

could anyone please give me the code on creating an internal table dynamically?

i want to create an internal table which i can add or remove a field as the program runs.

thanks in advance.

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
723

Hi,

Check this sample code from other thread.


===================================== 
REPORT zmaschl_create_data_dynamic . 

TYPE-POOLS: slis. 

DATA: it_fcat TYPE slis_t_fieldcat_alv, 
is_fcat LIKE LINE OF it_fcat. 
DATA: it_fieldcat TYPE lvc_t_fcat, 
is_fieldcat LIKE LINE OF it_fieldcat. 
DATA: new_table TYPE REF TO data. 
DATA: new_line TYPE REF TO data. 
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE, 
<l_line> TYPE ANY, 
<l_field> TYPE ANY. 

* Build fieldcat 
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' 
EXPORTING 
i_structure_name = 'SYST' 
CHANGING 
ct_fieldcat = it_fcat[]. 
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial. 
MOVE-CORRESPONDING is_fcat TO is_fieldcat. 
is_fieldcat-fieldname = is_fcat-fieldname. 
is_fieldcat-ref_field = is_fcat-fieldname. 
is_fieldcat-ref_table = is_fcat-ref_tabname. 
APPEND is_fieldcat TO it_fieldcat. 
ENDLOOP. 

* Create a new Table 
CALL METHOD cl_alv_table_create=>create_dynamic_table 
EXPORTING 
it_fieldcatalog = it_fieldcat 
IMPORTING 
ep_table = new_table. 

* Create a new Line with the same structure of the table. 
ASSIGN new_table->* TO <l_table>. 
CREATE DATA new_line LIKE LINE OF <l_table>. 
ASSIGN new_line->* TO <l_line>. 

* Test it... 
DO 30 TIMES. 
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>. 
<l_field> = sy-index. 
INSERT <l_line> INTO TABLE <l_table>. 
ENDDO. 

LOOP AT <l_table> ASSIGNING <l_line>. 
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>. 
WRITE <l_field>. 
ENDLOOP. 

Hope this will help.

Regards,

Ferry Liato

3 REPLIES 3
Read only

ferry_lianto
Active Contributor
0 Likes
724

Hi,

Check this sample code from other thread.


===================================== 
REPORT zmaschl_create_data_dynamic . 

TYPE-POOLS: slis. 

DATA: it_fcat TYPE slis_t_fieldcat_alv, 
is_fcat LIKE LINE OF it_fcat. 
DATA: it_fieldcat TYPE lvc_t_fcat, 
is_fieldcat LIKE LINE OF it_fieldcat. 
DATA: new_table TYPE REF TO data. 
DATA: new_line TYPE REF TO data. 
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE, 
<l_line> TYPE ANY, 
<l_field> TYPE ANY. 

* Build fieldcat 
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' 
EXPORTING 
i_structure_name = 'SYST' 
CHANGING 
ct_fieldcat = it_fcat[]. 
LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial. 
MOVE-CORRESPONDING is_fcat TO is_fieldcat. 
is_fieldcat-fieldname = is_fcat-fieldname. 
is_fieldcat-ref_field = is_fcat-fieldname. 
is_fieldcat-ref_table = is_fcat-ref_tabname. 
APPEND is_fieldcat TO it_fieldcat. 
ENDLOOP. 

* Create a new Table 
CALL METHOD cl_alv_table_create=>create_dynamic_table 
EXPORTING 
it_fieldcatalog = it_fieldcat 
IMPORTING 
ep_table = new_table. 

* Create a new Line with the same structure of the table. 
ASSIGN new_table->* TO <l_table>. 
CREATE DATA new_line LIKE LINE OF <l_table>. 
ASSIGN new_line->* TO <l_line>. 

* Test it... 
DO 30 TIMES. 
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>. 
<l_field> = sy-index. 
INSERT <l_line> INTO TABLE <l_table>. 
ENDDO. 

LOOP AT <l_table> ASSIGNING <l_line>. 
ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>. 
WRITE <l_field>. 
ENDLOOP. 

Hope this will help.

Regards,

Ferry Liato

Read only

sourabhshah
Product and Topic Expert
Product and Topic Expert
0 Likes
723

Hi Jesus,

Create a fieldcatalog with the desired fields and the call the method given below this creates a internal table at runtime.

DATA: lt_fieldcat TYPE lvc_t_fcat.

FIELD-SYMBOLS:<GT_TABLE1> TYPE TABLE.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_fieldcat

IMPORTING ep_table = lp_table.

ASSIGN lp_table->* TO <gt_table1>.

Regards,

Sourabh

Read only

0 Likes
723

Hi

The concept of usinf field symbols is quite useful in creating dynamic objects.

Plz try the following code :

tables: rsrd1.

data: linetype type string,

itabref type ref to data,

wa_fs type ref to data.

data : wa_out type string.

field-symbols: <fs> type standard table,

<fs1> type any .

parameter tbl like rsrd1-tbma_val. " For table name

linetype = tbl.

" dynamic table creation

create data itabref type standard table of (linetype).

assign itabref->* to <fs> . " Assign object to Field symbol

" It now behaves like an int table with initial

" values

select * from (tbl) into table <fs>.

create data wa_fs like line of <fs>.

assign wa_fs->* to <fs1> .

loop at <fs> into <fs1> . " or assigning <fs1>. even this works

endloop.

write 😕 'done' .

Regards

Pankaj