cancel
Showing results for 
Search instead for 
Did you mean: 

Internal table that holds any structure

0 Kudos

Hello experts,

How do I create an internal table which can hold any structure in it.

For e.g., I have structures <b>vseoattrib</b>, <b>vseoevent</b> and so on. I have to push these structures into the same internal table <b>components</b>.

Also I have to read these structures from the internal table and put it into a variable of that structure type.

How can I achieve this??

Regards,

Yuvaraj

Accepted Solutions (0)

Answers (7)

Answers (7)

Former Member
0 Kudos

Your problem will be solved using following.

Field-Symbol type any table

data : i_tab type ref to data

create data

I don't have time right now to write a program for proof of concept but it can be made work.

Former Member
0 Kudos

Hi Yuvaraj,

1. What is ur exact requirement ?

2. If u want to make things dynamic,

then EXPORT / IMPORT is required.

3. If u want to use pre-defined,

then u can go for just a normal

INCLUDE STRUCTURE.

regards,

amit m.

Former Member
0 Kudos

Looks like what you are looking for is similar to how an IDOC data is stored. Look at EDIDD structure. It has one field that identifies the structure name and the data for the structure itself is stored in a long character field SDATA. May be you can also do similarly. Define your internal table as

data: begin of itab occurs 0,

structure_name like dd02l-tabname,

sdata like edidd-sdata.

data: end of itab.

Then when you later want to read the data, then you can do as follows.


LOOP AT itab.
  CASE itab-structure_name.
    WHEN 'VSEOATTRIB'.
      move itab-sdata to vseoattrib.
    WHEN 'VSEOEVENT'.
      move itab-sdata to vseoevent.
    WHEN 'XXXX'.
       .....
  ENDCASE.
ENDLOOP.

Hope this is what you are looking for.

Srinivas

0 Kudos

Hello Srinivas,

Look at the following code:

REPORT zyuva_dynamic.

TYPES: BEGIN OF t1,

a TYPE i,

b TYPE string,

END OF t1,

BEGIN OF t2,

a TYPE string,

b TYPE string,

x TYPE char1,

y TYPE char5,

END OF t2.

DATA: d1 TYPE t1,

d2 TYPE t2.

d1-a = 10.

d1-b = 'Yuva'.

d2-x = 'A'.

d2-y = 'abcde'.

d2-a = 'xyz'.

d2-b = 'zzz'.

DATA: BEGIN OF itab OCCURS 0,

structure_name LIKE dd02l-tabname,

sdata LIKE edidd-sdata.

DATA: END OF itab.

itab-structure_name = 'd1'.

itab-sdata = d1.

APPEND itab.

CLEAR: d1, d2.

LOOP AT itab.

CASE itab-structure_name.

WHEN 'T1'.

MOVE itab-sdata TO d1.

WHEN 'T2'.

MOVE itab-sdata TO d2.

ENDCASE.

ENDLOOP.

WRITE: / d1-a, d1-b.

WRITE: / d2-a, d2-b, d2-x, d2-y.

I am getting syntax error at line itab-sdata = d1.

How do I proceed?

Yuvaraj

former_member188685
Active Contributor
0 Kudos
REPORT zyuva_dynamic.

<b>TYPES: BEGIN OF t1,
a TYPE i,
b TYPE string,
END OF t1,

BEGIN OF t2,
a TYPE string,
b TYPE string,
x TYPE char1,
y TYPE char5,
END OF t2.</b>

DATA: d1 TYPE <b>t1</b>,
d2 TYPE <b>t2</b>.

d1-a = 10.
d1-b = 'Yuva'.

d2-x = 'A'.
d2-y = 'abcde'.
d2-a = 'xyz'.
d2-b = 'zzz'.

DATA: BEGIN OF itab OCCURS 0,
structure_name LIKE dd02l-tabname,
sdata LIKE edidd-sdata.
DATA: END OF itab.

itab-structure_name = 'd1'.
itab-sdata = d1.
APPEND itab.

CLEAR: d1, d2.

LOOP AT itab.
CASE itab-structure_name.
WHEN 'T1'.
MOVE itab-sdata TO d1.
WHEN 'T2'.
MOVE itab-sdata TO d2.
ENDCASE.
ENDLOOP.

WRITE: / d1-a, d1-b.
WRITE: / d2-a, d2-b, d2-x, d2-y.

there is one small correction in your way of coding.

declare the structures globally(which you have declared using types)

define the bold ones in se11(global structures)

