Page 1 of 1

Fetching field names from work area

Posted: Wed Feb 12, 2014 7:30 am
by Abraxas
Hi everybody!

What I´m trying to do is fetching the fields within a work area dynamically, that is, without knowing the field names. The following code is taken from a subroutine and may illustrate what I mean:

Code: Select all

DATA: list  TYPE         vrm_values,
      value LIKE LINE OF list.

SELECT (select_field)
  INTO CORRESPONDING FIELDS OF wa
  FROM (select_table)
  ORDER BY (field_order).

  CASE select_field.
    WHEN 'abc'.
      value-key   = wa-key_abc.
      value-text  = wa-text_abc.
    WHEN 'def'.
      value-key   = wa-key_def.
      value-text  = wa-text_def.
    ...
  ENDCASE.

  APPEND value TO list.
ENDSELECT.
Now, I want to replace the CASE block by someting like the following (like known, e.g. from PHP):

Code: Select all

while (list(key, text) = each(wa))
{
  value-key  = key.
  value-text = text.
}
This way, I could work with SQL data completely dynamic and save a lot of work.

Does enybody have an idea how to recode this? I´ve seen something with field reference (<fr>) but still don´t understand the concept...

Re: Fetching field names from work area

Posted: Wed Feb 12, 2014 8:26 am
by Gothmog
A field symbol (defined by FIELD-SYMBOLS) will point to another data's value, it has no memory area of its own.
You use ASSIGN to have it point to the data you want.
Have a look at the ABAP help for these statement.

In your case, you'll have to use a dynamic name for the assignation :

Code: Select all

ASSIGN (field_name) TO <field_symbol>.
where field_name is a character data with a value of the 'wa-key_abc', for example.

Use CONCATENATE to create the field name, assign the field symbol to this field, and you can then write:

Code: Select all

value-key = <field_symbol>.
Do it for both the key and the text, and you're done.

Re: Fetching field names from work area

Posted: Wed Feb 12, 2014 10:32 am
by Abraxas
Thanks for your answer!

I tried the following code and got an exeption (Field symbol has not yet been assigned):

Code: Select all

TYPES:
  BEGIN OF wa_type,
    key  TYPE string,
    text TYPE string,
  END OF wa_type.

DATA:
  wa TYPE wa_type.

SELECT key_abc AS key text_abc AS text
  INTO CORRESPONDING FIELDS OF wa
  FROM (select_table)
  ORDER BY (field_order).

ASSIGN (wa-key)  TO <key_symbol>.
ASSIGN (wa-text) TO <text_symbol>.
value-key  = <key_symbol>.
value-text = <text_symbol>.
Any idea what could be wrong?

The error occurs at line "value-key = <key_symbol>."

Re: Fetching field names from work area

Posted: Wed Feb 12, 2014 10:45 am
by Gothmog
Always check the return code after an assignation if you want to avoid dumps.

Code: Select all

ASSIGN (wa-key)  TO <key_symbol>.
means : assign the field which name in contained in wa-key to the field symbol

Here, you want :

Code: Select all

ASSIGN wa-key TO <key_symbol>.
which means: assign the field wa-key to the field symbol

Re: Fetching field names from work area

Posted: Wed Feb 12, 2014 10:49 am
by Abraxas
Just found out the same - anyway thanks a lot for your help!

Re: Fetching field names from work area

Posted: Sun Feb 16, 2014 4:57 pm
by androb
Hi,

You can use a DO cycle for each record:

Code: Select all

FIELD-SYMBOLS: <lv_field> TYPE ANY.

DO.

* ls_record is the currently processes record of the select statement
  ASSIGN COMPONENT sy-index OF STRUCTURE ls_record TO <lv_field>.
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.

* here you can do anything with <lv_field>

ENDDO.
Regards,
Andy