Application Development 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: 

Creating a structure dynamically

Former Member
0 Kudos
3,033

Hi,

I have to create a structure based on a set of fields that I have. The field names and data types will come from a table at runtime. Using this information, how do I create a structure?

Can this be done using the cl_abap_structdescr=>create() method? If yes, how should I pass the data type as a reference (type cl_abap_datadescr).

Any help would be greatly appreciated. Thank you.

Regards,

Nithya

1 ACCEPTED SOLUTION

Former Member
0 Kudos
234

Hi nithya,

1. The field names and data types will come from a table at runtime

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are FIELD LIST

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 some inputs (ie. field list)

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 : infty(4) TYPE c OBLIGATORY.

DATA : iname LIKE dd02l-tabname.

START-OF-SELECTION.

*----


GET INFO

CONCATENATE 'P' infty INTO iname.

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.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING lt TYPE lvc_t_fcat .

*----


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.

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.

5 REPLIES 5

Former Member
0 Kudos
234

Hi,

Why don't you use field symbols to meet your requirement?.

Field-symbols <FS> type any.

This can meet your requirement If i am right.

Thank you.

regards,

Karun M

0 Kudos
234

hi,

check this thread..

Former Member
0 Kudos
235

Hi nithya,

1. The field names and data types will come from a table at runtime

For this purpose,

in my program,

there is an INDEPENDENT FORM

whose inputs are FIELD LIST

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 some inputs (ie. field list)

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 : infty(4) TYPE c OBLIGATORY.

DATA : iname LIKE dd02l-tabname.

START-OF-SELECTION.

*----


GET INFO

CONCATENATE 'P' infty INTO iname.

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.

*----


PERFORM

PERFORM mydyntable USING lt.

BREAK-POINT.

*----


  • INDEPENDENT FORM

*----


FORM mydyntable USING lt TYPE lvc_t_fcat .

*----


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.

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.

hymavathi_oruganti
Active Contributor
0 Kudos
234

if u want ITAB to be created dynamically, use the method

what AMIT suggested, now to get the structure do as following.....

FIELD-SYMBOLS: <FS> TYPE ANY.

DATA: DREF TYPE DATA.

CREATE DATA DREF LIKE LINE OF ITAB

ASSIGN DREF->* TO <FS>.

now <FS> contains the structure

0 Kudos
234

Hi Amit & Hymavathi,

Thank you, the problem is solved now.

Regards,

Nithya