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

Dynamic Internal table

Former Member
0 Likes
1,043

Hi Friends,

Can anyone of you help me in creating and using the dynamic internal table in ABAP?

Thanks and Regards,

Venkatesh Babu E

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
807

Please use this code.

for above WAS 6.20 .

report yes_tjung_sflight_test.

      • Data Declaration

data: sflighttype type ref to cl_abap_structdescr,

tabletype type ref to cl_abap_tabledescr,

comp_tab type cl_abap_structdescr=>component_table,

new_comp_tab like comp_tab,

linetype type ref to cl_abap_structdescr,

dref type ref to data.

      • Field Symbols

field-symbols: <wa_comp> like line of comp_tab.

field-symbols: <table> type any table.

sflighttype ?= cl_abap_typedescr=>describe_by_name('SFLIGHT').

comp_tab = sflighttype->get_components( ).

loop at comp_tab assigning <wa_comp>.

case <wa_comp>-name.

when 'CARRID' or 'CONNID' or 'FLDATE' or 'PRICE' or 'CURRENCY'.

append <wa_comp> to new_comp_tab.

endcase.

endloop.

linetype = cl_abap_structdescr=>create( new_comp_tab ).

tabletype = cl_abap_tabledescr=>create(

p_line_type = linetype

p_table_kind = cl_abap_tabledescr=>tablekind_std ).

create data dref type handle tabletype.

assign dref->* to <table>.

select * from sflight into corresponding fields of table <table>.

field-symbols: <wa_data> type any.

field-symbols: <wa_field> type any.

loop at <table> assigning <wa_data>.

write: /.

loop at new_comp_tab assigning <wa_comp>.

assign component sy-tabix of structure <wa_data> to <wa_field>.

write: <wa_field>.

endloop.

endloop.

For was 6.20

      • Tables

DATA: LT_DATA type ref to DATA.

DATA: LT_FIELDCATALOG type LVC_T_FCAT.

      • Structure

DATA: LS_FIELDCATALOG type LVC_S_FCAT.

      • Data References

DATA: NEW_LINE type ref to data.

      • Field Symbols

FIELD-SYMBOLS: <FS_DATA> type ref to DATA,

<FS_1> type any table,

<FS_2>,

<FS_3>.

LS_FIELDCATALOG-FIELDNAME = 'MANDT'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CARRID'. "Fieldname

LS_FIELDCATALOG-INTTYPE = 'C'. "Internal Type C-> Character

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CONNID'.

LS_FIELDCATALOG-INTTYPE = 'N'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'FLDATE'.

LS_FIELDCATALOG-INTTYPE = 'D'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'PRICE'.

LS_FIELDCATALOG-INTTYPE = 'P'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CURRENCY'.

LS_FIELDCATALOG-INTTYPE = 'C'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

assign LT_DATA to <FS_DATA>.

call method cl_alv_table_create=create_dynamic_table

exporting

it_fieldcatalog = LT_FIELDCATALOG

importing

ep_table = FS_DATA

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

      • So <FS_1> now points to our dynamic internal table.

assign <FS_DATA>->* to <FS_1>.

      • Next step is to create a work area for our dynamic internal table.

create data NEW_LINE like line of <FS_1>.

      • A field-symbol to access that work area

assign NEW_LINE->* to <FS_2>.

      • And to put the data in the internal table

select MANDT CARRID CONNID FLDATE PRICE CURRENCY

from SFLIGHT

into corresponding fields of table <FS_1>.

      • Access contents of internal table

loop at <FS_1> assigning <FS_2>.

assign component 1 of structure <FS_2> to <FS_3>.

write: / <FS_3>.

endloop.

data: lv_tablename type string value 'SFLIGHT'.

data: lv_dref type ref to DATA.

CREATE DATA lv_dref type table of (lv_tablename).

FIELD-SYMBOLS: <FS> TYPE STANDARD TABLE.

ASSIGN lv_dref->* TO <FS>.

select *

from sflight

into table <FS>.

Please reward if useful.

5 REPLIES 5
Read only

