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

Reg Dynamic Internal Table

Former Member
0 Likes
1,697

Hi,

I created a Dynamic internal table based onthe Blog By Rich Heilman.

I could create it succesfully but

now i wanted to loop through the data and modify the contents

How could it be done

regrads

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,553

Hi,

I gave so that you can know the datatype

Its given as cust_num -


> kunnr

znumber -


> kunnr

In the Run Time ,

The table has data in the following way for first record



KUNNR       0000012234
Name                                      IN0000012234
Country
Kunnr
Meins

but it must Be as



KUNNR       0000012234
Name                                    
Country       IN
Kunnr          0000012234
Meins

please help

16 REPLIES 16
Read only

JozsefSzikszai
Active Contributor
0 Likes
1,553

you'll need a dynamic work area as well for the dynamic internal table and you have to target each field separately, roughly:

LOOP AT dyn_itab ASSIGNING dyn_wa.
ASSIN COMPONENT 'name_of_field' OF STRUCTURE dyn_wa TO <field_symbol>.
<field_symbol> = value.
ENDLOOP.

Read only

Former Member
0 Likes
1,553

Hi

U need to use the field-symbols, I suppose u just have a field-symbol as internal table:

DATA: <WA> TYPE ANY,
           <FIELD> TYPE ANY.

LOOP AT <ITAB> ASSIGNING <WA>.
   ASSIGN COMPONENT <field name> OF STRUCTURE <WA> TO <FIELD>.
   IF <FIELD> = ......
      <FIELD> = .....
  ENDIF. 
ENDLOOP.

If you've an internal table with the structure of your dynamic table u can use it in order to change the data:

DATA: <WA> TYPE ANY,
           <FIELD> TYPE ANY.

LOOP AT <ITAB> ASSIGNING <WA>.
   LOOP AT STRUT_TAB WHERE FIELDNAME = .......
     ASSIGN COMPONENT STRUT_TAB-FIELDNAME OF STRUCTURE <WA> TO <FIELD>.
     CASE STRUT_TAB-FIELDNAME.
        WHEN ......... <FIELD> = ......
        WHEN ........ <FIELD> = ......
     ENDCASE.
  ENDIF. 
  ENDLOOP.
ENDLOOP.

Max

Edited by: max bianchi on Nov 18, 2008 10:31 AM

Read only

Former Member
0 Likes
1,553

Hi,

How to make the changes get reflected into the internal 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 = 4.

     <dyn_field> = 'ABCD'.

   ENDIF.
enddo.

ENDLOOP.

I want the <dyn_field> get updated into the my dynamic internal table

regards

Read only

0 Likes
1,553

if you use:

LOOP AT <dyn_table> INTO <dyn_wa>.

then you need MODIFY statement, to change the dynamic internal table

if you use:

LOOP AT <dyn_table> ASSIGNING <dyn_wa>.

then this will change the internal table directly, you DON'T need to use the MODIFY

Read only

0 Likes
1,553

Hi,

I tried by replacing INTO with ASSIGNING but the changes are not reflected.

please check my code




REPORT  zp_dynamic_internal_table.

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 .
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  PERFORM get_structure.
  PERFORM create_dynamic_itab.
  PERFORM get_data.
*  PERFORM manipulate_data.
  PERFORM write_out.


*&---------------------------------------------------------------------*
*&      Form  get_structure
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
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.                    "get_structure

*&---------------------------------------------------------------------*
*&      Form  create_dynamic_itab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
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.                    "create_dynamic_itab



*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_data.

* Select Data from table.
  SELECT * INTO TABLE <dyn_table>
             FROM (p_table).

ENDFORM.                    "get_data


*&---------------------------------------------------------------------*
*&      Form  write_out
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
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>.
*      ELSEIF sy-index = 4.
*      <dyn_field> = 'ABCD'.
*         WRITE: <dyn_field>.
       ELSE.
        WRITE: <dyn_field>.
      ENDIF.
    ENDDO.
  ENDLOOP.

ENDFORM.                    "write_out


*&---------------------------------------------------------------------*
*&      Form  MANIPULATE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*

