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

Declare a "type" table using parameter

Former Member
0 Likes
7,555

Hi experts,

i'm wondering if its posible to declare a X table from a parameter the user send...

PARAMETERS: type_table TYPE STRING.


DATA: temp_table TYPE type_table.


################################


Execute program


type_table: T001


so temp_table would be T001 type.


Regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,057

Have a look at this snippet. It creates a data reference of table type specified by user, and then uses a select query to populate 10 records of that database table to data reference via field symbol.

PARAMETERS tabname TYPE dd02l-tabname OBLIGATORY DEFAULT 'MARA'.

DATA dref TYPE REF TO data.

CREATE DATA dref TYPE STANDARD TABLE OF (tabname).

FIELD-SYMBOLS <fs> TYPE ANY TABLE.

ASSIGN dref->* TO <fs>.

SELECT * FROM (tabname) INTO TABLE <fs> up to 10 rows.

Have a look at this snippet. It creates a data reference of table type specified by user, and then uses a select query to populate 10 records of that database table to data reference via field symbol.

PARAMETERS tabname TYPE dd02l-tabname OBLIGATORY DEFAULT 'MARA'.

DATA dref TYPE REF TO data.

CREATE DATA dref TYPE STANDARD TABLE OF (tabname).

FIELD-SYMBOLS <fs> TYPE ANY TABLE.

ASSIGN dref->* TO <fs>.

SELECT * FROM (tabname) INTO TABLE <fs> up to 10 rows.

11 REPLIES 11
Read only

Former Member
0 Likes
5,057

Hello Alonso,

You can create dynamic internal tables. Review this link:

Dynamic Internal table - ABAP Development - SCN Wiki

I hope you help.

Read only

rosenberg_eitan
Active Contributor
0 Likes
5,057

Hi,

See also RTTS - Runtime Type Services .

               RTTC - Run Time Type Creation .

http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965

Regards.

Some code:

  • FORM test_08 .
  •  
  •   CONSTANTS: c_tabname TYPE tabname VALUE 'SCARR' .
  •  
  •   DATA: ob_abap_structdescr TYPE REF TO cl_abap_structdescr .
  •   DATA: ref_struct TYPE REF TO data .
  •   FIELD-SYMBOLS: <struct> TYPE ANY .
  •  
  •   ob_abap_structdescr ?= cl_abap_elemdescr=>describe_by_name( c_tabname ).

  •   CREATE DATA ref_struct TYPE HANDLE ob_abap_structdescr .
  •   ASSIGN ref_struct->* TO <struct> .
  •  
  •   DATA: ob_abap_tabledescr  TYPE REF TO cl_abap_tabledescr .
  •   DATA: ref_table TYPE REF TO data .
  •   FIELD-SYMBOLS: <table> TYPE table .
  •  
  •   ob_abap_tabledescr = cl_abap_tabledescr=>create( ob_abap_structdescr ).

  •   CREATE DATA ref_table TYPE HANDLE ob_abap_tabledescr .
  •   ASSIGN: ref_table->* TO <table> .
  •  
  •   SELECT * INTO CORRESPONDING FIELDS OF TABLE <table>
  •   FROM (c_tabname)
  •   UP TO 20 ROWS .
  •  
  •   LOOP AT <table> ASSIGNING <struct> .
  •  
  •   ENDLOOP.
  •  
  • ENDFORM .                                                   "test_08
Read only

Former Member
0 Likes
5,057

Hello Alonso,

Its better to create dynamic tables which would give you output at run time.

Refer the below link for more clear understanding.

Read only

0 Likes
5,057

Hi,

Please note that at the top of this article our lady guru recommend using RTTS .

Regards.

Read only

Former Member
0 Likes
5,058

Have a look at this snippet. It creates a data reference of table type specified by user, and then uses a select query to populate 10 records of that database table to data reference via field symbol.

PARAMETERS tabname TYPE dd02l-tabname OBLIGATORY DEFAULT 'MARA'.

DATA dref TYPE REF TO data.

CREATE DATA dref TYPE STANDARD TABLE OF (tabname).

FIELD-SYMBOLS <fs> TYPE ANY TABLE.

ASSIGN dref->* TO <fs>.

SELECT * FROM (tabname) INTO TABLE <fs> up to 10 rows.

Read only

0 Likes
5,057

Hi,

One more ingredient....

Read only

0 Likes
5,057

That was correct!!!

PARAMETERS: tipo_tab    TYPE STRING.

SYMBOLS: <temp_table> TYPE STANDARD TABLE.


DATA struct TYPE REF TO DATA.
CREATE DATA struct TYPE STANDARD TABLE OF (tipo_tab).
ASSIGN struct->* TO <temp_table>.


<removed by moderator>

Message was edited by: Manish Kumar

Read only

philipdavy
Contributor
0 Likes
5,057
  • The below given link, exactly deals with your problem.

DynamicInternaltable

Regards,

Philip.

Read only

claudia_bolch
Explorer
0 Likes
5,057

Hi Alonso,

to create a table with an unknown type, you need a field-symbol instead of a "normal" internal table. You define it as

FIELD-SYMBOLS <table> TYPE table.

Then you need your parameter for the type of the table. Define it as

DATA type_table TYPE ddobjname.

Then you need an Object for your data. You define it as

DATA dref_t TYPE REF TO data.

Now you can use

TRY.

        CREATE DATA dref_t TYPE TABLE OF (type_table).

        ASSIGN dref_t->* TO <table>.

    CATCH cx_sy_create_data_error.

* Do some error handling

ENDTRY.

Now you can use <table> as internal table of the type given in the parameter type_table.

If you want some information about the structure of the table, you can use the function module DDIF_FIELDINFO_GET.

Regards

Claudia

Read only

Former Member
0 Likes
5,057

Hi Alonso,

You can do it easily using the following steps:

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>.


Thanks and Regards,

Ashish

Read only

Former Member
0 Likes
5,057

thank you all!!!

PARAMETERS: tipo_tab    TYPE STRING.

SYMBOLS: <temp_table> TYPE STANDARD TABLE.


DATA struct TYPE REF TO DATA.
CREATE DATA struct TYPE STANDARD TABLE OF (tipo_tab).
ASSIGN struct->* TO <temp_table>.