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: 

dynamic internal table

Former Member
0 Kudos

Hi all,

how i create an dynamic internal table.

warm regards

6 REPLIES 6

Former Member
0 Kudos

Hi,

u can create dynamic itab using method:

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

to execute this u should build a field datalog and pass it to this method, and complete code is as follows


*&---------------------------------------------------------------------*
*& Report  ZMBJ_ITAB_CROSSTAB
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  zmbj_itab_crosstab
             no standard page heading.

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

form write_out.
* 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.
endform.

Jogdand M B

Former Member
0 Kudos

Hello,

Check this simple report.


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.

Vasanth

Former Member
0 Kudos

Hi,

In release WAS 620, you can do so by using : CREATE DATA new_line TYPE table of (p_tab). This syntax was not allowed in earlier releases. The FM Y_CREATE_DYNAMIC_TABLE can help you to achieve this goal.

Thanks,

Sandeep.

Former Member
0 Kudos

hi,

try this..

=====================================

REPORT zmaschl_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

Regards

Former Member
0 Kudos

HI,

see this code.

REPORT ZDYN_ITAB.

PARAMETERS:DB_TABLE(30).

DATA FCAT1 TYPE LVC_T_FCAT.

DATA:DYN_ITAB TYPE REF TO DATA,

WA TYPE REF TO DATA.

FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,

<WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = DB_TABLE

CHANGING

CT_FIELDCAT = FCAT1[].

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>.

CREATE DATA WA LIKE LINE OF <DISP_TABLE>.

ASSIGN WA->* TO <WA>.

SELECT * FROM (db_table) INTO <WA>.

APPEND <WA> TO <DISP_table>.

ENDSELECT.

<b>reward if helpful</b>

rgds,

bharat.

Former Member
0 Kudos

Hi,

Try this code.

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.

Thanks,

Sandeep.