10-10-2017 7:09 PM
Hi everyone!
I have some different source tables that I must read through SELECT statement, but the target tables is always the same. Due to a condition, I must read only one of these tables. The problem is that the names of the fields isn't always changes between the source tables. E.g.:
/BIC/ZSRC1 has fields D11 D12 D13
/BIC/ZSRC2 has fields D21 D22 D23
/BIC/ZSRC3 has fields D31 D32 D33
Target table: ITAB has fields F1 F2 F3.
Obviously, 1st 2nd and 3rd fields or source tables must be mapped into F1, F2 and F3 fields of target table.
Here is an example of the code I've developed that doesn't work:
v_col_syntax = 'D11 D12 D13'.
v_tab_syntax = '/BIC/ZSRC1'.
v_into_syntax = '(ITAB-F1, ITAB-F2, ITAB-F3)'.
SELECT (v_col_syntax)
FROM (v_tab_syntax)
INTO (v_into_syntax)"(gt_txtbuff-code, gt_txtbuff-TXTLG)
cl_demo_output=>write_data( ITAB ).
ENDSELECT.<br>
Any suggestions?
Thanks!
10-11-2017 5:41 AM
SELECT (v_col_syntax)
INTO TABLE LT
...
Where LT - table with 3 fields so first column from select goes to first column of local table, etc.
or use AS
SELECT field1 AS a, field2 AS b
10-10-2017 9:54 PM
Has D11, D21, D31 same DDIC type ?
Has D12, D22, D32 same DDIC type ?
Has D13, D23, D33 same DDIC type ?
Various options to map it:
If source and target have different field order
DATA:
v_col_syntax TYPE string VALUE 'CARRID as F1 CONNID as F2 FLDATE as F3',
v_tab_syntax TYPE string VALUE 'SFLIGHT', " BIC/ZSRC1',
v_into_syntax TYPE string VALUE '(ITAB-F1, ITAB-F2, ITAB-F3)',
BEGIN OF itab,
f1 TYPE sflight-carrid,
f2 TYPE sflight-connid,
f3 TYPE sflight-fldate,
END OF itab.
SELECT (v_col_syntax)
FROM (v_tab_syntax)
INTO CORRESPONDING FIELDS OF itab.
cl_demo_output=>write_data( itab ).
EXIT.
ENDSELECT.
or why do you need a dynamic INTO ?
SELECT (v_col_syntax)
FROM (v_tab_syntax)
INTO (ITAB-F1, ITAB-F2, ITAB-F3).
3rd alternative, replace ITAB-F1, ITAB-F2, ITAB-F3 with field-symbols assigned to ITAB-F1, ...
10-10-2017 10:20 PM
INTO CORRESPONDING only works when the field names are the same (not the OP's case).
I don't get either why dynamic INTO is needed. Get rid of ENDSELECT and just select INTO TABLE. As long as the field type / length is compatible, it will be just put into the internal table fields in sequence.
Perhaps OP could share more details.
10-11-2017 7:07 AM
10-11-2017 5:41 AM
SELECT (v_col_syntax)
INTO TABLE LT
...
Where LT - table with 3 fields so first column from select goes to first column of local table, etc.
or use AS
SELECT field1 AS a, field2 AS b
10-11-2017 7:08 AM
Dynamic creation of data types and usage is well covered. Search for RTTS.
10-11-2017 8:14 AM
If you're on 7.4 you can use CORRESPONDING maps:
select ... from (table_name) into @data(query_results) where ... .
case table_name.
when 'T1'.
itab = corresponding #( query_results mapping f1 = d11
f2 = d12 ).
when 'T2'.
itab = corresponding #( query_results mapping f1 = d21
f2 = d22 ).
endcase.
If you're on 7.5 you can use cl_abap_corresponding which can do the mapping part dynamically, see doco and blog 1 and blog 2.
10-11-2017 11:17 AM
Hi,
for the v_col_syntax you may use a STRING_TABLE. Each line represents a field (using joins it is <tabname>~fieldname) eventually followed by ALIAS, i.e. <field-a> AS <field-b>. Do not use the angle brackets, that's documentary syntax. If you use INTO CORRESPONDING FIELDS OF, you can always one structure contatinig all possible fields or you may even create a matching structure dynamically.
Jus my 2 cents
Regards Clemens