‎2011 Oct 10 2:40 PM
Hello Folks,
how do we SELECT the fields dynamically ( with only the fields present in the dynamic internal table )?
For eg : If my dynamic internal table has only 4 fields. I need to fetch only these 4 fields.
‎2011 Oct 11 4:14 AM
Charan,
Above replies are correct, but I believe unless your query/table is too heavy, you can live with 'into corresponding fields of' as it provides a very minimum overhead. So, you can simply use * instead of finding the exact column names in your dynamic internal table. Depends more on your scenario that what is table size, no. of fields in DB table.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_fieldcat
IMPORTING
ep_table = i_dyntab "dynamic itab
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
ASSIGN i_dyntab->* TO <f_dyntable>. "<f_dyntable> behaves a normal itab now
sellect * from ztable into CORRESPONDING FIELDS OF TABLE <f_dyntable>
where .......................
‎2011 Oct 10 2:44 PM
Hi,
use the technique of field symbol, referencing, Assign component etc of dynamic programming.
simple exapmle coding is as below,
DATA:
Create variable that can contain referecene to any data
dataref TYPE REF TO data.
FIELD-SYMBOLS:
TYPE ANY,
TYPE ANY.
PARAMETERS:
p_tab TYPE tabname.
START-OF-SELECTION.
Create a workarea for the tabel selected on the selection screen
CREATE DATA dataref TYPE (p_tab).
The variable dataref cannot be accessed directly, so a field symbol is
used
ASSIGN dataref->* TO .
SELECT *
FROM (p_tab) UP TO 10 ROWS
INTO .
NEW-LINE.
DO.
Write all the fields in the record
ASSIGN COMPONENT sy-index
OF STRUCTURE
TO .
IF sy-subrc 0.
EXIT.
ENDIF.
WRITE .
ENDDO.
ENDSELECT.
Thanks,
Chandra
‎2011 Oct 10 2:46 PM
Try to create select query in a string variable using concatenation based on fields in your internal table.
For example:- if your itab contains say field1, field2, field3, make a string say
loop at itab.
concatenate itab-field in string variable say str_var
endloop.
then
select ( str_var) from table into corresponding field of internal table.
Hope this helps.
Thanks. Rashi.
‎2011 Oct 10 2:48 PM
There is no statement that does this.
we got one that is quite close to what you need. The addition "INTO CORRESPONDING FIELDS OF" of the select statement.
But as i´m not sure if this transfers everything to application server and then throws away fields that are not needed i would guess this one produces overhead.
Only chance i see is to first find out what fields you got in your itab, making use of the statement "loop at component of structure".
Build a list of fields and then select just those.
on the other hand, i´m not even sure you can make the part , where you provide the fields to select, generic.
‎2011 Oct 10 2:55 PM
Thank you for the replies. can anyone explain with a better example?
‎2011 Oct 10 3:17 PM
From your very thin initial description I would understand that the standard SELECT (column_syntax) might be what you need to use, in conjunction with INTO CORRESPONDING as described by Florian.
Please read ABAP online help:
http://help.sap.com/abapdocu_702/en/abapselect_clause_cols.htm#!ABAP_ALTERNATIVE_3@3@
The data object column_syntax can be a character-type data object or an internal table with a character-type data type
Thomas
‎2011 Oct 10 4:49 PM
TYPES : BEGIN OF ty_m1 ,
matnr TYPE mara-matnr ,
END OF ty_m1 ,
BEGIN OF ty_m2 ,
matnr TYPE mara-matnr ,
ernam TYPE mara-ernam ,
END OF ty_m2 .
DATA : i_m1 TYPE TABLE OF ty_m1 ,
i_m2 TYPE TABLE OF ty_m2 .
DATA : v_fields TYPE string .
START-OF-SELECTION .
* select with one field
PERFORM get_fields USING i_m1 CHANGING v_fields .
WRITE : / v_fields .
SELECT (v_fields)
INTO TABLE i_m1
FROM mara.
* WHERE matnr IN s_matnr .
* select with two fields
PERFORM get_fields USING i_m2 CHANGING v_fields .
WRITE : / v_fields .
SELECT (v_fields)
INTO TABLE i_m1
FROM mara.
* WHERE matnr IN s_matnr .
*&---------------------------------------------------------------------*
*& Form get_fields
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->INTERNAL_TABLE text
*----------------------------------------------------------------------*
FORM get_fields USING internal_table TYPE ANY TABLE
CHANGING fields TYPE string .
DATA:
wa_ref TYPE REF TO data,
desc_table TYPE REF TO cl_abap_tabledescr,
desc_struc TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS:
<p_data> TYPE ANY,
<p_component> TYPE abap_compdescr.
CLEAR fields .
CREATE DATA wa_ref LIKE LINE OF internal_table.
ASSIGN wa_ref->* TO <p_data>.
desc_table ?= cl_abap_tabledescr=>describe_by_data( internal_table ).
desc_struc ?= desc_table->get_table_line_type( ).
LOOP AT desc_struc->components ASSIGNING <p_component>.
CONCATENATE fields <p_component>-name INTO fields SEPARATED BY space .
ENDLOOP.
ENDFORM. "get_fieldspart of above code is taken from
‎2011 Oct 11 4:14 AM
Charan,
Above replies are correct, but I believe unless your query/table is too heavy, you can live with 'into corresponding fields of' as it provides a very minimum overhead. So, you can simply use * instead of finding the exact column names in your dynamic internal table. Depends more on your scenario that what is table size, no. of fields in DB table.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = i_fieldcat
IMPORTING
ep_table = i_dyntab "dynamic itab
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
ASSIGN i_dyntab->* TO <f_dyntable>. "<f_dyntable> behaves a normal itab now
sellect * from ztable into CORRESPONDING FIELDS OF TABLE <f_dyntable>
where .......................
‎2011 Oct 11 7:19 AM