Fetching field names from work area

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: Snowy, thx4allthefish, YuriT, Gothmog

Post Reply
Abraxas
Posts: 4
Joined: Wed Feb 12, 2014 7:13 am

Fetching field names from work area

Post by Abraxas » Wed Feb 12, 2014 7:30 am

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...
Last edited by Abraxas on Wed Feb 12, 2014 8:44 am, edited 1 time in total.

Gothmog
Posts: 1955
Joined: Wed Sep 12, 2007 4:46 am
Location: Probably not home

Re: Fetching field names from work area

Post by Gothmog » Wed Feb 12, 2014 8:26 am

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.
68 74 74 70 3a 2f 2f 74 69 6e 79 75 72 6c 2e 63 6f 6d 2f 62 64 6f 37 6d 77 67

Abraxas
Posts: 4
Joined: Wed Feb 12, 2014 7:13 am

Re: Fetching field names from work area

Post by Abraxas » Wed Feb 12, 2014 10:32 am

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

Gothmog
Posts: 1955
Joined: Wed Sep 12, 2007 4:46 am
Location: Probably not home

Re: Fetching field names from work area

Post by Gothmog » Wed Feb 12, 2014 10:45 am

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
68 74 74 70 3a 2f 2f 74 69 6e 79 75 72 6c 2e 63 6f 6d 2f 62 64 6f 37 6d 77 67

Abraxas
Posts: 4
Joined: Wed Feb 12, 2014 7:13 am

Re: Fetching field names from work area

Post by Abraxas » Wed Feb 12, 2014 10:49 am

Just found out the same - anyway thanks a lot for your help!

androb
Posts: 2
Joined: Tue Feb 11, 2014 4:20 am

Re: Fetching field names from work area

Post by androb » Sun Feb 16, 2014 4:57 pm

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
Last edited by Gothmog on Mon Feb 17, 2014 4:33 am, edited 1 time in total.
Reason: code tags

Post Reply