01-21-2014 2:59 PM
Hello ABAP experts,
I would like to create a structure. This structure should have the field names that I select from a table value.
For example,
Let's say my select statement returns the value 'VALUE1', my structure definition should be
begin of stru1 ,
'VALUE1'(20) type c,
belnr type belnr,
matnr type matnr,
end of stru1.
Is this possible? If so, can you please provide me a sample code?
Thank you,
Ram Kannan
Moderator message - FAQ. Unmarked as question & removed points allocated.
Message was edited by: Suhas Saha
01-21-2014 6:42 PM
Hi ,
Can you please elaborate little more?
select statement suppose returns multiple valuees in internal table like 'Val1','Val2' and 'val3'.
Then u need structure like,
begin of stru1 ,
'VAL1'(20) type c,
val2(20) type c,
val3(20) type c,
end of stru1.
Am i getting right?
BR
Sumeet
01-21-2014 6:53 PM
Hello,
For you requirement, you will need to create a dynamic internal table based in your fields received in your SELECT statement and use a field symbol structure to assign for future use.
You can use this content as reference: Dynamic Internal table - ABAP Development - SCN Wiki.
Focus in the FORM routine CREATE_DYNAMIC_ITAB, last 2 lines.
01-21-2014 6:57 PM
TYPES: BEGIN OF ty_struc,
value1(20) TYPE c,
belnr TYPE belnr,
matnr TYPE matnr,
END OF stru1.
Here you have the structure definition - or table definition.
DATA ls_struc TYPE ty_struc.
Gives you the structure at run time. If you mean creation via RTT, you should do:
DATA lr_data TYPE REF TO data.
CREATE DATA lr_data TYPE ty_struc.
And for the usage of the structure you should derefence it:
FIELD-SYMBOLS <fs_struc> TYPE any.
ASSIGN lr_data->* TO <fs_struc>.
Best,
Sander
01-21-2014 7:20 PM
DATA: lt_fieldcat TYPE lvc_t_fcat.
1) Fill your fields into LT_FIELDCAT.
2) Then create a dynamic internal table using that field catalogue.
DATA: lt_dyn_table TYPE REF TO data,
ls_structure TYPE REF TO data.
FIELD-SYMBOLS : <fs_dyn_table> TYPE ANY TABLE,
<fs_structure> TYPE any.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcat
IMPORTING
ep_table = lt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
ASSIGN lt_table->* TO <fs_table>.
3) Then create the structure dynamically.
CREATE DATA ls_structure LIKE LINE OF <fs_table>.
ASSIGN ls_structure->* TO <fs_structure>.
01-21-2014 7:40 PM
01-21-2014 10:19 PM
Hi Renan,
can you explain to me what an ALV fieldcategory has to do with dynamically creating a table?
Best,
Sander
01-22-2014 12:41 AM
Hey Sander,
Basically the requirement of our friend is: create a structure with dynamic columns (will come from a database table).
So in this case, we can't define previous a type to cast a structure or a internal table using the command CREATE DATA. For solve this, we have the option to use the static method CREATE_DYNAMIC_TABLE from class CL_ALV_TABLE_CREATE based on a fieldcatalog.
The fields defined in the fieldcatalog table will be our "type" to create the dynamic internal table, like is to create ALV columns in a ALV Report.
01-22-2014 12:54 AM
If you want more technical details, check the include "LSKBHF06" where the actual dynamic table creation takes place.
01-23-2014 12:37 PM
Hey Renan,
ok, of course that's also an applicable solution. I guess I missed the dynamic field part in the question.
Best,
Sander
01-23-2014 12:56 PM
Hello All,
can you explain to me what an ALV fieldcategory has to do with dynamically creating a table?
This method of creating dynamic runtime structures is no longer necessary if you have the RTTS classes available. ( Too many threads on this topic)
cl_alv_table_create=>create_dynamic_table( ) method calls GENERATE SUBROUTINE POOL internally which has it's own disadvantages. (Also discussed in the forums & highlighted in the SAP documentation)
I don't understand why people do not search before posting their question.
BR,
Suhas
01-23-2014 1:31 PM
My point exactly. So Ram, you could also consider the solution provided below.
REPORT z_example_rtt_comp_create.
DATA gt_component TYPE cl_abap_structdescr=>component_table.
*----------------------------------------------------------------------*
* CLASS lcl_comp DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_comp DEFINITION.
PUBLIC SECTION.
CLASS-METHODS add_field
IMPORTING
iv_field TYPE fieldname
iv_rollname TYPE any.
CLASS-METHODS get_component
RETURNING
value(rt_component) TYPE cl_abap_structdescr=>component_table.
PRIVATE SECTION.
CLASS-DATA mt_component TYPE cl_abap_structdescr=>component_table.
ENDCLASS. "lcl_comp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_comp IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_comp IMPLEMENTATION.
METHOD add_field.
DATA ls_component TYPE cl_abap_structdescr=>component.
ls_component-name = iv_field.
ls_component-type ?= cl_abap_datadescr=>describe_by_name( iv_rollname ).
INSERT ls_component INTO TABLE mt_component.
ENDMETHOD. "add_field
METHOD get_component.
rt_component[] = mt_component[].
ENDMETHOD. "get_component
ENDCLASS. "lcl_comp IMPLEMENTATION
START-OF-SELECTION.
lcl_comp=>add_field(
iv_field = 'VALUE1'
iv_rollname = 'CHAR20' ). "#EC NOTEXT
lcl_comp=>add_field(
iv_field = 'BELNR'
iv_rollname = 'BELNR' ). "#EC NOTEXT
lcl_comp=>add_field(
iv_field = 'MATNR'
iv_rollname = 'MATNR' ). "#EC NOTEXT
* INSERT ANY ADDITIONAL VALUES YOU LIKE
gt_component = lcl_comp=>get_component( ).
DATA lo_struct_type TYPE REF TO cl_abap_structdescr.
DATA lo_table_type TYPE REF TO cl_abap_tabledescr.
DATA lx_root TYPE REF TO cx_root.
TRY.
* RTT structure type handler
lo_struct_type ?= cl_abap_structdescr=>create(
p_components = gt_component ).
* RTT Table type handler - based on structured type)
lo_table_type = cl_abap_tabledescr=>create(
p_line_type = lo_struct_type ).
CATCH cx_sy_struct_attributes INTO lx_root.
sy-msgv1 = lx_root->if_message~get_text( ).
MESSAGE e398(00) WITH sy-msgv1.
EXIT.
ENDTRY.
DATA lr_table TYPE REF TO data.
DATA lr_struct TYPE REF TO data.
FIELD-SYMBOLS <ft> TYPE ANY TABLE.
FIELD-SYMBOLS <fs> TYPE ANY.
* Creation and dereferencing of the table
CREATE DATA lr_table TYPE HANDLE lo_table_type.
ASSIGN lr_table->* TO <ft>.
* Creation and dereferencing of the structure
CREATE DATA lr_struct TYPE HANDLE lo_struct_type.
ASSIGN lr_struct->* TO <fs>.
01-22-2014 2:37 AM