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

Get structure from TYPE ANY variable

sistemes_emaya
Explorer
0 Likes
19,732

Hi,

I have a method wich has the importing parameter: TABLA TYPE ANY. I need to know its structure and I have the following code:



data: dtTabla type ref to data,
        estructura type ref to cl_abap_structdescr, "CL_ABAP_TYPEDESCR.
        lt_components     TYPE abap_component_tab,
        PV_TABLA TYPE DD02L-TABNAME.

  FIELD-SYMBOLS: <tablaITab> type any table,
                 <FS> type any.

  PV_TABLA = 'TABLA'.

  Create data dtTabla TYPE STANDARD TABLE OF (PV_TABLA).
  assign dtTabla->* to <tablaITab>.

  estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( <tablaITab> )." tabla ).
  lt_components = estructura->get_Components( ).

But the program throws an exception in line "Create data dtTabla TYPE STANDARD TABLE OF (PV_TABLA)."

What am I doing wrong?

Thanks.

Edited by: Sistemes Emaya on Nov 30, 2010 11:43 AM

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
7,934

Hello,

As mentioned by Max, PV_TABLA should contain a DDIC table/structure name & not a local table/structure.

BR,

Suhas

21 REPLIES 21
Read only

Former Member
0 Likes
7,934

Hi

Create data dtTabla TYPE STANDARD TABLE OF (PV_TABLA).

This code works if PV_TABLE contains the name of dictionary structure and perhaps of a type declared in the program.

TABLA is the importing parameter

But why don't you transfer it to method DESCRIBE_BY_DATA directly:

estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( TABLA ).
lt_components = estructura->get_Components( ).

Max

Read only

0 Likes
7,934

Hi Max,

This was my first option but when the following statemen is executed:

 estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( tabla ).

It throws CX_SY_MOVE_CAST_ERROR exception.

Tabla is an internal table wich I don't know its type, maybe the parameter declaration is incorrect??

Read only

roberto_vacca2
Active Contributor
0 Likes
7,934

Hi...

that's a general routine for getting info about a table structure using field symbols:

Probably you're using a bad reference in the definition data.... <tablaITab> type any table should be=> <tablaITab> Type table ..

data: g_head_ref TYPE REF TO data. "or other types

data: g_item_ref TYPE REF TO data. "or other types...

..............................................................

field-symbols: <fs_head> type table.

field-symbols: <fs_wa> type any.

data: l_comp type flag.

DATA: MY_STRUCT TYPE REF TO CL_ABAP_STRUCTDESCR.

ASSIGN g_head_ref->* TO <fs_head>.

LOOP AT <fs_head> ASSIGNING <fs_wa>.

MY_STRUCT ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <fs_wa> ).

EXIT.

ENDLOOP.

Hope to help,

Read only

0 Likes
7,934

Hi Roberto,

The problem is that I don't know the type of the internal table wich is passed by parameter (type any??).

Your solution only works if I know previously the type.

Thanks.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
7,935

Hello,

As mentioned by Max, PV_TABLA should contain a DDIC table/structure name & not a local table/structure.

BR,

Suhas

Read only

0 Likes
7,934

Hi Suhas,

Then how can I get the structure from an internal table which I don't know its type??

Thanks.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
7,934

Hello,

How is "TABLA" defined in your program? Is it a table / structure ?

BR,

Suhas

Read only

0 Likes
7,934

Hi Suhas,

Tabla is a parameter of an object method, its definition is the following: TABLA TYPE ANY (importing).

I want to call this method with an internal table in parameter Tabla, but the structure of the internal table is unkown and then the method has to read the structure and the data of the internal table.

Thanks.

Read only

Former Member
0 Likes
7,934

Hi

TYPE ANY doesn't define an internal table

Max

Read only

0 Likes
7,934

Hi Max,

I suspected something like that, do you know how I have to declare the parameter TABLA ?

Thanks.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
7,934

Hello,

My Bad You had mentioned it earlier i should have read your initial post carefully !

But TYPE ANY doesn't define an internal table, generic type for a table should be ANY TABLE.

Anyway check this code snippet:

DATA: go_tab_descr TYPE REF TO cl_abap_tabledescr,
      go_struc_descr TYPE REF TO cl_abap_structdescr,
      wa_comp TYPE abap_compdescr.
 
FIELD-SYMBOLS:
<wa> TYPE ANY, "FS to store Work Area value
<val> TYPE ANY.  "FS to store field value
 
go_tab_descr ?= cl_abap_tabledescr=>describe_by_data( <itab> ).
 
CHECK sy-subrc = 0.
 
"Get the structure of your internal table
go_struc_descr ?= go_tab_descr->get_table_line_type( ).
 
LOOP AT <itab> ASSIGNING <wa>.
  LOOP AT go_struc_descr->components INTO wa_comp.
*   To access the components of the structure dynamically
    ASSIGN COMPONENT wa_comp-name OF STRUCTURE <wa> TO <val>.
  ENDLOOP.
ENDLOOP.

You can check this thread for further details.

Hope this is useful.

BR,

Suhas

Edited by: Suhas Saha on Nov 30, 2010 4:49 PM

Read only

Former Member
0 Likes
7,934

I suspected something like that, do you know how I have to declare the parameter TABLA ?

TABLA TYPE TABLE.

Max

Read only

0 Likes
7,934

Hi Max,

Thanks for your reply, I've changed the type parameter to TABLE and now it works almost perfect, I just have the following issue: I need to know the structure of the internal table, but I just can get the names of the fields not the types.

If I try the following code (with TABLA TYPE TABLE):


data: estructura type ref to CL_ABAP_TYPEDESCR.

estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( tabla ).

The object "estructura" just have a property called "Key" in which I can see the the fields names of the internal table but not its type.

