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

Creating Dynamic Internal Table

Former Member
0 Likes
1,002

Hi, i try to create dynamic internal table after reading article that i find from this forum. But its still didnt fulfill my needs.

EX:

Below is the contents of the database table :

LGART

1000

2000

3000

Base on the contents i want to create the internal table with the field

intab-1000

intab-2000

intab-3000

And if there are 2 addition of lgart into the database table (ex 4000 and 5000)

Then the field for internal table should be

intab-1000

intab-2000

intab-3000

intab-4000

intab-5000

I try to use the following code

*DATA: BEGIN OF STRUCT OCCURS 10,

  • FILDNAME(8) TYPE C,

  • ABPTYPE TYPE C,

  • LENGTH TYPE I,

*END OF STRUCT.

*

    • The dynamic program source table

*DATA: BEGIN OF INCTABL OCCURS 10,

  • LINE(72),

*END OF INCTABL.

*

*DATA: LNG TYPE I, TYPESRTING(6).

*

    • Sample dynamic internal table stucture

*STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

*APPEND STRUCT. CLEAR STRUCT.

*

*STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

*APPEND STRUCT. CLEAR STRUCT.

*

*STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

*APPEND STRUCT. CLEAR STRUCT.

*

    • Create the dynamic internal table definition in the dyn. program

*INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

*INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

*

*LOOP AT STRUCT.

  • INCTABL-LINE = STRUCT-FILDNAME.

  • LNG = STRLEN( STRUCT-FILDNAME ).

*

  • IF NOT STRUCT-LENGTH IS INITIAL .

  • TYPESRTING(1) = '('.

  • TYPESRTING+1 = STRUCT-LENGTH.

  • TYPESRTING+5 = ')'.

  • CONDENSE TYPESRTING NO-GAPS.

  • INCTABL-LINE+LNG = TYPESRTING.

  • ENDIF.

*

  • INCTABL-LINE+15 = 'type '.

  • INCTABL-LINE+21 = STRUCT-ABPTYPE.

  • INCTABL-LINE+22 = ','.

  • APPEND INCTABL.

*ENDLOOP.

*INCTABL-LINE = 'end of dyntab. '.

*APPEND INCTABL.

*

    • Create the code processes the dynamic internal table

*INCTABL-LINE = ' '. APPEND INCTABL.

*INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

*INCTABL-LINE = 'dyntab-field2 = ''19970814''.'. APPEND INCTABL.

*INCTABL-LINE = 'dyntab-field3 = 1.'. APPEND INCTABL.

*INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

*INCTABL-LINE = ' '. APPEND INCTABL.

*INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

*INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

*INCTABL-LINE = 'endloop.'. APPEND INCTABL.

*

    • Create and run the dynamic program

*INSERT REPORT 'zdynpro'(001) FROM INCTABL.

*SUBMIT ZDYNPRO.

But there in one problem, after the process of submit zdynpro, the internal table will gone, because the definition of the internal table only define inside the process of zdynpro.....

Any other idea ?

Thanks

11 REPLIES 11
Read only

vinod_gunaware2
Active Contributor
0 Likes
958

type-pools : abap.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

Write out data from table.

loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

-


REPORT ZCLUST1 .

  • Example: how to create a dynamic internal table

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

  • Create and run the dynamic program

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

SUBMIT ZDYNPRO.

-


or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

START-OF-SELECTION.

*line = ' CITYFROM CITYTO '.

line = ' AIRPTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

  • WRITE: / wa-cityfrom, wa-cityto.

WRITE 😕 wa-airpto.

ENDLOOP.

ENDIF.

regards

vinod

Read only

Former Member
0 Likes
958

Hi Jonathen,

Have u checked with these link...these will help u with the code, u have written.

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Rgds,

Prakashsingh

Read only

0 Likes
958

Hi, all

thanks for the reply, i have try all the link above. Its working however, i noticed that all the internal table is creating in the field symbols..... After i create the dynamic internal table, there are still lots of process to go before display the output, so its kinda difficult to process the contents of the dynamic internal table..... is there a way to create the internal table not using field symbols.....

Read only

0 Likes
958

field symbols are for dynamic purpose.

i dont think u can do with out field symbols.

Read only

0 Likes
958

Jonathan,

1. why is it a problem to process using field symbols? What is the difficulty?

2. are you sure you need a dynamic table anyway? Your original spec seems to just need an ordinary table.

Read only

0 Likes
958

Hi, Neil,

1. After creating the dynamic table, there are lots of process to modied the value that stored inside the dynamic table field, before writing the output to ALV.

2. Definitely yes, because beside using the dynamic internal table, the other option is using structure, but the problem is everytime the contents of the database being added, i need to change the structure.

Thanks.

Read only

0 Likes
958

but ordinary tables can have their contents changed without having their structure modified.

Dynamic tables are only needed if the columns are going to vary. If you just need different contents you can refresh and rebuild the table as you go along.

Read only

0 Likes
958

Jonathan,

I dont understand this. Why do you want to change a the structure. As per my understanding, this is how I worked with dynamic tables before.

Create a structure with whatever fields u want to work with. Based on the requirements build a field catalog, and create a dynamic table from that field catalog and assign it to a field-symbol.

Thanks,

Read only

0 Likes
958

I need an internal table with dynamic field because the database table contents will be add and remove frequently.......

Neil, may i ask you some question will it affect the performance of the programs if we going to use lots of field symbolds definition in one program ?

Thanks

Read only

0 Likes
958

I think you can maintain (add/delete/change/refresh) your internal table as often as the db activity. I don't see that dynamic tables offer any advantage over conventional ones for this purpose.

Performance may be affected but what do you mean by 'lots'? Maybe you can free/release some data as you go along?

Read only

0 Likes
958

Jonathan,

Infact with the use of field symbols the performance will improve. We can avoid unnecessary statements used for working with internal tables like modify etc...

Thanks,