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: 

Open SQL: SELECT with dynamic field list

deca
Explorer
0 Kudos

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!

2 REPLIES 2

gabmarian
Active Contributor

Instead of @ use parenthesis like in the old syntax:

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

Escaping a variable/constant in the column list is used when you want to fill a specific field of every line in the result set with the value of the object.

deca
Explorer
0 Kudos

I was quite sure I've already tried it, but probably when I did I made some more mistakes somewhere else...

Of course it works and of course thak for your help!