I have following internal table:
VBELN POSNR ELEMENT VALUE
4711 10 E1 12
4711 10 E2 23
Is there any possibility in ABAP (Framework, Class, etc), so I can fast create a new internal table at runtime which would look like this:
VBELN POSNR E1 E2
4711 10 12 23
Would appreciate any kind of help.
Thanks and BR.
Yes. What you want are the ABAP Runtime Type Services (RTTS), more precisely the ABAP Runtime Type Creation (RTTC).
DATA(vbeln_descriptor) = CAST cl_abap_datadescr( cl_abap_typedescr=>describe_by_name( 'VBELN' ) ).
DATA(posnr_descriptor) = CAST cl_abap_datadescr( cl_abap_typedescr=>describe_by_name( 'POSNR' ) ).
DATA(components) = VALUE abap_component_tab( ( name = 'VBELN'
type = vbeln_descriptor )
( name = 'POSNR'
type = posnr_descriptor ) ).
DATA(value_descriptor) = cl_abap_typedescr=>describe_by_name( 'VALUE' ).
LOOP AT vbeln_rows INTO DATA(vbeln_row).
INSERT VALUE #(
name = vbeln_row-element
type = value_descriptor )
INTO TABLE components.
ENDLOOP.
DATA(row_descriptor) = cl_abap_structdescr=>get( components ).
DATA(table_descriptor) = cl_abap_tabledescr=>create( row_descriptor ).
DATA itab TYPE REF TO data.
CREATE DATA itab TYPE HANDLE table_descriptor.
Related
We all know these excellent ABAP statements which allows finding unique values in one-liner:
it_unique = VALUE #( FOR GROUPS value OF <line> IN it_itab
GROUP BY <line>-field WITHOUT MEMBERS ( value ) ).
But what about extracting duplicates? Can one utilize GROUP BY syntax for that task or, maybe, table comprehensions are more useful here?
The only (though not very elegant) way I found is:
LOOP AT lt_marc ASSIGNING FIELD-SYMBOL(<fs_marc>) GROUP BY ( matnr = <fs_marc>-matnr
werks = <fs_marc>-werks )
ASSIGNING FIELD-SYMBOL(<group>).
members = VALUE #( FOR m IN GROUP <group> ( m ) ).
IF lines( members ) > 1.
"throw error
ENDIF.
ENDLOOP.
Is there more beautiful way of finding duplicates by arbitrary key?
So, I just put it as answer, as we with Florian weren't able to think out something better. If somebody is able to improve it, just do it.
TYPES tt_materials TYPE STANDARD TABLE OF marc WITH DEFAULT KEY.
DATA duplicates TYPE tt_materials.
LOOP AT materials INTO DATA(material)
GROUP BY ( id = material-matnr
status = material-pstat
size = GROUP SIZE )
ASCENDING REFERENCE INTO DATA(group_ref).
CHECK group_ref->*-size > 1.
duplicates = VALUE tt_materials( BASE duplicates FOR <status> IN GROUP group_ref ( <status> ) ).
ENDLOOP.
Given
TYPES: BEGIN OF key_row_type,
matnr TYPE matnr,
werks TYPE werks_d,
END OF key_row_type.
TYPES key_table_type TYPE
STANDARD TABLE OF key_row_type
WITH DEFAULT KEY.
TYPES: BEGIN OF group_row_type,
matnr TYPE matnr,
werks TYPE werks_d,
size TYPE i,
END OF group_row_type.
TYPES group_table_type TYPE
STANDARD TABLE OF group_row_type
WITH DEFAULT KEY.
TYPES tt_materials TYPE STANDARD TABLE OF marc WITH DEFAULT KEY.
DATA(materials) = VALUE tt_materials(
( matnr = '23' werks = 'US' maabc = 'B' )
( matnr = '42' werks = 'DE' maabc = 'A' )
( matnr = '42' werks = 'DE' maabc = 'B' ) ).
When
DATA(duplicates) =
VALUE key_table_type(
FOR key IN VALUE group_table_type(
FOR GROUPS group OF material IN materials
GROUP BY ( matnr = material-matnr
werks = material-werks
size = GROUP SIZE )
WITHOUT MEMBERS ( group ) )
WHERE ( size > 1 )
( matnr = key-matnr
werks = key-werks ) ).
Then
cl_abap_unit_assert=>assert_equals(
act = duplicates
exp = VALUE tt_materials( ( matnr = '42' werks = 'DE') ) ).
Readability of this solution is so bad that you should only ever use it in a method with a revealing name like collect_duplicate_keys.
Also note that the statement's length increases with a growing number of key fields, as the GROUP SIZE addition requires listing the key fields one by one as a list of simple types.
What about the classics? I'm not sure if they are deprecated or so, but my first think is about to create a table clone, DELETE ADJACENT-DUPLICATES on it and then just compare both lines( )...
I'll be eager to read new options.
I'm working on a routine which moves the lines of a string table (in this case fui_elements) into a structure of unknown type (fcwa_struct).
DATA(li_temp) = ... "fill assignment table here
LOOP AT fui_elements ASSIGNING FIELD-SYMBOL(<lfs_element>).
ASSIGN COMPONENT li_temp[ sy-tabix ] OF STRUCTURE fcwa_struct
TO FIELD-SYMBOL(<lfs_field>).
IF sy-subrc <> 0.
"somethings wrong with the fui_elements data
ENDIF.
<lfs_field> = <lfs_element>.
ENDLOOP.
If the table i_field_customizing (STANDARD TABLE OF string) is not initial, I want to use its values.
Otherwise I want to generate an integer table (so that the loop runs equally often regardless of the table's values). Here lw_max is the amount of fields the imported structure has:
DATA(li_temp) = COND #( WHEN i_field_customizing[] IS INITIAL
THEN VALUE t_integer_tt( FOR X = 1
THEN X + 1
WHILE X <= lw_max
( X ) )
ELSE i_field_customizing ).
But when I run the report with i_field_customizing like that:
DATA(i_field_customizing) = VALUE t_string_tt( ( `KUNNR` ) ( `NAME1` ) ).
I get this exception on the line where I try to construct li_temp:
CX_SY_CONVERSION_NO_NUMBER (KUNNR cannot be interpreted as a number)
My current guess is that COND gets its type statically. Does anybody know how I can get around this?
What you are trying to achieve will not be possible because the type of an inline definition of a variable using COND is decided at compilation time and not at runtime.
Please see my question here. The type that will be taken is always the type of the variable that stands directly after THEN. You can decide what type will be chosen at compilation time by fiddling with negating the condition and switching places of variables after THEN at ELSE but it will be always either or and from what I understand you want to be able to do it dynamically so your ASSIGN COMPONENT statement works as expected with integers.
Even by specifically casting the variable after ELSE one gets the same short dump as you do.
DATA(li_temp) = COND #( WHEN i_field_customizing IS INITIAL
THEN VALUE t_integer_tt( ( 1 ) ( 2 ) )
ELSE CAST t_string_tt( REF #( i_field_customizing ) )->* ).
Alternatively you could cast to REF TO DATA but then you have to dereference it to a field symbol of type STANDARD TABLE.
REPORT zzy.
CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS:
main.
ENDCLASS.
CLASS lcl_main IMPLEMENTATION.
METHOD main.
TYPES:
t_integer_tt TYPE STANDARD TABLE OF i WITH EMPTY KEY,
t_string_tt TYPE STANDARD TABLE OF string WITH EMPTY KEY.
FIELD-SYMBOLS:
<fs_table> TYPE STANDARD TABLE.
DATA: BEGIN OF l_str,
kunnr TYPE kunnr,
name1 TYPE name1,
END OF l_str.
* DATA(i_field_customizing) = VALUE t_string_tt( ( `KUNNR` ) ( `NAME1` ) ).
DATA(i_field_customizing) = VALUE t_string_tt( ).
DATA(li_temp) = COND #( WHEN i_field_customizing IS INITIAL
THEN CAST data( NEW t_integer_tt( ( 1 ) ( 2 ) ) )
ELSE CAST data( REF #( i_field_customizing ) ) ).
ASSIGN li_temp->* TO <fs_table>.
LOOP AT <fs_table> ASSIGNING FIELD-SYMBOL(<fs_temp>).
ASSIGN COMPONENT <fs_temp> OF STRUCTURE l_str TO FIELD-SYMBOL(<fs_field>).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_main=>main( ).
I can do the following query using Open SQL:
select count(*) into lv_count
from prcd_elements client specified
where waerk = 'USD'
How do I do the same thing with ABAP Database Connectivity (ADBC) native SQL?
I've tried the following but it gives me an exception... I'm not sure what data type I need for lt_table to hold a count, and class CL_SQL_RESULT_SET (returned by execute_query) seems to require a local table.
data lt_table type standard table of prcd_elements.
data(lo_sql) = new cl_sql_statement( ).
data(lo_result) = lo_sql->execute_query(
|select count(*) from prcd_elements where waerk = 'USD'|
).
lo_result->set_param_table( REF #( lt_table ) ).
lo_result->next_package( ).
lo_result->close( ).
Thanks!
With some help from a few mysterious benefactors, I figured this one out. I needed to create an internal table with a single column of type I (Integer), and then extract the count from this internal table.
types:
begin of s_count,
count type i,
end of s_count.
data lt_table type standard table of s_count.
data(lo_sql) = new cl_sql_statement( ).
data(lo_result) = lo_sql->execute_query(
|select count(*) from prcd_elements where waerk = 'USD'|
).
lo_result->set_param_table( REF #( lt_table ) ).
lo_result->next_package( ).
lo_result->close( ).
data(lv_count) = lt_table[ 1 ]-count.
You can also use a variable instead of a internal table;
data lv_count type i.
data(lo_sql) = new cl_sql_statement( ).
lo_sql->execute_query(select count(*) into :lv_count from prcd_elements where waerk = 'USD').
I am pasting this program for example but i will never know the type of the table (here vbap and vbak).
My goals is to display my field symbol without knowing the types.
Is it possible ?
Here is my code :
REPORT ZTEST_FME_FOL.
type-pools slis .
FIELD-SYMBOLS : <mytable> TYPE ANY TABLE.
DATA : lv_alv_table TYPE REF TO cl_salv_table,
lv_funct TYPE REF TO cl_salv_functions,
lv_columns TYPE REF TO cl_salv_columns_table,
lv_column TYPE REF TO CL_SALV_COLUMN_table.
SELECT * from vbap INNER JOIN VBAK ON vbap~vbeln = vbak~vbeln UP TO 10 ROWS INTO TABLE <mytable>.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lv_alv_table
CHANGING
t_table = <mytable> ).
CATCH cx_salv_msg .
ENDTRY.
lv_funct = lv_alv_table->get_functions( ).
lv_funct->set_all( Abap_True ).
lv_columns = lv_alv_table->get_columns( ).
lv_alv_table->display( ).
Thanks in advance !
Depending on what you 're trying to do there's going to be more validation required than what I've done, but in essence this is what you need.
Using (dynamic) joins may be particularly tricky.
report zevw_test_dynamic_alv.
parameters: p_table type string obligatory.
field-symbols: <gt_table> type standard table.
data: gt_data type ref to data.
start-of-selection.
create data gt_data type table of (p_table).
assign gt_data->* to <gt_table>.
select * from (p_table) up to 10 rows
into table <gt_table>.
perform display_results using <gt_table>. "Your ALV stuff will be in here
You may even have to build the fieldcat manually and then use
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt_fieldcat[]
importing
ep_table = gt_data.
to get the data reference
I would like to find a way to export the SAP MD04 - overview tree in Excel.
I don't understand what will be the best approach.
Thanks in advance for any suggestion.
Regs
S.
I've done this workaround:
You have to use 2 Functional Module CS_BOM_EXPL_MAT_V2 + MD_STOCK_REQUIREMENTS_LIST_API
In the INFOSET Data Reading Program I've used the CS_BOM_EXPL_MAT_V2 for the BOM Explosion.
REPORT RSAQDVP_TEMPLATE .
data:
STPOX type STPOX ,
it_data type standard table of STPOX .
field-symbols: <struc> type STPOX .
* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_HEAD>
* (select your data here into internal table IT_DATA)
call function 'CS_BOM_EXPL_MAT_V2'
EXPORTING
auskz = 'X'
capid = p_capid
datuv = p_datuv
emeng = u_qty
mehrs = 'X'
mtnrv = p_mtnrv
werks = p_werks
TABLES
stb = it_data.
loop at it_data assigning <struc>.
move-corresponding <struc> to STPOX .
* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_BODY>
endloop.
Than I've added Additional Field calling the MD_STOCK_REQUIREMENTS_LIST_API.
CLEAR MNG01_FM.
call function 'MD_STOCK_REQUIREMENTS_LIST_API'
EXPORTING
matnr = STPOX-IDNRK
werks = p_werks
TABLES
mdezx = it_da2.
READ TABLE it_da2 ASSIGNING <struct2> WITH KEY DELKZ = 'SB'.
if sy-subrc = 0.
MNG01_FM = <struct2>-MNG01.
endif.