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

structura generated dynamically

Former Member
0 Likes
942

Hi. Could You tell me how to influence on itab structure depending on some parameter?

For example if parameter x = 3 the data structure would be

DATA: BEGIN OF itab OCCURS 0,

LEVEL1 LIKE c,

LEVEL2 LIKE c,

LEVEL3 LIKE c,

END OF itab.

If the x parameter would be 9 the data will have LEVEL1, ... LEVEL9 fields.

How to achieve that? Greetings. P.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
901

Hello Piotr

If you have already the RTTI classes available on your system you may use them. For details refer to:

[Creating Flat and Complex Internal Tables Dynamically using RTTI|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/creating%2bflat%2band%2bcomplex%2binternal%2btables%2bdynamically%2busing%2brtti]

Regards,

Uwe

10 REPLIES 10
Read only

Former Member
0 Likes
901

Hi,

Check this sample.

REPORT ZV_DYNAMIC .

PARAMETERS: P_COL1 TYPE I,

P_COL2 TYPE I.

FIELD-SYMBOLS: <FS> TYPE ANY.

DATA: CHECK.

TYPES: BEGIN OF TYP_ITAB,

COL1 TYPE I,

COL2 TYPE I,

COL3 TYPE I,

COL4 TYPE I,

COL5 TYPE I,

COL6 TYPE I,

END OF TYP_ITAB.

DATA: ITAB TYPE TABLE OF TYP_ITAB, WA LIKE LINE OF ITAB.

WA-COL1 = 1.

WA-COL2 = 2.

WA-COL3 = 3.

WA-COL4 = 4.

WA-COL5 = 5.

WA-COL6 = 6.

APPEND WA TO ITAB.

WA-COL1 = 1.

WA-COL2 = 2.

WA-COL3 = 3.

WA-COL4 = 4.

WA-COL5 = 5.

WA-COL6 = 6.

APPEND WA TO ITAB.

DATA: COL1(20),

COL2(20),

CHAR,

CHAR2.

WRITE P_COL1 TO CHAR.

CONCATENATE 'COL' CHAR INTO COL1.

WRITE P_COL2 TO CHAR.

CONCATENATE 'COL' CHAR INTO COL2.

*FIELD-SYMBOLS <F> TYPE ANY.

*ASSIGN (COL1) TO <F>.

DATA: BEGIN OF ITAB1 OCCURS 0,

COL1 TYPE I,

COL2 TYPE I,

END OF ITAB1.

FIELD-SYMBOLS: <FS1> TYPE ANY,

<FS2> TYPE ANY.

DATA: VAR TYPE I.

LOOP AT ITAB ASSIGNING <FS>.

IF SY-TABIX = 1.

ASSIGN COMPONENT COL1 OF STRUCTURE <FS> TO <FS1>.

ELSE.

ASSIGN COMPONENT COL2 OF STRUCTURE <FS> TO <FS2>.

ENDIF.

APPEND <FS1> TO ITAB1.

MOVE <FS1> TO ITAB1-COL1.

MOVE <FS2> TO ITAB1-COL2.

APPEND ITAB1.

WRITE:/ <FS1>,<FS2>.

ENDLOOP.

LOOP AT ITAB1.

WRITE:/ ITAB1-COL1, ITAB1-COL2.

ENDLOOP.

Regards,

Satish

Read only

RaymondGiuseppi
Active Contributor
0 Likes
901

Yes you can. A way is to use cl_alv_table_create.

Look at this code sample at sdn : [ABAP Code Sample for ALV Grid from Dynamically-Created Internal Table|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/1d22e990-0201-0010-588d-c4b4b431c52b] or [search sdn for cl_alv_table_create|https://www.sdn.sap.com/irj/sdn/advancedsearch?query=cl_alv_table_create&cat=sdn_codesamples].

Regards

Read only

uwe_schieferstein
Active Contributor
0 Likes
902

Hello Piotr

If you have already the RTTI classes available on your system you may use them. For details refer to:

[Creating Flat and Complex Internal Tables Dynamically using RTTI|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/creating%2bflat%2band%2bcomplex%2binternal%2btables%2bdynamically%2busing%2brtti]

Regards,

Uwe

Read only

0 Likes
901

Satish Panakala

code leads me into dump

Raymond Giuseppi

DATA: wa_fieldcat TYPE slis_fieldcat_alv.

returns 'The type "SLIS_FIELDCAT_ALV" is unknown.'

Uwe Schieferstein

go_table TYPE REF TO cl_salv_table,

returns 'The type "CL_ALV_TABLE" is unknown.'

Edited by: Piotr Wojciechowski on Jan 25, 2008 9:06 AM

Read only

0 Likes
901

Hello Pjotr

You have to add the type-pool to your report:


REPORT z_my_report.

TYPE-POOLS: slis.
...

Regards

Uwe

Read only

0 Likes
901

Add the following line to the global data of your program:

>TYPE-POOLS: SLIS.

Regards

Read only

0 Likes
901

OK. I try to do simple example:

REPORT ZWOP_TEST1 .

TYPE-POOLS: slis.

FIELD-SYMBOLS : <newtab> TYPE table.

DATA: dyntab LIKE dntab OCCURS 0 WITH HEADER LINE.

DATA: i_fcat TYPE lvc_t_fcat.

DATA: wa_fcat TYPE lvc_s_fcat.

DATA: dref TYPE REF TO data.

wa_fcat-fieldname = 'F1'.

wa_fcat-ref_field = 'text01'.

wa_fcat-ref_table = 'zlstrom01'.

APPEND wa_fcat TO i_fcat .

wa_fcat-fieldname = 'F2'.

wa_fcat-ref_field = 'text01'.

wa_fcat-ref_table = 'zlstrom01'.

APPEND wa_fcat TO i_fcat .

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = i_fcat

IMPORTING

ep_table = dref.

ASSIGN dref->* TO <newtab>.

<newtab>-F1 = 'aaa'.

<newtab>-F2 = 'bbb'.

APPEND <newtab>.

"<NEWTAB>" is a table without a header line and therefore has no component called "F1".

Read only

0 Likes
901

hi Piotr,

single fields of dynamically created strucures/tables you can reach the following way:

ASSIGN COMPONENT field OF STRUCTURE structure TO field_symbol.

hope this helps

ec

Read only

0 Likes
901
  • Eric Cartman*

Could You tell me how to add 2 records to my <newtab> in my example program?

F1

F2

aaa

bbb

ccc

ddd

Read only

0 Likes
901

you need a dynamic work area as well...

definition:

DATA : gw_dynwa TYPE REF TO data.

FIELD-SYMBOLS : <gf_line> TYPE ANY,

<gf_field> TYPE ANY.

in the program, after the table is ready and assigned to field-symbol:

CREATE DATA gw_dynwa LIKE LINE OF <newtable>.

ASSIGN gw_dynwa->* TO <gf_line>. ==> Now you have work area as well

To insert line into internal table trough the work area you have to code for each singel field:

ASSIGN COMPONENT 'F1' OF STRUCTURE <gf_line> TO <gf_field>.

<gf_field> = 'aaa'.

...

when one line is read, you can append:

APPEND <gf_line> to <newtable>.