Former Member
0 Likes
807
REPORT zzz_test1
             NO STANDARD PAGE HEADING
             MESSAGE-ID zcs_c2c_001.

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.

Reward if this helsp.

Read only

0 Likes
807

give any table name at selection screen as per ur requirement.


parameter p_table type tabname.

field-symbols <tab> type table.
field-symbols <tab1> type any.


types: begin of itab,
      t_name type tabname,
      t_ref type ref to data,
      end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.
perform print_table using p_table.
*&---------------------------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM fetch_data  USING P_TABLE1 type tabname.

data itab2 type itab.
itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM.                    " fetch_data
*&---------------------------------------------------------------------*
*&      Form  print_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM print_table  USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.
DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.
*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.
READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.
*WRITE:/ p_table1.
NEW-LINE.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: <field>,'    '.
ENDDO.
ENDDO.
ENDFORM.                    " print_table

Read only

Former Member
0 Likes
808

Please use this code.

for above WAS 6.20 .

report yes_tjung_sflight_test.

      • Data Declaration

data: sflighttype type ref to cl_abap_structdescr,

tabletype type ref to cl_abap_tabledescr,

comp_tab type cl_abap_structdescr=>component_table,

new_comp_tab like comp_tab,

linetype type ref to cl_abap_structdescr,

dref type ref to data.

      • Field Symbols

field-symbols: <wa_comp> like line of comp_tab.

field-symbols: <table> type any table.

sflighttype ?= cl_abap_typedescr=>describe_by_name('SFLIGHT').

comp_tab = sflighttype->get_components( ).

loop at comp_tab assigning <wa_comp>.

case <wa_comp>-name.

when 'CARRID' or 'CONNID' or 'FLDATE' or 'PRICE' or 'CURRENCY'.

append <wa_comp> to new_comp_tab.

endcase.

endloop.

linetype = cl_abap_structdescr=>create( new_comp_tab ).

tabletype = cl_abap_tabledescr=>create(

p_line_type = linetype

p_table_kind = cl_abap_tabledescr=>tablekind_std ).

create data dref type handle tabletype.

assign dref->* to <table>.

select * from sflight into corresponding fields of table <table>.

field-symbols: <wa_data> type any.

field-symbols: <wa_field> type any.

loop at <table> assigning <wa_data>.

write: /.

loop at new_comp_tab assigning <wa_comp>.

assign component sy-tabix of structure <wa_data> to <wa_field>.

write: <wa_field>.

endloop.

endloop.

For was 6.20

      • Tables

DATA: LT_DATA type ref to DATA.

DATA: LT_FIELDCATALOG type LVC_T_FCAT.

      • Structure

DATA: LS_FIELDCATALOG type LVC_S_FCAT.

      • Data References

DATA: NEW_LINE type ref to data.

      • Field Symbols

FIELD-SYMBOLS: <FS_DATA> type ref to DATA,

<FS_1> type any table,

<FS_2>,

<FS_3>.

LS_FIELDCATALOG-FIELDNAME = 'MANDT'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CARRID'. "Fieldname

LS_FIELDCATALOG-INTTYPE = 'C'. "Internal Type C-> Character

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CONNID'.

LS_FIELDCATALOG-INTTYPE = 'N'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'FLDATE'.

LS_FIELDCATALOG-INTTYPE = 'D'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'PRICE'.

LS_FIELDCATALOG-INTTYPE = 'P'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

LS_FIELDCATALOG-FIELDNAME = 'CURRENCY'.

LS_FIELDCATALOG-INTTYPE = 'C'.

append LS_FIELDCATALOG to LT_FIELDCATALOG.

assign LT_DATA to <FS_DATA>.

call method cl_alv_table_create=create_dynamic_table

exporting

it_fieldcatalog = LT_FIELDCATALOG

importing

ep_table = FS_DATA

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

      • So <FS_1> now points to our dynamic internal table.

assign <FS_DATA>->* to <FS_1>.

      • Next step is to create a work area for our dynamic internal table.

create data NEW_LINE like line of <FS_1>.

      • A field-symbol to access that work area

