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

type any

former_member196517
Contributor
0 Likes
789

Hi colleagues,

can you please let me know how can i build a line type from a dynamic table of type any..

i mean i have a dynamic table at runtime which is of type any.. can i build a line type because i need to loop thru this.

Regards

Anuj

6 REPLIES 6
Read only

Former Member
0 Likes
756

Given that you already have a field-symbol pointing at your original type named: <orgTable> and it also assumes that your table's row type is a structure, it wouldn't be too hard to modify it to support other types of line types. The following should work (although haven't tried it in the ABAP ide):

code

data lineType type ref to cl_abap_structdescr.

data tableType type ref to cl_abap_tabledescr.

data componentTable type component_table..

tableType ?= cl_abap_tabledescr=>describe_by_data( <orgType> ).

lineType ?= tableType->get_table_line_type( ).

componentTable = lineType->get_components( ).

append your additional type here.

append fooType to componentTable.

lineType ?= cl_abap_structDescr=>create( componentTable ).

tableType = cl_abap_tabledescr=>create( lineType ).

now create your new data area.

data newTable type ref to data.

field-symbol <newTable> type standard table.

create data newTable handle tableType.

assign newTable->* to <newTable>.

[/code]

Edited by: Parminder Singh Saluja on Jan 21, 2008 10:47 AM

Read only

Former Member
0 Likes
756

Anuj,

Use FIELD SYMBLes for this.

FIELD-SYMBOLS : <gf_table> TYPE ANY TABLE.

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/frameset.htm

Don't forget to reward if useful

Read only

Former Member
0 Likes
756

hi

good

check the below code

If i understand you correctly you are looking to take a table generated dynamically and add another column to it.

Given that you already have a field-symbol pointing at your original type named: <orgTable> and it also assumes that your table's row type is a structure, it wouldn't be too hard to modify it to support other types of line types. The following should work (although haven't tried it in the ABAP ide):

code

data lineType type ref to cl_abap_structdescr.

data tableType type ref to cl_abap_tabledescr.

data componentTable type component_table..

tableType ?= cl_abap_tabledescr=>describe_by_data( <orgType> ).

lineType ?= tableType->get_table_line_type( ).

componentTable = lineType->get_components( ).

append your additional type here.

append fooType to componentTable.

lineType ?= cl_abap_structDescr=>create( componentTable ).

tableType = cl_abap_tabledescr=>create( lineType ).

now create your new data area.

data newTable type ref to data.

field-symbol <newTable> type standard table.

create data newTable handle tableType.

assign newTable->* to <newTable>.

[/code]

thanks

mrutyun^

Read only

0 Likes
756

Hi ALL,

thanks for reply,

however my problem is that i am in function module which imports a table of type any.. i want to loop thru this now.. how is it possible.

Regards

Anuj

Read only

matt
Active Contributor
0 Likes
756

If you don't know the names or number of components then do this:

FIELD-SYMBOLS <wa> TYPE ANY.

FIELD-SYMBOLS <fld> TYPE ANY.

DATA fld_count TYPE i.

LOOP AT fm_table ASSIGNING <wa>.

i = 1.

DO.

ASSIGN COMPONENT i OF STRUCTURE <wa> TO <fld>.

IF sy-subrc IS NOT INITIAL:

EXIT.

ENDIF.

...<fld> contains the value of the ith component of the current record of fm_table.

ENDDO.

ENDLOOP.

If you do know the names of the components

FIELD-SYMBOLS <wa> TYPE ANY.

FIELD-SYMBOLS <fld> TYPE ANY.

DATA: lp_data TYPE REF TO DATA.

CREATE DATA lp_data LIKE LINE OF fm_table.

ASSIGN lp_data->* TO <wa>.

ASSIGN COMPONENT (name or number) OF STRUCTURE <wa> TO <fld>.

LOOP AT fm_table INTO <wa>.

... <fld> contains the value of the component assigned above, in the current record of fm_table.

ENDLOOP.

Read only

0 Likes
756

Hi Anuj,

I've got exactly the same problem. I solved it (partially based on Parminder's code, thanks for that!) this way:


FUNCTION ZANYTAB.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_ANYTAB) TYPE  ANY TABLE
*"----------------------------------------------------------------------

* Declarations
  DATA: descr_ref   TYPE ref to cl_abap_typedescr,
        descr_table TYPE ref to cl_abap_tabledescr,
        descr_struc TYPE ref to cl_abap_structdescr.

  FIELD-SYMBOLS: <comp_wa> TYPE abap_compdescr.

  descr_ref = cl_abap_typedescr=>describe_by_data( I_ANYTAB ).
  descr_table ?= ls_descr_ref.
  descr_struc ?= descr_table->get_table_line_type( ).

  LOOP AT descr_struc->components ASSIGNING <comp_wa>.
    WRITE: / <comp_wa>-name, <comp_wa>-type_kind,
             <comp_wa>-length, <comp_wa>-decimals.
  ENDLOOP.

ENDFUNCTION.

Hope this helps.

Regards,

Bernd