Application Development and Automation 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: 
Read only

How to declare internal table field reference dynamically

Former Member
0 Likes
3,533

Hi,

I have a requirement to declare the internal table field reference dynamically. I would know the field reference only during runtime and at that time, I accordingly need to build the internal table and that will be used later for different purpose. The structure of the internal table should appear as mentioned below for better understaning.

DATA: BEGIN OF it_tab OCCURS 0,

key LIKE <dynamic>,

text LIKE t179t-vtext,

END OF it_tab.

<Dynamic> - dynamic field reference is known at the run time.

Please help me as how to declare this internal table having dynamic field reference.

Thanks,

Lakshmi

7 REPLIES 7
Read only

Former Member
0 Likes
2,223

Hi Lakshmi,

This is one of the way to create Dynamic Internal Table..

Check this may help u..

PARAMETERS : p_table(10) TYPE C.

DATA: w_tabname TYPE w_tabname,

w_dref TYPE REF TO data,

FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.

w_tabname = p_table.

CREATE DATA w_dref TYPE TABLE OF (w_tabname).

ASSIGN w_dref->* TO <t_itab>.

SELECT *

FROM (w_tabname) UP TO 20 ROWS

INTO TABLE <t_itab>.

--

Krishna Chaitanya G

Read only

Former Member
0 Likes
2,223

Hi Krishna,

Thank you for the response. But my requirement is different from the code you have provided. I do not have my internal table structure referring to a particular table.

The main point here is that the internal table field reference is dynamic. The example I have provided shows my requirement.

Thanks,

Lakshmi

Read only

0 Likes
2,223

Hi

Try to see the references of the class cl_alv_table_create, static method create_dynamic_table.

Hope it helps

Kleber Santos

Read only

MarcinPciak
Active Contributor
0 Likes
2,223

What I understand is that you want the field of a table be dynamic, not a table itself. Is that right?

For this you can simply declare that field as generic field (reference to any type). During runtime you can hold any reference there.


DATA: BEGIN OF it_tab OCCURS 0,
key TYPE REF TO DATA,    "generic field
...
END OF it_tab.

"during runtime
GET REFERENCE OF ... TO itab-key.  "just get the reference of any field here

You can also use that field when passing different references (known at runtime).

Please note!

You cannot declare the field like:


key LIKE some_field.  "some_field known at runtime

Syntax error will be raised in the above statement, as during compilation some_field is not a type itself.

So you must handle that with data reference or if you really want that field receive dynamic type at runtime, you will have to create a subroutine pool wherein you place all the code of the program and then submit that program.

For more info refer http://www.sapdev.co.uk/tips/dynamic/dyn_prog.htm .

Regards

Marcin

Read only

Former Member
0 Likes
2,223

Hi Marcin,

Thank you for the help. I have a problem if i declare internal table the way it was suggested when i populate the values to this internal table.

I have to populate the values from other internal table to this internal table and while populating the values, i get error message that the value is not convertible to KEY field. Please suggest. Example is provided below.

DATA: BEGIN OF it_tab OCCURS 0,

key TYPE REF TO data,

text TYPE t179t-vtext,

END OF it_tab.

LOOP AT it_tab1 INTO wa_it_tab1.

it_tab-key = wa_it_tab1-<field1>. "This is where i have the problem as I cannot directly assign the value of

" wa_it_tab1- <field1> to it_tab-key

APPEND it_tab.

ENDLOOP.

Thanks,

Lakshmi

Read only

0 Likes
2,223

Hi Lakshmi,

The way you want it to work is not possible I think. This is beacuse in key field you can store only references of other data objects i.e. of other table field, not the value itself. You will later need to dereference it using field symbol in order to get value behind it. So the code would look like


LOOP AT it_tab1 INTO wa_it_tab1.
get reference of wa_it_tab1-some_field into it_tab-key.  
APPEND it_tab.
ENDLOOP.

So now IT_TAB-KEY holds the reference of SOME_FIELD from IT_TAB1 (not the value!). If you want to know the value you need to dereference it, but you cannot store it directly in this IT_TAB table.


field-symbols <value> type any.

loop at it_tab into wa_it_tab.
   assign wa_it_tab-key to <value>.
   write <value>.  "here you have the value behind this reference
endloop.

The problem which however raises is that when you refresh your original table IT_TAB1, the reference in this new table IT_TAB-KEY will "point" to nothing.

Generally I don't understand your business requirement. If you want to have dynamic field which holds the value you have to generate whole table dynamically, then pass the values from the original table to this newly cretead one. This way you can have any fields in that table and populate them with respective values.

Otherwise going the above way, you can only have a reference of some other field/data objects (but not the value). This will gone after the referencing object is gone too. Assuming above you have to use the dynamic table generation rather that generic field which holds only a reference.

Hope this is clear

Marcin

Read only

Former Member
0 Likes
2,223

Create a dynamic table using field catalog. You can create the field catalog based on your required fields.

Check this link......

https://wiki.sdn.sap.com/wiki/display/ABAP/DynamicInternaltable