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: 

Return local table as a parameter of an method

Stef3
Explorer
0 Kudos

Hello everyone, I am new to abap programming. I have a local table where I store data from a join select. Which is defined like this 

    TYPES: BEGIN OF adrc,
             addrnumber TYPE c LENGTH 10,
             name1      TYPE c LENGTH 60,
             name2      TYPE c LENGTH 60,
             city1      TYPE c LENGTH 40,
             city2      TYPE c LENGTH 40,
             post_code1 TYPE c LENGTH 10,
             street     TYPE c LENGTH 60,
             house_num1 TYPE c LENGTH 10,
             house_num2 TYPE c LENGTH 10,
             adrnr      TYPE adrnr,
           END OF adrc.
Data:  lt_adrc          TYPE TABLE OF adrc, ...

How can I define now a return parameter for a method of the same local table like lt_adrc. Anything I find online like the following does not work.

  Methods: prepare_data IMPORTING lv_read_sucess TYPE abap_bool returning value(lt_data) type table of adrc,...

OR

TYPES tt_adrc TYPE STANDARD TABLE OF adrc WITH DEFAULT KEY.
...
          prepare_data IMPORTING lv_read_sucess TYPE abap_bool returning value(lt_data) type tt_adrc,...

Is there a solution or is this just not done anymore. 

Thank you so much!

Stefanie

6 REPLIES 6

Eduardo-CE
Active Participant
0 Kudos

Hello,

You need to use EXPORTING or CHANGING.

Heres a link that might help.

https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapmethods.htm

 

This is not true. Returning parameter is OK (and often recommended) to use.

-- Tomas --

Edrilan_Berisha
Product and Topic Expert
Product and Topic Expert

Hi Stefanie,

 

why is your second approach not working?

That should actually work.

 

Best,

Edrilan Berisha

SAP S/4HANA Cloud Financials Development


Sandra_Rossi
Active Contributor

As @Edrilan_Berisha said, your second approach should work, and maybe you are confused by defining a "local table" instead of just using the result parameter, no need to use an intermediate internal table.

Maybe another source of confusion (maybe the compiler is confused too), is that you named your type the same as the DDIC type "ADRC". Choose a program type name like TS_ADRC.

Example:

TYPES tt_adrc TYPE STANDARD TABLE OF ts_adrc WITH DEFAULT KEY.

METHODS prepare_data 
  IMPORTING 
    iv_read_sucess TYPE abap_bool 
  returning 
    value(rt_adrc) type tt_adrc.

METHOD prepare_data.
  SELECT ...
    FROM adrc
    INTO TABLE rt_adrc.
ENDMETHOD.

METHOD main.
  DATA(lt_adrc) = prepare_data( iv_read_success = abap_true ).
ENDMETHOD.

 

Tomas_Buryanek
Active Contributor

Where did you define that structure type "TYPES: BEGIN OF adrc..." ?
It should be defined in class. I hope it is not defined in method.

Another issue might be conflict/confusion with global data dictionary table ADRC which has same name like your type. Try to choose different name for your structure type.

After that it should work like in your example:

"Class definitions:
TYPES: BEGIN OF address_struc,
       ...
       END OF address_struc.
TYPES tt_address TYPE STANDARD TABLE OF address_struc WITH DEFAULT KEY.

"Method:
prepare_data RETURNING value(lt_data) type tt_address...
-- Tomas --

0 Kudos

Thank you all for your help. You are probably right that the compiler got confused, it is now working. I am now parallel looking how things should be defined.