Skip to Content

Select INTO fields dynamically

Oct 10, 2017 at 06:09 PM


avatar image

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 ).

Any suggestions?


10 |10000 characters needed characters left characters exceeded

Dynamic creation of data types and usage is well covered. Search for RTTS.

* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Evgeniy Astafev Oct 11, 2017 at 04:41 AM
SELECT (v_col_syntax)

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 |10000 characters needed characters left characters exceeded
Mike Pokraka Oct 11, 2017 at 07: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 ).

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 |10000 characters needed characters left characters exceeded
Clemens Li Oct 11, 2017 at 10:17 AM


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

10 |10000 characters needed characters left characters exceeded
Tibor Gerke Oct 10, 2017 at 08: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

    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)
    cl_demo_output=>write_data( itab ).

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, ...

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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.


Worse, the OP appears to be using a table with header lines.