assign NEW_LINE->* to <FS_2>.

      • And to put the data in the internal table

select MANDT CARRID CONNID FLDATE PRICE CURRENCY

from SFLIGHT

into corresponding fields of table <FS_1>.

      • Access contents of internal table

loop at <FS_1> assigning <FS_2>.

assign component 1 of structure <FS_2> to <FS_3>.

write: / <FS_3>.

endloop.

data: lv_tablename type string value 'SFLIGHT'.

data: lv_dref type ref to DATA.

CREATE DATA lv_dref type table of (lv_tablename).

FIELD-SYMBOLS: <FS> TYPE STANDARD TABLE.

ASSIGN lv_dref->* TO <FS>.

select *

from sflight

into table <FS>.

Please reward if useful.

Read only

Former Member
0 Likes
807

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

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

<b>REPORT zus_sdn_dynamic_itab_create.

DATA:

gs_fcat TYPE lvc_s_fcat,

gt_fcat TYPE lvc_t_fcat,

gdo_data TYPE REF TO data.

FIELD-SYMBOLS:

<gt_itab> TYPE table.

START-OF-SELECTION.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'MARA'

CHANGING

ct_fieldcat = gt_fcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL FUNCTION

'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'KNB1'

CHANGING

ct_fieldcat = gt_fcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

DELETE gt_fcat WHERE NOT ( fieldname = 'MATNR' OR

fieldname = 'KUNNR' OR

fieldname =

'BUKRS' ).

LOOP AT gt_fcat INTO gs_fcat.

gs_fcat-col_pos = syst-tabix.

MODIFY gt_fcat FROM gs_fcat INDEX syst-tabix.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

  • I_STYLE_TABLE =

it_fieldcatalog = gt_fcat

  • I_LENGTH_IN_BYTE =

IMPORTING

ep_table = gdo_data

  • E_STYLE_FNAME =

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ASSIGN gdo_data->* TO <gt_itab>.

APPEND INITIAL LINE TO <gt_itab>.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'

EXPORTING

it_fieldcat_lvc = gt_fcat

TABLES

t_outtab = <gt_itab>

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

END-OF-SELECTION.</b>

Read only

Former Member
0 Likes
807

Hi,

Here are the steps for creating dynamic internal tables.

1. Create your field catalog either manually or automatically using the function module, LVC_FIELDCATALOG_MERGE. Add more rows to the field catalog table (T_FIELDCAT) at run time.

2. Use the field catalog to create a table dynamically using the method below.

DATA: T_OUTPUT TYPE REF TO DATA

FIELD-SYMBOLS: <T_OUTPUT> TYPE TABLE

Call Method CL_ALV_TABLE_CREATE->CREATE_DYNAMIC_TABLE

Exporting

IT_FIELDCATALOG = T_FIELDCAT

Importing

EP_TABLE = T_OUTPUT

ASSIGN T_OUTPUT->* TO <T_OUTPUT>.

Now the field symbol <T_OUTPUT> is pointing to an output table of the structure that contains the fields which were determined at runtime. Now fill this table with the data and pass <T_OUTPUT> to the method SET_TABLE_FOR_FIRST_DISPLAY and the ALV grid should show the data properly.

Example:

  • the content of itab will be fields of the new table

loop at itab1 into wa1.

Gs_FIELDCAT-TABNAME = 'itab2'.

GS_FIELDCAT-FIELDNAME = wa1-packid.

GS_FIELDCAT-OUTPUTLEN = 2.

GS_FIELDCAT-KEY = space.

GS_FIELDCAT-SELTEXT_L = wa1-packid.

GS_FIELDCAT-COL_POS = 1.

GS_FIELDCAT-JUST = 'L'.

APPEND GS_FIELDCAT TO GT_FIELDCAT.

endloop.

LOOP AT GT_FIELDCAT INTO GS_FIELDCAT.

MOVE-CORRESPONDING GS_FIELDCAT TO ls_fcat.

APPEND ls_fcat TO lt_fieldcat.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = lt_fieldcat

IMPORTING ep_table = t_output.

hope this helps.

Regards,

Kinshuk