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

inserting into ztable by dynamic table creation

divya_nayudu
Participant
0 Likes
1,065

We tried inserting data in a z-table by putting dynamic internal table generation code...

The code to generate structure of dynamic table is as follows:

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.

The above code works just fine but in the " ref_table_des->components[].", every datatype of field is either getting doubled or halfed in length. Unable to figure out the reason. has anyone faced such an issue. Inputs would help. Thanks.

7 REPLIES 7
Read only

divya_nayudu
Participant
0 Likes
898

This requirement needs to be fulfilled today itself. Could anyone provide any input even close to it?

Read only

0 Likes
898

Hi,

If you notice it is only applicable for character-like fixed length data objects namely of type c, n. This is because you are working in Unicode system which stores such characters in 16-bit (2 bajts) length. So when you declare


data char_var(1) type c value 'A'.

This one char occupies memory of 2 bajts (for non-Unicode it is 1bajt). That's why you are getting your length doubled. What I suggest is to check inside the loop if type is c or n, if so devide it by 2. This length field which you can see in idetails is a bit confusing, because it return phisicall memory length this data object occupies, which is different from your declared length. For all the otehrs types like i, p etc it is no longer valid as they always occupy samo length memory independently of system (Unicode/non-Unicode)

Hope this is now clear.

Marcin

Read only

0 Likes
898

Even I thought of the divide thing, But even if we apply that or fetch the exact length from DD03 table, there is anothr function module being called in the code:

DATA: dy_table TYPE REF TO data.

ifc TYPE lvc_t_fcat.

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

Here, all the datatypes are now getting correct length, except for the datatype "DEC" and "CURR". In the table "IFC", the values are all correct. But after calling the above mentioned FM, values change for DEC and CURR.

In case of these two, the length changes from 13 to 7 in CURR. And from 5 to 3 for DEC. After this, I am unable to set its exact length. Not sure how to set it for "dy_table" being returned. Went inside the code but found no way out.

Read only

Former Member
0 Likes
898

Hi,

Check the below code, this may help you

FIELD-SYMBOLS: <outtab> TYPE ANY TABLE,
      <l_line>  TYPE ANY,
      <l_field> TYPE ANY.
DATA: new_table TYPE REF TO data.
DATA: new_line  TYPE REF TO data.
DATA: it_fieldcat TYPE lvc_t_fcat.
DATA: wa_fieldcat TYPE lvc_s_fcat.
...

CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = it_fieldcat
        IMPORTING
          ep_table        = new_table.

ASSIGN new_table->* TO <outtab>.
CREATE DATA new_line LIKE LINE OF <outtab>.
ASSIGN new_line->* TO <l_line> .


SELECT matnr aenam UP TO 5 ROWS
FROM mara
INTO TABLE itab.



LOOP AT itab.
  ASSIGN COMPONENT 'MATNR' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = itab-matnr.
  ASSIGN COMPONENT 'AENAM' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = itab-aenam.

  INSERT <l_line> INTO TABLE <outtab>.
ENDLOOP.

Regards,

Manoj Kumar P

Read only

0 Likes
898

am unabl to understnd how to fit your piece of code in mine...How do i change length of datatype of <dyntable>

Read only

0 Likes
898

Its working now. Fetched the exact length from DD03 table. Worked out with another short piece of code.Thanks to all.

Read only

former_member222860
Active Contributor
0 Likes
898

Hey,

Even i have the same unreolved issue for QUAN and CURR fields

can u explain a bit clear, how u resolved this

thanks in advance

Mahesh