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 structure

Former Member
0 Likes
1,734

can anybody give me a sample code of creating a dynamic internal table structure? what im getting from searching the net is that it acquires the table structure of the table you will define in the code. but what i need to do is that i should be the one to define the fields, type, etc....

18 REPLIES 18
Read only

Former Member
0 Likes
1,701

Hi,

Declaration for Dynamic Table:-

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

<dyn_table1> TYPE STANDARD TABLE,

<dyn_wa1>,

<dyn_wa>.

FIELD-SYMBOLS: <l_fs_dyn> TYPE ANY,

<l_fs_bom> TYPE ANY.

*---Dynamic Internal Table

DATA: dy_table TYPE REF TO data,

dy_table1 TYPE REF TO data,

dy_line TYPE REF TO data.

Creation of Dynamic Internal Table:-

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = fcat (for which internal table needs to be built)

IMPORTING

ep_table = dy_table

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2

.

IF sy-subrc <> 0.

ENDIF.

Population of Dynamic Internal Table:-

CLEAR: l_tabix,

w_tabix.

ASSIGN dy_table->* TO <dyn_table>.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

ASSIGN dy_table->* TO <dyn_table1>.

CREATE DATA dy_line LIKE LINE OF <dyn_table1>.

ASSIGN dy_line->* TO <dyn_wa1>.

LOOP AT it_parent.

l_tabix = sy-tabix.

LOOP AT fcat INTO ls_fcat TO 23."WHERE col_pos <= '19'.

ASSIGN COMPONENT ls_fcat-fieldname

OF STRUCTURE <dyn_wa> TO <l_fs_dyn>.

ASSIGN COMPONENT ls_fcat-fieldname

OF STRUCTURE it_parent TO <l_fs_bom>.

<l_fs_dyn> = <l_fs_bom>.

ENDIF.

ENDLOOP

APPEND <dyn_wa> TO <dyn_table>.

CLEAR: <dyn_wa>,

<dyn_wa1>,

<l_fs_bom>,

<l_fs_dyn>.

Build your fcat as field type etc.....

Thanks & Regards,

Ruchi Tiwari

Read only

0 Likes
1,701

hi ruchi, im a newbie in this language. some symbols are not defined in your sample code. can i have the correct code?

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
1,701

Hi,

Refer:

https://www.sdn.sap.com/irj/scn/advancedsearch?cat=sdn_wiki&query=dynamicinternaltable&adv=false&sortby=cm_rnd_rankvalue

Hope this helps you.

Regards,

Tarun

Read only

0 Likes
1,701

Hi Ian,

Let me know specificly in which part are u facing problem so that i can elaborate more.....

Thanks & Regards,

Ruchi Tiwari

Read only

Former Member
0 Likes
1,701

hi,

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE ,

<dyn_wa>.

*---Dynamic Internal Table

DATA: dy_table TYPE REF TO data,

dy_line TYPE REF TO data.

ASSIGN dy_table->* TO <dyn_table>.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

thanks

Read only

former_member632729
Contributor
0 Likes
1,701

Hi Dude,

Go through this Link :

Field Symbols

Link:[http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/content.htm]

Link:[http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb387a358411d1829f0000e829fbfe/content.htm]

Example


DATA: wa(10) VALUE '0123456789'.

DATA: BEGIN OF line1,
         col1(3),
         col2(2),
         col3(5),
      END OF line1.

DATA: BEGIN OF line2,
         col1(2),
         col2 LIKE sy-datum,
      END OF line2.

FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,
               <f2> STRUCTURE line2 DEFAULT wa.

WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,
       / <f2>-col1, <f2>-col2.

Example using the correct syntax (TYPE and CASTING):

DATA: wa(10) VALUE '0123456789'.

DATA: BEGIN OF line1,
         col1(3),
         col2(2),
         col3(5),
      END OF line1.

DATA: BEGIN OF line2,
         COL1(2),
         COL2 LIKE sy-datum,
      END OF line2.

FIELD-SYMBOLS: <f1> LIKE line1.
ASSIGN wa TO <f1> CASTING.

FIELD-SYMBOLS: <f2> LIKE line2.
ASSIGN wa TO <f2> CASTING.

WRITE: / <f1>-col1, <F1>-col2, <F1>-col3,
       / <f2>-col1, <F2>-col2.

Edited by: Raghunath Shyamala on Mar 23, 2009 4:44 AM

Read only

Former Member
0 Likes
1,701

is it possible to have an indefinite number of fields?

Read only

0 Likes
1,701

Hi,

