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

variable field internal table

Former Member
0 Likes
570

Greetings,

Has anyone created an internal table programmatically with the number of fields based on user input?

Thanks in advance,

JR

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
548

Hi,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

  • Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

  • Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

Thanks,

Naren

4 REPLIES 4
Read only

Former Member
0 Likes
549

Hi,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

  • Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

  • Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

Thanks,

Naren

Read only

Former Member
0 Likes
548

hi Joseph,

Check out this one


REPORT ztest_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.

Read only

Former Member
0 Likes
548

Hi,

Check out this thread..

these threads have code similar to your requirement ...

Thanks

Read only

Former Member
0 Likes
548

Thank you for your input. Very interesting technique.