and try...

it is working fine.

thanks

vijay

0 Kudos

Hello Vijay,

I am still getting an error like

"ITAB-SDATA" and "D1" are not mutually convertible in a Unicode system.

yuvaraj

former_member188685
Active Contributor
0 Kudos

hi yuvaraj,

since you are using string,int. SDATA is not accepting.

so if you want to pass the info to sdata then you should use char and specify length.

that will do..

thanks

vijay

Former Member
0 Kudos

Hi Yuvaraj,

1. ITAB-SDATA" and "D1" are not mutually convertible in a Unicode system

Make your table fields as CHARACTER fields

(Not string, not number etc)

and your problem will solve.

2. Take this code (just copy paste)

(its ur code only with minor modifications)

It runs fine.

*----


REPORT zyuva_dynamic.

*TYPES: BEGIN OF t1,

*a TYPE i,

*b TYPE string,

*END OF t1,

*

*BEGIN OF t2,

*a TYPE string,

*b TYPE string,

*x TYPE char1,

*y TYPE char5,

*END OF t2.

TYPES: BEGIN OF t1,

a TYPE c,

b(10) TYPE c,

END OF t1,

BEGIN OF t2,

a(10) TYPE c,

b(10) TYPE c,

x(10) TYPE c,

y(10) TYPE c,

END OF t2.

DATA: d1 TYPE t1,

d2 TYPE t2.

d1-a = 10.

d1-b = 'Yuva'.

d2-x = 'A'.

d2-y = 'abcde'.

d2-a = 'xyz'.

d2-b = 'zzz'.

DATA: BEGIN OF itab OCCURS 0,

structure_name LIKE dd02l-tabname,

sdata LIKE edidd-sdata.

DATA: END OF itab.

itab-structure_name = 'D1'.

itab-sdata = d1.

APPEND itab.

CLEAR: d1, d2.

LOOP AT itab.

CASE itab-structure_name.

WHEN 'D1'.

MOVE itab-sdata TO d1.

WHEN 'D2'.

MOVE itab-sdata TO d2.

ENDCASE.

ENDLOOP.

WRITE: / d1-a, d1-b.

WRITE: / d2-a, d2-b, d2-x, d2-y

.

Regards,

Amit M.

Former Member
0 Kudos

Hi Yuvraj,

It is not possible to do it this way as unicode checking is more stricter than the previous syntax check in the non-object oriented world.

You have to have all your structures compatible. Declare all the structure involved in moving from and to the SDATA field as character structures. Any numeric manipulations that need to be done, do it seperately.

Srinivas

ralf_geiger
Explorer
0 Kudos

Hi Yuvaraj,

when I understand your problem correctly you will have some structures of types you do not know yet and you want to have them in one component table. I wouldn't use INCLUDES because as you already noticed they can have the same component names. So why not making seperate components of those structures and have them in one table?

My idea would look like this:

TABLE components
  LINE:
  |__ struct1 type <dynamically structure>
  |__ struct2 type <dynamically structure>
  |__ ...

You can create this table with this coding:


DATA:
  lr_structdescr type ref to cl_abap_structdescr,
  lt_components type CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
  ls_component type line of CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

  DATA:
     lr_line type ref to data,
     lr_table type ref to data.

  FIELD-SYMBOLS:
     <comp> type any,
     <line> type any,
     <table> type any table.

  CONSTANTS: lcon_struct1 TYPE string value '<Structure Type 1>',
             lcon_struct2 TYPE string value '<Structure Type 2>'.

**********************************************************
* Create line dynamically.

* Create two structure components in one line
  lr_structdescr ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_NAME( lcon_struct1 ).
  ls_component-name = 'STRUCT1'.
  ls_component-type ?= lr_structdescr.
  APPEND ls_component TO lt_components.

  lr_structdescr ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_NAME( lcon_struct2 ).
  ls_component-name = 'STRUCT2'.
  ls_component-type ?= lr_structdescr.
  APPEND ls_component TO lt_components.

* Create descriptor for line of table.
  lr_structdescr = CL_ABAP_STRUCTDESCR=>CREATE( lt_components ).

* Now create data:
  CREATE DATA lr_line TYPE HANDLE lr_structdescr.
  ASSIGN lr_line->* TO <line>.
  CREATE DATA lr_table LIKE TABLE OF <line>.
  ASSIGN lr_table->* TO <table>.

*********************************************************

