Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Select INTO fields dynamically

former_member533212
Discoverer

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!

1 ACCEPTED SOLUTION

former_member210008
Active Participant
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
7 REPLIES 7

Former Member

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

0 Kudos

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.

matt
Active Contributor
0 Kudos

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

former_member210008
Active Participant
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

matt
Active Contributor
0 Kudos

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

pokrakam
Active Contributor

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.

Clemenss
Active Contributor

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