Application Development and Automation 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: 
Read only

SELECT command with variables

Former Member
0 Likes
628

Hi,

I have a program where the user enters a number of tables and a number of fields (existing SAP fields) into the selection screen.

In an INCLUDE structure, I have a SELECT command that will simply connect these tables (currently using JOINs) and fetch all these fields and put them into an internal table.

The SELECT currently looks like this:

SELECT

   KNC1~KUNNR

   KNC1~BUKRS

   KNC1~GJAHR

   KNB1~AKONT

   KNA1~KUNNR

   KNA1~NAME1

   KNA1~NAME2

   KNA1~ORT01

   KNA1~ERNAM

   T001~BUTXT

   T001~WAERS

   FROM KNC1

    JOIN KNB1 ON KNB1~KUNNR = KNC1~KUNNR

             AND KNB1~BUKRS = KNC1~BUKRS

    JOIN KNA1 ON KNA1~KUNNR = KNC1~KUNNR

    JOIN T001 ON T001~BUKRS = KNC1~BUKRS

INTO TABLE <F_TABLE>.

In the long run, I have to use the variables I get from the selection screen for both the fields and the tables, but I have to start somewhere plus I think the solution is quite similar: First, I just want to somehow use those field-variables in the SELECT (I already have them in the form [table]~[field]). The idea is that, since I do not know how many fields there might be, I will concatenate them into one field_list (KNC1~KUNNR KNC1~BUKRS ...) and use that in the SELECT.

Can anybody give me a hint on how to do this? Field-symbols don't seem to work - or I haven't yet found out how: I can declare them and assign the correct value to them and, when viewed in the debugger, they look all right, but the SELECT does not want to know about them.

Thanks a lot!

Best regards,

Sapperdapper

2 REPLIES 2
Read only

Former Member
0 Likes
576

one sample program, here columns are variable.

REPORT demo_select_dynamic_columns .

DATA: itab TYPE STANDARD TABLE OF spfli,

      wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

      list LIKE TABLE OF line(72).

line = ' CITYFROM CITYTO '.

APPEND line TO list.

SELECT DISTINCT (list)

  INTO CORRESPONDING FIELDS OF TABLE itab

  FROM spfli.

IF sy-subrc EQ 0.

  LOOP AT itab INTO wa.

    WRITE: / wa-cityfrom, wa-cityto.

  ENDLOOP.

ENDIF.

Read only

0 Likes
576

Hi,

I will close this and mark it as "Assumed answered". I have now fully dynamized the SELECT statement and the joins between all the tables involved and I haven't yet found a scenario where it doesn't work. Well, I'll have to do some testing and if I don't find one, the user is sure to find some scenario that will break my code 😉

Thanks for all the help!

Best regards,

Sapperdapper