What you now have is a table which has two components (STRUCT1 and STRUCT2). Those components are typed dynamically at runtime.

To fill these table you can Fill the <line> and insert it into the <table> e.g.

*------- Directly fill the data table.

* Either adress the components like this
  ASSIGN COMPONENT 'STRUCT1' OF STRUCTURE <line> TO <comp>.
  <comp> = <your data line of structure 1>.

* or this
  ASSIGN lr_line->('STRUCT2') TO <comp>.
  <comp> = <your data line of structure 2>.

  INSERT <line> INTO TABLE <table>.

**********************************************************

If you now want, you can export this table into an itab (as my predecessor suggested). The Export doesn't care if this is a static or dynamic structure. It just pushes the data into your itab and tag it with a name.

e.g.:


*------- Using Export 
  TYPES: "buffer table type
    BEGIN OF itab_line,
      clustr TYPE int2,
      clustd(1000) TYPE x,
    END OF itab_line.

  DATA:
    itab type table of itab_line. "buffer table

  EXPORT table FROM <table> TO INTERNAL TABLE itab.

**********************************************************

What you now did is you have the data from the table within the itab.. but serialized.

**********************************************************

Reading the <table> directly without having it exported would look like this:


  LOOP AT <table> ASSIGNING <line>.

    ASSIGN COMPONENT 'STRUCT1' OF STRUCTURE <line> TO <comp>. " CASTING TYPE <Structure>.

  ENDLOOP.

Using your example you can have the struct vseoattrib now in that <comp>. Using the CASTING you could cast it to a known Structure type.

**********************************************************

When you used EXPORT you need to import the data back to work with it. So this wold look like this:


* Read from exported table into data.
  CLEAR: <table>.

  IMPORT table TO <table> FROM INTERNAL TABLE itab.

Make sure your <table> is assigned to the right data..

That's it! I don't know what you want to do with that table exactly but only to summerize structures that belong together (in one line) and you do not know those structure types yet you can use this solution.

Regards,

Ralf

Former Member
0 Kudos

data : begin of itab.

include structure vseoattrib.

include structure vseoevent.

*--and so onn.you can include structures like this.

*--even you can add variables also here .

data : matnr like mara-matnr.

data :end of tab.

for your 2nd q's :

suppose vseoattrib have a field f1,

then

use

read table itab index <some index_based on your condition>.

either you can use MOVE CORRESPONDING

or manually assigning itab field values to different variables.

regards,

srikanth

0 Kudos

Hello,

What if I have components with similar name in the structures??

regards, yuvaraj

Message was edited by: Yuvaraj Ramamurthy

Former Member
0 Kudos

Hi again,

1. If you are talking about INCLUDE Strucutre, then

It will give error !

2. I probably understood your question

in another way, thats why i had

answered in that way !

regards,

amit m.

Message was edited by: Amit Mittal

Former Member
0 Kudos

Hi Yuvaraj,

1. Try this code (just copy paste)

Notice how the data is exported, then cleared

and again got back using import.

REPORT abc.

*----


TYPES: BEGIN OF itab_line,

clustr TYPE int2,

clustd(1000) TYPE x,

END OF itab_line.

DATA: itab TYPE TABLE OF itab_line.

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

data : t002 LIKE TABLE OF t002 WITH HEADER LINE.

SELECT * FROM t001 INTO TABLE t001.

SELECT * FROM t002 INTO TABLE t002.

EXPORT t001 t002 TO INTERNAL TABLE itab.

BREAK-POINT.

*----


refresh t001.

clear t001.

refresh t002.

clear t002.

*----


import t001 t002 from internal table itab.

BREAK-POINT.

Regards,

Amit M.

Message was edited by: Amit Mittal

0 Kudos

Hello Amit,

Exporting is OK. But while importing I donno the type of the structure at the top. How do I import the structure to the variable of that structure type?

regards,

yuvaraj

Former Member
0 Kudos

Hi again,

1. donno the type of the structure at the top.

its not clear !

2. Please note that

while using EXPORT / IMPORT

we MUST KNOW THE STRUCTURES.

There is no way out !

regards,

amit m.

hymavathi_oruganti
Active Contributor
0 Kudos

the concept of FIELD-GROUPS may help u.

Former Member
0 Kudos

Hi,

Declare an internal table like:

types: begin of ty_table

include type vseoattrib

include type vseoevent

......so on

end of ty_table.

data : IT_TABLE type table of ty_table.

Hope it helps...

Lokesh

Pls. reward appropriate points