form MANIPULATE_DATA .

 LOOP AT <dyn_table> ASSIGNING <dyn_wa>.

 DO .

   ASSIGN COMPONENT sy-index
      OF STRUCTURE <dyn_wa> to <dyn_field>.

   IF sy-subrc <> 0.
     EXIT.
   ENDIF.

   IF sy-index = 4.
     <dyn_field> = 'ABCD'.
*     modify <dyn_table>
   ENDIF.

ENDDO.

ENDLOOP.



endform.                    " MANIPULATE_DATA

regards

Read only

0 Likes
1,553

the line:

  • PERFORM manipulate_data.

is commented!

Read only

0 Likes
1,553

Ya...

I checked it soon after I posted.......

I got one more problem

My DataBase Table consists of the following fields and sample data as follws



KUNNR           NAME1      LAND1      KUNNR        MEINS
0000012234                         IN       0000012234
0000123456    fghhgldf          DE        0000123456
0545658778    vvklfbjkfjbo;      IN         0545658778
2356152FGY  sgdfghgtfjhtf     IN         GDDFGBGH
SDAFESGG   RYERYER      IN

But when I update

I am having the O/P as follows



0000012234                               IN0000012234                                   ABCD
0000123456fghhgldf                    DE 0000123456                                 ABCD
0545658778vvklfbjkfj bo;                 IN 0545658778                                ABCD
2356152FGYsgdfghgtfj htf                 IN GDDFGBGH                             ABCD
2356152FGYsgdfghgtfj htf                 IN GDDFGBGH                              ABCD

But the Second Kunnr Is not updated

please Help

Read only

0 Likes
1,553

in DB table you cannot have two fields with the same names. This is also true for internal tables. Check again, how the internal table is built up!

Read only

Former Member
0 Likes
1,554

Hi,

I gave so that you can know the datatype

Its given as cust_num -


> kunnr

znumber -


> kunnr

In the Run Time ,

The table has data in the following way for first record



KUNNR       0000012234
Name                                      IN0000012234
Country
Kunnr
Meins

but it must Be as



KUNNR       0000012234
Name                                    
Country       IN
Kunnr          0000012234
Meins

please help

Read only

0 Likes
1,553

in the form GET_STRUCTURE, can you do the LOOP like this:

LOOP AT idetails INTO xdetails.
    CLEAR xfc.
    xfc-fieldname = xfc-ref_field = xdetails-name .
    xfc-ref_table   = p_table.
*    xfc-datatype = xdetails-type_kind.
*    xfc-inttype = xdetails-type_kind.
*    xfc-intlen = xdetails-length.
*    xfc-decimals = xdetails-decimals.
    APPEND xfc TO ifc.
  ENDLOOP.

Read only

0 Likes
1,553

Hi

Try to change your routine GET_STRUCTURE:

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-REF_FIELD = xdetails-name.
    xfc-REF_TABLE = p_table.


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

Max

Read only

0 Likes
1,553

Hey ,

Its perfect

Got it

thanks a lot

Could you please explain what did you do ?

xfc-fieldname = xfc-ref_field = xdetails-name .

and one more dbt

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

how do we get the table structure with the above statemnt

regards

Read only

0 Likes
1,553

Hi

Anyway rember if you're using a release greater or equal to 47 u can use an easier way to create an dynamic internal table:

create data dy_table type table of (p_table).
assign dy_table->* to <dyn_table>.

Read only

0 Likes
1,553

xfc-fieldname = xfc-ref_field = xdetails-name .

the above means that the value of xdetails-name will be loaded into xfc-ref_field and xfc-fieldname as well (from back to forth). Important is the " xfc-ref_table = p_table." too. These two lines makes that the dynamic internal table is referred (field by field) to the DB table. it is much simplier (and safer) to do this way, than to suffer with each type and decimals and so on...

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

here the method describe_by_name of class cl_abap_typedescr will be called with parameter (p_table) and the ooutgoing result will be loaded into ref_table_des. pls. check the SAPHelp for detailed explanation!

Read only

0 Likes
1,553

Thnks guys

Read only

former_member206439
Contributor
0 Likes
1,553

Hi

sample modification

you can do like this

LOOP AT <f_tab> ASSIGNING <f_line>.

ASSIGN COMPONENT 'WTG004' OF STRUCTURE <f_line> TO <f_field>.

<f_field> = '100.00'.

ENDLOOP.