‎2012 Jan 17 2:11 PM
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> .
‎2012 Jan 17 2:35 PM
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
‎2012 Jan 17 2:35 PM
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
‎2012 Jan 17 2:39 PM
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>.
‎2012 Jan 17 2:57 PM
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
‎2012 Jan 17 3:21 PM
‎2012 Jan 17 3:34 PM
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
‎2012 Jan 17 4:41 PM
Can you use CL_ABAP_DATADESCR (and it's subclasses) to get the info you need at runtime?
‎2012 Jan 17 6:12 PM
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
‎2012 Jan 18 7:16 AM
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
‎2012 Jan 18 9:21 AM
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