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
641

Hi,

Can we create a dynamic internal table using data of an existing internal table.

i.e. if itab1 is have data like :

row col value

-


1 1 ab

1 2 cd

1 3 ef

2 1 gh

2 2 ij

2 3 kl

and I want to create itab2 dynamically like this :

row col1 col2 col3

-


1 ab cd ef

2 gh ij kl

The issue lies only while creating internal table using non DDIC structure i.e. internal table of program.

Thanks

Vipin

4 REPLIES 4
Read only

Former Member
0 Likes
534

Hi Vipin,

The followin link may be hlep to you for your requriement.

http://www.sap-img.com/ab030.htm

and this thread also

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Warm Regards,

Vijay

Read only

0 Likes
534

hi,

Sample piece of code of how to create a dynamic internal table.

  LOOP AT it_fields INTO wa_fields TO 150.
    lv_cnt = lv_cnt + 1.
    wa_cat-tabname = p_tab.
    wa_cat-fieldname = wa_fields-fieldname.
    wa_cat-col_pos = lv_cnt.
    wa_cat-inttype    = wa_fields-inttype.
    wa_cat-datatype = wa_fields-datatype.
    wa_cat-intlen = wa_fields-intlen.
    wa_cat-seltext = wa_fields-fieldname.
    wa_cat-decimals = wa_fields-decimals.
    wa_cat-ref_field = wa_fields-fieldname.
    wa_cat-ref_table = p_tab.
    APPEND wa_cat TO it_cat.
    CLEAR wa_cat.

    wa_fname-fld = wa_fields-fieldname.
    APPEND wa_fname TO it_fname.
    CLEAR wa_fname.
  ENDLOOP.

**--Create a dynamic internal table with the 150 fields
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_cat
    IMPORTING
      ep_table        = d_ref.

  ASSIGN d_ref->* TO <f_fs>.

**--Populate the Where clause as a string
  LOOP AT s_where.
    CONCATENATE lv_where s_where-low INTO lv_where SEPARATED BY space.
  ENDLOOP.

**--Select the data from the table given as input and populate
**--it into the dynamic internal table created based on the where condition
  SELECT (it_fname)
    FROM (p_tab)
    INTO CORRESPONDING FIELDS OF TABLE <f_fs>
   WHERE (lv_where).
**--If no entries are found that satisfies the selection criteria
  IF sy-subrc <> 0.
    MESSAGE i000(zz) WITH 'No data found'(010).
  ENDIF.

Regards

Sailaja.

Read only

p291102
Active Contributor
0 Likes
534

Hi,

This is the sample report for DYNAMIC ALV report.

REPORT YMS_DYNAMICALV.

TYPE-POOLS: SLIS.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,

<DYN_WA>.

DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,

IT_FLDCAT TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS: P_FLDS(5) TYPE C.

SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

  • build the dynamic internal table

PERFORM BUILD_DYN_ITAB.

  • write 5 records to the alv grid

DO 5 TIMES.

PERFORM BUILD_REPORT.

ENDDO.

  • call the alv grid.

PERFORM CALL_ALV.

************************************************************************

  • Build_dyn_itab

************************************************************************

FORM BUILD_DYN_ITAB.

DATA: NEW_TABLE TYPE REF TO DATA,

NEW_LINE TYPE REF TO DATA,

WA_IT_FLDCAT TYPE LVC_S_FCAT.

  • Create fields .

DO P_FLDS TIMES.

CLEAR WA_IT_FLDCAT.

WA_IT_FLDCAT-FIELDNAME = SY-INDEX.

WA_IT_FLDCAT-DATATYPE = 'CHAR'.

WA_IT_FLDCAT-INTLEN = 5.

APPEND WA_IT_FLDCAT TO IT_FLDCAT .

ENDDO.

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

ENDFORM. "build_dyn_itab

*********************************************************************

  • Form build_report

*********************************************************************

FORM BUILD_REPORT.

DATA: FIELDNAME(20) TYPE C.

DATA: FIELDVALUE(5) TYPE C.

DATA: INDEX(3) TYPE C.

FIELD-SYMBOLS: <FS1>.

DO P_FLDS TIMES.

INDEX = SY-INDEX.

  • Set up fieldvalue

CONCATENATE 'FLD' INDEX INTO

FIELDVALUE.

CONDENSE FIELDVALUE NO-GAPS.

  • <b> assign component index of structure <dyn_wa> to <fs1>.

  • <fs1> = fieldvalue.</b>

ENDDO.

  • Append to the dynamic internal table

APPEND <DYN_WA> TO <DYN_TABLE>.

ENDFORM. "build_report

************************************************************************

  • CALL_ALV

************************************************************************

FORM CALL_ALV.

DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.

DO P_FLDS TIMES.

CLEAR WA_CAT.

WA_CAT-FIELDNAME = SY-INDEX.

WA_CAT-SELTEXT_S = SY-INDEX.

WA_CAT-OUTPUTLEN = '5'.

APPEND WA_CAT TO ALV_FLDCAT.

ENDDO.

  • Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IT_FIELDCAT = ALV_FLDCAT

TABLES

T_OUTTAB = <DYN_TABLE>.

ENDFORM. "call_alv

Thanks,

Sankar M

Read only

Former Member
0 Likes
534

Hi vipin,

Yes , you can create a Dynamic Internal table .Just chek out this program .

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.

regards,

venkat