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: 

how to create dynamic internal table

Former Member
0 Kudos

hi all,

how to create dynamic internal table

if possible i need even the sample code

Thanks in advance

Points will be rewarded

1 ACCEPTED SOLUTION

Pawan_Kesari
Active Contributor
0 Kudos

REPORT zpwtest .

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



9 REPLIES 9

Former Member
0 Kudos

go through this

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/dynamic%2binternal%2btable">https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/dynamic%2binternal%2btable</a>

regards

shiba dutta

Pawan_Kesari
Active Contributor
0 Kudos

REPORT zpwtest .

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



0 Kudos

Its easy but i could not understand at the ASSIGN COMPONENT 1 OF STRUCTURE <fs_2> TO <fs_3>. can any one explain

Former Member
0 Kudos

Hi,

try this short example:

PARAMETER: P_DBTAB LIKE DATABROWSE-TABLENAME DEFAULT 'MARA'.

*

DATA: GDO_DATA TYPE REF TO DATA.

FIELD-SYMBOLS: <GT_ITAB> TYPE TABLE.

*

CREATE DATA GDO_DATA TYPE TABLE OF (P_DBTAB).

ASSIGN GDO_DATA->* TO <GT_ITAB>.

*

SELECT * FROM (P_DBTAB) UP TO 100 ROWS INTO TABLE <GT_ITAB>.

Regards, Dieter

Former Member
0 Kudos

Hi Jayasree,

Dynamic internal tables can be created using <b>CL_ALV_TABLE_CREATE</b> class and method <b>CREATE_DYNAMIC_TABLE.</b>

Just fill the field catalog(IT_FIELDCATALOG) in the parameter of method (like you do in normal ALV reports) and it creates a pointer to dynamic data table.

Check this code.

report  ytest.
data: lt_fieldcatalog type lvc_t_fcat.
data: ls_fieldcatalog type lvc_s_fcat.
field-symbols: <fs_data> type ref to data.
field-symbols: <fs_1>.
field-symbols: <fs_2> type any table.
field-symbols: <fs_3> type ypoll.
 
 
data: lt_data type ref to data.
 
assign lt_data to <fs_data>.
 
ls_fieldcatalog-fieldname = 'MANDT'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'POLLID'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'TEAM'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'INITIATOR'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'DESCRIPTION'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'APPROVED'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'INITIATED_DATE'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'END_DATE'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
ls_fieldcatalog-fieldname = 'WINNER'.
ls_fieldcatalog-tabname   = 'LT_TAB'.
append ls_fieldcatalog to lt_fieldcatalog.
 
 
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.
 
assign <fs_data>->* to <fs_1>.
assign <fs_1> to <fs_2>.
 
loop at <fs_2> assigning <fs_3>.
  write: <fs_3>-pollid.
endloop.

Thanks,

Vinay

Former Member
0 Kudos

HI,

DATA:DB_TABLE(30) TYPE C value 'MARA'.

DATA FCAT1 TYPE LVC_T_FCAT.

DATA:DYN_ITAB TYPE REF TO DATA,"holding the dynamic internal table

WA TYPE REF TO DATA."holding the wa for dynamic internal table

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"creating dynamic internal table using fieldcat

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>."creating internal table by refering the dynamically generated internal table structure

CREATE DATA WA LIKE LINE OF <DISP_TABLE>."creating work area for the internal table

ASSIGN WA->* TO <WA>.

SELECT * FROM (DB_TABLE) INTO <WA>."filling the internal table

APPEND <WA> TO <DISP_table>.

ENDSELECT.

<b>reward if helpful</b>

rgds,

bharat.

Former Member
0 Kudos

hi,

u can use the


  data : it_tabdescr type abap_compdescr_tab,
  wa_tabdescr type abap_compdescr.
  data : ref_table_descr type ref to cl_abap_structdescr.

* Return structure of the table.
  ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
  it_tabdescr[] = ref_table_descr->components[].
  loop at it_tabdescr into wa_tabdescr.
  clear wa_fieldcat.
  wa_fieldcat-fieldname = wa_tabdescr-name .
  wa_fieldcat-datatype = wa_tabdescr-type_kind.
  wa_fieldcat-inttype = wa_tabdescr-type_kind.
  wa_fieldcat-intlen = wa_tabdescr-length.
  wa_fieldcat-decimals = wa_tabdescr-decimals.
  append wa_fieldcat to it_fieldcat.
  endloop.


* Create dynamic internal table and assign to Field-Symbol
  call method cl_alv_table_create=>create_dynamic_table
  EXPORTING
  it_fieldcatalog = it_fieldcat
  IMPORTING
  ep_table = dyn_table.
  assign dyn_table->* to <fs_table>.
* Create dynamic work area and assign to Field Symbol
  create data dyn_line like line of <fs_table>.
  assign dyn_line->* to <fs_wa>.




0 Kudos

thanks if am having 8 structures can i use the one dynamic internal table for all these structures

0 Kudos

ya.. bt first you have to create the internal table for field catalog according to ur requirement and logic.. then you have to pass that internal table and you can get ur requirement...