u.n
u.n

Reputation: 52

Dynamic ASSIGN of table row expression

In my ABAP report I have some structure:

data: 
  begin of struct1,
    field1 type char10,
  end of struct1.

I can access to it's field field1 directly:

data(val) = struct1-field1

or dynamically with assign:

assign ('struct1-field1') to field-symbol(<val>).

Also I have some internal table:

data: table1 like standard table of struct1 with default key.
append initial line to table1.

I can access to column field1 of first row directly:

data(val) = table1[ 1 ]-field1.

But I can not get access to field1 with dynamic assign:

assign ('table1[ 1 ]-field1') to field-symbol(<val>).

After assignment sy-subrc equals "4".

Why?

Upvotes: 1

Views: 2466

Answers (2)

Suncatcher
Suncatcher

Reputation: 10621

Sandra is absolutely right, if table expressions are not specified in help, then they are not allowed.

You can use ASSIGN COMPONENT statement for your dynamicity:

FIELD-SYMBOLS: <tab> TYPE INDEX TABLE.

ASSIGN ('table1') TO <tab>.
ASSIGN COMPONENT 'field1' OF STRUCTURE <tab>[ 1 ] TO FIELD-SYMBOL(<val>).

However, such dynamics is only possible with index tables (standard + sorted) due to the nature of this version of row specification. If you try to pass hashed table into the field symbol, it will dump.

Upvotes: 2

Sandra Rossi
Sandra Rossi

Reputation: 13646

The syntax of ASSIGN (syntax1) ... is not the same as the syntax of the Right-Hand Side (RHS) of assignments ... = syntax2.

The syntax for ASSIGN is explained in the documentation of ASSIGN (variable_containing_name) ... or ASSIGN ('name') ... (chapter 1. (name) of page ASSIGN - dynamic_dobj).

Here is an abstract of what is accepted:

  • "name can contain a chain of names consisting of component selectors [(-)]"
  • "the first name [can be] followed by an object component selector (->)"
  • "the first name [can be] followed by a class component selector (=>)"

No mention of table expressions, so they are forbidden. Same for meshes...

Concerning the RHS of assignments, as described in the documentation, it can be :

  • Data Objects
  • Return values or results of functional methods, return values or results of built-in functions and constructor expressions, or return values or results of table expressions
  • Results of calculation expressions

Upvotes: 2

Related Questions