You have to determine the number of fields atleast at run time dynamically and prepare your fieldcatalog that needs to be passed to create_table method.

Regards,

Raghavendra

Read only

Former Member
0 Likes
1,701

Hi,

u can make a dynamic internal table with unlimited fields.

check this code.


DATA: struct_type TYPE REF TO cl_abap_structdescr,
      comp_tab    TYPE cl_abap_structdescr=>component_table,
      dref        TYPE REF TO data,
      itab_type   TYPE REF TO cl_abap_tabledescr,
      wa_comp     LIKE LINE OF comp_tab,
      index       TYPE char10.

FIELD-SYMBOLS : <tab> TYPE STANDARD TABLE,
                <wa> TYPE ANY.

PARAMETER col TYPE i.

"Here u have to define the fields of the internal table.
DO col TIMES.
  index = sy-index.
  CONCATENATE 'COL' index INTO wa_comp-name.                       "Field name
  CONDENSE wa_comp-name NO-GAPS.
  wa_comp-type ?= cl_abap_datadescr=>describe_by_name( 'I' ).     "Field data type
  APPEND  wa_comp TO comp_tab.
ENDDO.

struct_type = cl_abap_structdescr=>create( comp_tab ).
itab_type   = cl_abap_tabledescr=>create( struct_type ).
CREATE DATA dref TYPE HANDLE itab_type.
ASSIGN dref->* TO <tab>.                                 "This is the dynamic Internal table
CREATE DATA dref TYPE HANDLE struct_type.
ASSIGN dref->* TO <wa>.                                  "This is the work area for the above table.

set debugger and see..

Read only

0 Likes
1,701

hi Sukriti,

how am i going to put values to the work area or table?

Read only

0 Likes
1,701

Hi,

Do like this....


FIELD-SYMBOLS: <fs> TYPE any.


LOOP AT comp_tab INTO wa_comp.
  ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <fs>.       "<---get by field name
  IF <fs> IS ASSIGNED.
    <fs> = value.           "<----Value
    UNASSIGN <fs>.
  ENDIF.
ENDLOOP.

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
1,701

Hi,

Refer wiki code for sample dynamic internal table

https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/sample%252b%252bdynamic%252binternal%252...

Hope this helps you.

Regards,

Tarun

Read only

0 Likes
1,701

sorry, but i still dont know how those values to work area and then to internal table and what if i have 10 fields to put values in for example?

Edited by: lan sdng on Mar 26, 2009 6:43 AM

Read only

I355602
Product and Topic Expert
Product and Topic Expert
Read only

0 Likes
1,701

FIELD-SYMBOLS: <fs> TYPE any.
  
LOOP AT comp_tab INTO wa_comp.
  ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <fs>.       "<---get by field name
  IF <fs> IS ASSIGNED.
    <fs> = value.           "<----Value
    UNASSIGN <fs>.
  ENDIF.
ENDLOOP.

the field names r stored in the table 'comp_tab'.

so looping at this table gives u all the components of the work area. as <wa> is assigned type ANY. so u cannot get the field names diretly..eg. <wa>-fld_name.

so u have to do like this....

ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <fs>.

here u re taking one field 'wa_comp-name' at a time, and is being assigned to <fs>. now u can put values into <fs> which refers to a field of <wa> at runtime.

U can check with debugger....

at the end of loop use APPEND <wa> to <tab>.

so if u want to pu values from an internal table to this dynamic tabe...use like this...


FIELD-SYMBOLS: <fs_wa> TYPE any,
            <fs1> type any.

LOOP at itab ASSIGNING <fa_wa>.  
  LOOP AT comp_tab INTO wa_comp.
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <fs>.       "<---get by field name
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <fs_wa> TO <fs1>.       "<---get by field name
    IF <fs> IS ASSIGNED.
      <fs> = <fs1>.           "<----Value
      UNASSIGN <fs>.
    ENDIF.
  ENDLOOP.
  APPEND <WA> to <dyn_tab>.
ENDLOOP.

Read only

0 Likes
1,701

how come the values does not appear in the internal table if i do this?

LOOP AT comp_tab INTO wa_comp.

ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <fs>.

IF <fs> IS ASSIGNED.

<fs> = 1.

ENDIF.

ENDLOOP.

APPEND <wa> TO <tab>.

Read only

0 Likes
1,701

ur code is OK. values are entering in the table....

Read only

Former Member
0 Likes
1,701

Hi,

Kindly go through these below threads:

https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/creating%252bflat%252band%252bcomplex%25...

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Hope it helps

Regards

Mansi