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

Setting a TYPE dynamically

Former Member
0 Likes
2,561

Hi All,

I'm trying to create a data with a type setted dynamically.

I have 2 parameters : an internal table (p_reference) and the name (colonne, as a string) of one of its columns.

My code doesnt work because the instruction : "create data: ty_type type (desired_type)"

wont accept these desired_type :

- P(8) DECIMALS 3

- P8

It can only accept 'P'

Can I set the length and the decimals later Or should I change something else to make this code work ?

Thanks a lot,

Jessie

FIELD-SYMBOLS <table_reference>  TYPE STANDARD TABLE.
  ASSIGN p_reference TO <table_reference>.

  FIELD-SYMBOLS <table_reference_fields> TYPE ANY.
  DATA gs_fldname TYPE REF TO data.
  CREATE DATA gs_fldname LIKE LINE OF <table_reference>.
  ASSIGN gs_fldname->* TO <table_reference_fields>.

  FIELD-SYMBOLS <lfs_comp_wa> TYPE abap_compdescr.

  l_descr_ref ?= cl_abap_typedescr=>describe_by_data( <table_reference_fields> ).

  LOOP AT l_descr_ref->components[] ASSIGNING <lfs_comp_wa>.
    IF <lfs_comp_wa>-name = colonne.
      exit.
    endif.
  endloop.

  data desired_type type string.

" This CONCATENATE dont work but this is just an example

  concatenate <lfs_comp_wa>-type_kind 
	      <lfs_comp_wa>-length 
              ' DECIMALS ' 
              <lfs_comp_wa>-decimals
  into desired_type.


  data: ty_type type ref to data .
  field-SYMBOLS <fs_data> type any .
  create data: ty_type type (desired_type) .  " crash :(
  ASSIGN: ty_type->* TO <fs_data> .

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,199

Allowed syntax are

DATA dref TYPE REF TO data.
CREATE DATA dref TYPE p LENGTH 11 DECIMALS 3.
* or
DATA: dref2 TYPE REF TO data,
      len TYPE i VALUE 11,
      dec TYPE i VALUE 3.
CREATE DATA dref2 TYPE p LENGTH len DECIMALS dec.
* or
DATA: dref3 TYPE REF TO data,
      dataelem type c length 30 value 'WRBTR'.
CREATE DATA dref3 TYPE (dataelem).

Regards,

Raymond

9 REPLIES 9
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,200

Allowed syntax are

DATA dref TYPE REF TO data.
CREATE DATA dref TYPE p LENGTH 11 DECIMALS 3.
* or
DATA: dref2 TYPE REF TO data,
      len TYPE i VALUE 11,
      dec TYPE i VALUE 3.
CREATE DATA dref2 TYPE p LENGTH len DECIMALS dec.
* or
DATA: dref3 TYPE REF TO data,
      dataelem type c length 30 value 'WRBTR'.
CREATE DATA dref3 TYPE (dataelem).

Regards,

Raymond

Read only

former_member226519
Active Contributor
0 Likes
2,199

why not reading the data type of the table field?


    select single rollname into lv_type from dd03l
                                        where tabname   = lv_dbtab
                                        and   fieldname = ls_fld_except-fieldname.
    if sy-subrc <> 0.
      exit.
    endif.

    create data f_any type (lv_type).
    assign f_any->* to <f_any>.

Read only

0 Likes
2,199

Thanks both of you,

do you mean that something like this would work :

TYPES : BEGIN OF ts_vbap,
             vbeln   TYPE vbap-vbeln,
             posnr   TYPE vbap-posnr,
         END OF ts_vbap.
DATA : t_vbap    TYPE TABLE OF ts_vbap.

select single rollname into lv_type from dd03l
                                    where tabname   = 't_vbap'
                                    and   fieldname = 'vbeln'.

My internal table t_vbap has no entry in dd03l (maybe it has at runtime ?)

Jessie

Read only

0 Likes
2,199

you have to refer to a DDIC table, VBAP in your case.

Read only

0 Likes
2,199

Sure. Thanks for the tip, it will be useful later, but in this case I cannot refer to VBAP :

I am working with an internal table : can I find the name of the table to which the column refers and then use it and your solution ?

Jessie

Edited by: jcmartin01 on Jan 17, 2012 5:35 PM

Read only

0 Likes
2,199

Can you use CL_ABAP_DATADESCR (and it's subclasses) to get the info you need at runtime?

Read only

0 Likes
2,199

Yes, I'm using this class, and I get a list of object of type abap_compdescr.

But then I have to use the tip that Ryamond gave me to 'reconstruct' a type for my data.

I was looking for a direct way to get the type of the columns of my internal table.

I think I' ll have to work with my code and Raymond's except if someone finds a better solution....

Jessie

Read only

0 Likes
2,199

Did you try a cl_abap_typedescr->get_ddic_object to get a ddic reference to your field ?

REPORT zgetddic.

TYPE-POOLS: abap.

DATA: field LIKE mara-matnr, " try various way to define field here
      lo_abap_typedescr TYPE REF TO cl_abap_typedescr,
      lt_ddic_objects TYPE dd_x031l_table,
      ls_ddic_object LIKE LINE OF ddic_objects.

CALL METHOD cl_abap_datadescr=>describe_by_data
  EXPORTING
    p_data      = field
  RECEIVING
    p_descr_ref = lo_abap_typedescr.

CALL METHOD lo_abap_typedescr->get_ddic_object
  RECEIVING
    p_object = lt_ddic_objects.

READ TABLE lt_ddic_objects INDEX 1 INTO ls_ddic_object.

WRITE: / sy-subrc, ls_ddic_object-tabname.

Regards,

Raymond

Read only

Former Member
0 Likes
2,199

Try out this:

REPORT ZZAETEST_DYNAMIC_DATA .

DATA XVBAP TYPE TABLE OF VBAP.

DATA COLUMN(30) VALUE 'NETWR'.

FIELD-SYMBOLS <TABLE_REFERENCE> TYPE STANDARD TABLE.

ASSIGN XVBAP TO <TABLE_REFERENCE>.

FIELD-SYMBOLS <TABLE_REFERENCE_FIELDS> TYPE ANY.

DATA GS_FLDNAME TYPE REF TO DATA.

CREATE DATA GS_FLDNAME LIKE LINE OF <TABLE_REFERENCE>.

ASSIGN GS_FLDNAME->* TO <TABLE_REFERENCE_FIELDS>.

FIELD-SYMBOLS <SINGLE_FIELD>.

ASSIGN COMPONENT COLUMN OF

STRUCTURE <TABLE_REFERENCE_FIELDS> TO <SINGLE_FIELD>.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

FIELD-SYMBOLS <FS_DATA> TYPE ANY .

DATA: TY_TYPE TYPE REF TO DATA .

CREATE DATA: TY_TYPE LIKE <SINGLE_FIELD>.

ASSIGN: TY_TYPE->* TO <FS_DATA> .

<FS_DATA> = '123.45'.

BREAK-POINT.

Kind Regards Alfons