If I try to change the declaration of the estructure variable:



data: estructura type ref to cl_abap_structdesc.

estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( tabla ).

It throws an CX_SY_MOVE_CAST_ERROR exception, and I can't see the "components" property.

Thanks.

Read only

Former Member
0 Likes
7,934

This is the same example, but with internal table:

DATA: T_BKPF TYPE STANDARD TABLE OF BKPF.
DATA: T_T001 TYPE STANDARD TABLE OF T001.

APPEND INITIAL LINE TO T_T001.
PERFORM GET_STRUT USING T_T001.
APPEND INITIAL LINE TO T_BKPF.
PERFORM GET_STRUT USING T_BKPF.

FORM GET_STRUT USING TABLA TYPE TABLE.
  DATA: ESTRUCTURA    TYPE REF TO CL_ABAP_STRUCTDESCR,
        LT_COMPONENTS TYPE ABAP_COMPONENT_TAB,
        LT_COMPONENT  TYPE ABAP_COMPONENTDESCR.

  FIELD-SYMBOLS: <WA> TYPE ANY.

  READ TABLE TABLA ASSIGNING <WA> INDEX 1.

  ESTRUCTURA ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( <WA> ).
  LT_COMPONENTS = ESTRUCTURA->GET_COMPONENTS( ).

  LOOP AT LT_COMPONENTS INTO LT_COMPONENT.
    WRITE: / LT_COMPONENT-NAME.
  ENDLOOP.

ENDFORM.                    "GET_STRUT

Max

Read only

0 Likes
7,934

Hi Max and Suhas,

Thanks for your replies, now it works percfetly with your help.

thanks guys.

Read only

Former Member
0 Likes
7,934

Hi,

Could u plese check this program it may help u.

PARAMETER: p_table TYPE tabname DEFAULT 'ZTESTTE'.
 
DATA: dref TYPE REF TO data.
 
FIELD-SYMBOLS:
<itab> TYPE STANDARD TABLE, "Dyn. Int. Table
<wa> TYPE ANY, "Work Area
<val> TYPE ANY.
 
*Create the data reference for internal table
CREATE DATA dref TYPE STANDARD TABLE OF (p_table).
 
*De-referencing the data reference variable
ASSIGN dref->* TO <itab>.
CLEAR dref.
 
* Create data reference for work area
CREATE DATA dref TYPE (p_table).
ASSIGN dref->* TO <wa>.
 
DO.
  ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <val>.
  IF sy-subrc = 0.
    <val> = 'Test SDN'.
  ELSE.
    APPEND <wa> TO <itab>.
    CLEAR <wa>.
    EXIT.
  ENDIF.
ENDDO.

Thanks,

Balu.

Read only

0 Likes
7,934

Hi subrahmanyam24,

Your solution don't work with internal tables.

Thanks.

Read only

0 Likes
7,934

I've tried this code and it works fine:

TABLES: T001, BKPF.

PERFORM GET_STRUT USING T001.
PERFORM GET_STRUT USING BKPF.


FORM GET_STRUT USING TABLA TYPE ANY.
  DATA: ESTRUCTURA    TYPE REF TO CL_ABAP_STRUCTDESCR,
        LT_COMPONENTS TYPE ABAP_COMPONENT_TAB,
        LT_COMPONENT  TYPE ABAP_COMPONENTDESCR.

  ESTRUCTURA ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( TABLA ).
  LT_COMPONENTS = ESTRUCTURA->GET_COMPONENTS( ).

  LOOP AT LT_COMPONENTS INTO LT_COMPONENT.
    WRITE: / LT_COMPONENT-NAME.
  ENDLOOP.

ENDFORM.                    "GET_STRUT

Max

Read only

0 Likes
7,934

Hi Max,

Ok, but this tables are defined in DDIC, but the internal tables is declared with types which are not declared in DDIC their types are declared in other programs.

Thanks.

Read only

Former Member
0 Likes
7,934

Hi,

Please find the code first we need create the internal table after that we need create the stuctue for that table.

data: dtTabla type ref to data,

estructura type ref to cl_abap_structdescr, "CL_ABAP_TYPEDESCR.

lt_components TYPE abap_component_tab,

PV_TABLA TYPE DD02L-TABNAME.

FIELD-SYMBOLS: <tablaITab> type any table,

<FS> type any.

pv_tabla = 'LFA1'.

CREATE DATA dtTablaTYPE STANDARD TABLE OF (PV_TABLA).

ASSIGN dtTabla->* TO <tablaITab>.

DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( pV_table ).

idetails[] = ref_table_des->components[].

LOOP AT idetails INTO xdetails.

CLEAR xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

APPEND xfc TO ifc.

CLEAR xdetails.

ENDLOOP.

regards,

muralii

Read only

roberto_vacca2
Active Contributor
0 Likes
7,934

Hi...

if your tablaITable is a table you should have any data inside, so try this one:


data: dtTabla type ref to data,
        estructura type ref to cl_abap_structdescr, "CL_ABAP_TYPEDESCR.
        lt_components     TYPE abap_component_tab,
        PV_TABLA TYPE DD02L-TABNAME.

FIELD-SYMBOLS: <TABLA> TYPE ANY.


  FIELD-SYMBOLS: <tablaITab> type any table,
                 <FS> type any.

ASSIGN PV_TABLA TO <TABLA>.
<TABLA> = 'MARA'.


  Create data dtTabla TYPE STANDARD TABLE OF (PV_TABLA).
  assign dtTabla->* to <tablaITab>.

LOOP AT <tablaITab> ASSIGNING <fs>.

  estructura ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( <fs> )." tabla ).
  lt_components = estructura->get_Components( ).
ENDLOOP.