Application Development 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: 

Create structure at run time for table type any

Former Member
0 Kudos
2,947

Hi,

I am trying to create a local structure of type t_table inside one method where t_table type any , and it is an importing parameter for this method.

At run time t_table can contain any table data like gt_flight(custom internal table).

now inside my method I have to determine the structure of this incoming table t_table and then will create local strcuture of it's type.

From SDN I got some solution but it's having some limitation as it will work only when 'gt_flight' strcuture is defined inside respository.

lv_descr_ref ?= cl_abap_typedescr=>describe_by_data( t_table ).

Platform : MySAP ERP 2004

Please let me know if anyone have some solution for this.

Thanks in advance.

Sanjay

1 ACCEPTED SOLUTION

Former Member
537

Sanjay,

You can try something like this (Older version - however it works).

FIELD-SYMBOLS : <FT_TABLE> TYPE ANY TABLE.

assign table to <FT_TABLE>.

CREATE DATA workarea like line of <FT_TABLE>.

Regards,

Ravi

Note : Please mark the helpful answers.

11 REPLIES 11

Former Member
0 Kudos
537

Hi Sanjay,

Its right

cl_abap_typedescr=>describe_by_data( t_table )

requires that the table definition must be defined in the ABAP Dictionary.

You can create custom <b>structures</b> in SE11 and use them in your program.

Regards,

Wenceslaus.

0 Kudos
537

But every time I will get a new custom structure as input so it will not be a good idea to create a static strcuture in SE11 .

Sanjay

0 Kudos
537

check out this code sample taken from ABAP key word documentation, i guess this is what you want.

DATA: struct_type TYPE REF TO cl_abap_structdescr, 
      comp_tab TYPE cl_abap_structdescr=>component_table, 
      comp LIKE LINE OF comp_tab, 
      dref TYPE REF TO data. 

FIELD-SYMBOLS: <struc> TYPE ANY, 
               <comp> TYPE ANY. 

comp-name = 'column1'. 
comp-type = cl_abap_elemdescr=>get_c( 40 ). 
APPEND comp TO comp_tab. 

comp-name = 'column2'. 
comp-type = cl_abap_elemdescr=>get_i( ). 
APPEND comp TO comp_tab. 

struct_type = cl_abap_structdescr=>create( comp_tab ). 

CREATE DATA dref TYPE HANDLE struct_type. 

ASSIGN dref->* TO <struc>. 
ASSIGN COMPONENT 'COLUMN1' OF STRUCTURE <struc> TO <comp>. 
<comp> = 'Amount'. 

ASSIGN dref->* TO <struc>. 
ASSIGN COMPONENT 'COLUMN2' OF STRUCTURE <struc> TO <comp>. 
<comp> = 11. 

Regards

Raja

Former Member
538

Sanjay,

You can try something like this (Older version - however it works).

FIELD-SYMBOLS : <FT_TABLE> TYPE ANY TABLE.

assign table to <FT_TABLE>.

CREATE DATA workarea like line of <FT_TABLE>.

Regards,

Ravi

Note : Please mark the helpful answers.

0 Kudos
537

Thanks for that hint Ravi.

One small doubt what is the type of workarea you are taking ??

data workarea type ??

0 Kudos
537

Sanjay,

You can define work area like this.

Data : workarea TYPE REF to DATA.

This is dynamic declaration if you don't know the type at design time.

regards,

Ravi

Note : Please mark the helpful answers.

0 Kudos
537

Hi all,

Thanks a lot for your valuable tips for that problem.

After a long struggle utltimately it is resolved now and the soltuion is as follows.

---

--

field-symbols:

<fs> type any table,

<tab> type any,

<field> type any,

<wa> type any.

*--Assign incoming table to <fs>

assign t_table to <fs>.

*--create a data ref to <fs> ,LIKE will create a ref to an empty

*--structure <fs> which is required to create tree instance

create data tab_ref like <fs>.

assign tab_ref->* to <tab>.

*--Create wa of type <fs>

create data wa_ref like line of <fs>.

assign wa_ref->* to <wa>.

--

--

This will give you an empty table type of T_TABLE(i.e table any) as well as WA type T_TABLE.

Even though I had already touched almost all soltuions suggested by you guys before this posting but it got clicked just after Ravi's reply i.e WA type ref to ...

Thanks a lot Ravi.

Regards,

Sanjay

0 Kudos
537

Sanjay,

Glad to hear that your problem is solved.

Please mark the helpful answers and mark the thread as closed.

Regards,

Ravi

jayanthi_jayaraman
Active Contributor
0 Kudos
537

Hi,

Try this sample code and kindly reward points by clicking the star on the left of reply,if it helps.

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

0 Kudos
537

Hi Jayaraman,

This technique is really useful if we have predefined structure in the data dictionary(as in your case it is SYST).

But in my case t_table(my importing parameter of the method) is of type ANY that means it can contain any strcuture at the run time.

following your technique I tried to implement my scenario like this.

-


report zxyz.

types : begin of t_tree,

carrid type s_carr_id,

planetype type s_planetye,

end of t_tree.

data gt_tree type table of t_tree.

select carrid planetype up to 5 rows from sflight

into table gt_tree.

call method cap_alv->publish_alv_tree

exporting

t_table = gt_tree.

-


method publish_alv_tree

*!importing t_table type any

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 't_table'

CHANGING

ct_fieldcat = it_fcat[].

--

-


Even if somehow you pass the structure t_tree to the method but it still won't work b'cuz i_structure_name expects a structure name which is already defined in the DD.

If still I am missing your solution anywhere pls let me know.

Thanks,

Sanjay

0 Kudos
537

if you want to use 'REUSE_ALV_FIELDCATALOG_MERGE' for your internal table you have to follow like below

move: sy-repid to wf_repid ,

t_table to int_tab_name .

translate int_tab_name to upper case .

call function 'REUSE_ALV_FIELDCATALOG_MERGE'

exporting

i_program_name = wf_repid

i_internal_tabname = int_tab_name

  • I_STRUCTURE_NAME =

  • I_CLIENT_NEVER_DISPLAY = 'X'

i_inclname = wf_repid

i_bypassing_buffer = 'X'

  • I_BUFFER_ACTIVE =

changing

ct_fieldcat = wf_fld_cat[]

exceptions

inconsistent_interface = 1

program_error = 2

others = 3

.

Regards

Raja