Skip to Content
0
Jul 31, 2019 at 09:17 AM

Open SQL: SELECT with dynamic field list

763 Views Last edit Jul 31, 2019 at 09:18 AM 2 rev

Hello,

I have to write a SELECT with a dynamic list of fields. I did it several times using traditional SELECT command:

Traditional SELECT

DATA: lv_fields TYPE string.
SELECT SINGLE (lv_fields)
         FROM mara
         INTO ls_mara
        WHERE matnr = lv_matnr.

I wish to write the same select with the new syntax (in this case I'm working on 7.52 but I still have customer with previous releases), something like this:

TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         laeda TYPE mara-laeda,
         pstat TYPE mara-pstat,
         mbrsh TYPE mara-mbrsh,
       END OF ty_mara.
DATA: lt_mara   TYPE STANDARD TABLE OF ty_mara.
DATA: lv_fields TYPE char1024.

* LV_FIELDS is the result of a routine, I show here as static variable 
* only for sample purpose
lv_fields = 'MATNR, LAEDA, PSTAT, MBRSH'.

SELECT @lv_fields
       FROM mara
       INTO TABLE @lt_mara
      WHERE matnr IN @s_matnr.

This code is ok for syntax check, but if you run it, table LT_MARA is like this:

--------------------------------------------------------
|MATNR                      | LAEDA    | PSTAT | MBRSH |
--------------------------------------------------------
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
|MATNR, LAEDA, PSTAT, MBRSH | 00000000 |       |       |
--------------------------------------------------------

(as many rows as record in MARA that should be read according to WHERE condition)

As you can imagine, this result is not ok for me ;-)

How could I solve this matter?

Thank you and best regards!