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: 

want to create an internal table at runtime

Former Member
0 Kudos

Hi all ,

I am trying to create an itab within my scheme by making use of a table loop.

[code]

data :

llv_imp type table of RSIMP,

gt_imp_par type string occurs 0.

Field-Symbols :

<fs_imp_par> like RSIMP,

<fs_struct> type string.

Form inst_par_types

Changing

lv_imp like llv_imp

lt_imp_par like gt_imp_par.

Data :

lv_str(72) type c ,

lv_line type I ,

lv_delim type C value ',' .

Describe table lv_imp lines lv_line.

Loop at lv_imp assigning <fs_imp_par>.

if sy-tabix = lv_line.

lv_delim = '.'.

endif.

if <fs_imp_par>-default is not initial.

concatenate <fs_imp_par>-parameter 'TYPE' <fs_imp_par>-typ

'VALUE' <fs_imp_par>-default lv_delim into

lv_str SEPARATED BY space.

else.

concatenate <fs_imp_par>-parameter 'TYPE' <fs_imp_par>-typ

lv_delim into lv_str SEPARATED BY space.

endif.

append lv_str to lt_imp_par.

clear lv_str.

endloop.

Types :

Begin of l_tabname ,

<b><i>{T$lt_imp_par$$$&lt_imp_par&$$}</i></b>

End of l_tabname.

EndForm.

[/code]

But when i instantiate this scheme it dumps as the highlighted line creates 2 different definitions of same variable lt_imp_par (1 as var & another one as table).

What shall I do to get the solution?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Try with this example...

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.

Regards!

Andrea.-

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Please see the following for creating an internal table at runtime.



report zrich_0002.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.

data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.

type-pools : abap.

data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.

selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.


* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].

loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-inttype = wa_details-type_kind.
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.

* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.

assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.

* Select Data from table.
select * into corresponding fields of table <dyn_table>
           from (p_table).

* 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.

Regards,

Rich HEilman

Former Member
0 Kudos

Try with this example...

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.

Regards!

Andrea.-

Former Member
0 Kudos

Hi,

please use below logic

DATA V_FORM TYPE P.

DATA: SYNTAX_CHECK_MSG(240),

SYNTAX_CHECK_LINE TYPE I,

SYNTAX_CHECK_WORD(72).

DATA SOURCE_TAB(72) OCCURS 0 WITH HEADER LINE.

DATA V_SPACE TYPE C VALUE ''''.

APPEND 'Program zformula.' TO SOURCE_TAB..

APPEND 'form calculate_formula changing form_value.' TO SOURCE_TAB.

APPEND 'compute form_value = ' TO SOURCE_TAB.

CONCATENATE V_SPACE '67.341667' V_SPACE INTO SOURCE_TAB.

APPEND SOURCE_TAB.

SOURCE_TAB = '+'.

APPEND SOURCE_TAB.

CONCATENATE V_SPACE '1.240000 ' V_SPACE INTO SOURCE_TAB.

APPEND SOURCE_TAB.

APPEND '.' TO SOURCE_TAB.

APPEND 'ENDFORM.' TO SOURCE_TAB.

SYNTAX-CHECK FOR SOURCE_TAB MESSAGE SYNTAX_CHECK_MSG

LINE SYNTAX_CHECK_LINE

WORD SYNTAX_CHECK_WORD.

DATA Z_SEQ_NO(4) TYPE N.

CALL FUNCTION 'NUMBER_GET_NEXT'

EXPORTING

NR_RANGE_NR = '01'

OBJECT = 'ZSDFORMDYN'

IMPORTING

NUMBER = Z_SEQ_NO.

DATA Z_REPID LIKE SY-REPID.

CONCATENATE 'Z@><'

Z_SEQ_NO

INTO Z_REPID.

INSERT REPORT Z_REPID FROM SOURCE_TAB.

GENERATE REPORT Z_REPID.

IF SY-SUBRC NE 0.

DO 1000 TIMES.

GENERATE REPORT Z_REPID.

IF SY-SUBRC = 0.

EXIT.

ENDIF.

ENDDO.

ENDIF.

PERFORM CALCULATE_FORMULA IN PROGRAM (Z_REPID)

CHANGING V_FORM.

Regards,

Amole