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

Dynamic data table

madhura_lobo
Product and Topic Expert
Product and Topic Expert
0 Likes
1,112

Hi,

The following is the requirement. I am not familiar with working with data, table as types.

The interface has a method say get_data() to return the contents of a DB table. The classes implementing this interface can be querying different DB tables and hence the return type of the interface method should be generic type.

The program where this method get_data() is called and recieves the data returned will not be aware of the type of DB table returned. Now i have to take this returned data and convert it into an XML.

PROGRAM:

lo_badi_ei->get_data( IMPORTING e_data = lt_data ).

lt_data once recieved has to be converted to XML. I can use CALL TRANSFORMATION for that.

But what should be the type of lt_data? should it be

field-symbol: <lt_data> type standard table.

I tried but I am not able to figure out how to do the above requirement?

Any help will be appreciated.

Thanks and Regards,

Madhura Lobo

7 REPLIES 7
Read only

matt
Active Contributor
0 Likes
1,070

The calling program MUST know the structure of the imported table. The way round this is not to return the table in get_Data, but a reference to the table. (TYPE REF TO DATA). Then you can have

DATA: lr_data TYPE REF TO DATA.

FIELD-SYMBOLS: <lt_data> TYPE STANDARD TABLE. 

lr_data = lo_badi_ei->get_data( ).
ASSIGN lr_data->* TO <lt_data>.

Then use CALL TRANSFORMATION using <lt_data>.

matt

Read only

madhura_lobo
Product and Topic Expert
Product and Topic Expert
0 Likes
1,070

Thanks for the reply Matt.

Inside get_data() i have the following.

data:

lt_data type zssp_tt_area_data,

ls_data like LINE OF lt_data,

lt_rfc_data type zsca_tt_rfc_connection_data,

ls_rfc_data like LINE OF lt_rfc_data.

ls_data-area = 'A1'.

ls_data-data = lt_rfc_data.

how to assign ls_data to e_data ?

Thanks and Regards,

Madhura Lobo

Edited by: Madhura Lobo on Jan 21, 2011 12:25 PM

Read only

matt
Active Contributor
0 Likes
1,070

In the method, you have to create the data, and then store the reference to that data as an attribute of the class. For example:

data: ls_data like LINE OF lt_data,
      lt_rfc_data type zsca_tt_rfc_connection_data,
      ls_rfc_data like LINE OF lt_rfc_data.

field-symbols: <lt_data> type zssp_tt_area_data.
     
create data me->r_data type zssp_tt_area_data.
assign me->r_data->* to <lt_data>.

ls_data-area = 'A1'.
ls_data-data = lt_rfc_data.
insert ls_data into table <lt_data>.

r_refdata = me->r_data

Where r_refdata is the returning parameter of the method - TYPE REF TO DATA, and me->r_data is an attribute of the class - also TYPE REF TO DATA.

Note, you can't use GET REFERENCE OF lt_data, as lt_data only exists during the method call. You have to have something that remains in memory while in the calling part of your program.

Read only

Clemenss
Active Contributor
0 Likes
1,070

Hi Madhura,

>

> ls_data-area = 'A1'.

> ls_data-data = lt_rfc_data.

>

> how to assign ls_data to e_data ?

GET REFERENCE OF  ls_data INTO e_data.

where e_data is defined TYPE REF TO DATA.

If you want to use it, you have

FIELD-SYMBOLS:
  <s_data>TYPE LINE OF zssp_tt_area_data.
ASSIGN e_data->* TO  <s_data>.

Regards

Clemens

Read only

matt
Active Contributor
0 Likes
1,070

As I noted at the end of my previous posting, you can't use GET REFERENCE when the variable you are getting reference to is local. As soon as you leave the method, e_data will be initial.

Here's an example:

*----------------------------------------------------------------------*
*       INTERFACE lcl_if
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE lcl_if.

  METHODS: get_data EXPORTING er_data TYPE REF TO data.

ENDINTERFACE.                    "lcl_if

*----------------------------------------------------------------------*
*       CLASS lcl_int DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_int DEFINITION.

  PUBLIC SECTION.

    INTERFACES lcl_if.
    DATA: r_data TYPE REF TO data.

ENDCLASS.                    "lcl_int DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_int IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_int IMPLEMENTATION.

  METHOD lcl_if~get_data.

    DATA: l_some_data TYPE i VALUE 3.

    FIELD-SYMBOLS: <l_some_data> TYPE i.

    CREATE DATA me->r_data LIKE l_some_data.
    ASSIGN me->r_data->* TO <l_some_data>.

    <l_some_data> = l_some_data.

    er_data = me->r_data.

  ENDMETHOD.                    "lcl_if~get_Data

ENDCLASS.                    "lcl_int IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_string DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_string DEFINITION.
  PUBLIC SECTION.
    INTERFACES lcl_if.
    DATA: r_data TYPE REF TO data.

ENDCLASS.                    "lcl_string DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_string IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_string IMPLEMENTATION.

  METHOD lcl_if~get_data.

    DATA: l_some_data TYPE string VALUE `a string`.

    FIELD-SYMBOLS: <l_some_data> TYPE string.

    CREATE DATA me->r_data LIKE l_some_data.
    ASSIGN me->r_data->* TO <l_some_data>.

    <l_some_data> = l_some_data.

    er_data = me->r_data.

  ENDMETHOD.                    "lcl_if~get_Data

ENDCLASS.                    "lcl_string IMPLEMENTATION

DATA: r_data TYPE REF TO data,
      r_lcl  TYPE REF TO lcl_if.

FIELD-SYMBOLS: <data> TYPE ANY.

START-OF-SELECTION.

  CREATE OBJECT r_lcl TYPE lcl_int.
  r_lcl->get_data( IMPORTING er_data = r_data ).
  ASSIGN r_data->* TO <data>.
  WRITE: / <data>.

  CREATE OBJECT r_lcl TYPE lcl_string.
  r_lcl->get_data( IMPORTING er_data = r_data ).
  ASSIGN r_data->* TO <data>.
  WRITE: / <data>.

Edited by: Matt on Jan 22, 2011 7:23 AM

Read only

Clemenss
Active Contributor
0 Likes
1,070

Hi,

oh, sorry, Matt is right:

Local data are lost as soon as we leave current block. Data created dynamically will survive as long as any reference exists.

So wee need a slight modification, we will replace

*data: ls_data like LINE OF lt_data,

with

data: lr_data type ref to data,

and add the statements

FIELD-SYMBOLS: <s_data> like LINE OF lt_data.
CREATE DATA  lr_data like LINE OF lt_data.
ASSIGN lr_data->* to <s_data>.

Then replace all ls_data with <s_data>.

i.e.

GET REFERENCE OF  <s_data> INTO e_data.

Regards,

Clemens

Read only

madhura_lobo
Product and Topic Expert
Product and Topic Expert
0 Likes
1,070

Thanks a lot for the reply. It helped completely.