3
votes

I need to return the rows of some tables via RFC, and the names of these tables are not known before the execution.

I have this statement which gets executed in a loop:

  SELECT *
    up to iv_max_count rows
    into table <lt_result>
    FROM (iv_table_name) AS ltab
    WHERE (SQL_WHERE).

How can I concatenate the <lt_result> results to one list/table and return this via RFC?

Of course all tables can have different structures. Creating one big table which holds all rows does not help.

2
You might consider editing the title - you're not really looking for a list of tables here, you're looking to pass multiple tables back that are unknown until runtime. - Bryan Cain
@BryanCain I updated the title. Does it match to the topic now? - guettli
I believe that better describes what you’re trying to do, yes. - Bryan Cain
Why not simply serializing the rows to XML or JSON? - Sandra Rossi
@SandraRossi that's what we are currently do. But I guess binary RFC is better then ascii (bloated) JSON. - guettli

2 Answers

3
votes

You can't return an arbitrary structure or structures in an RFC, they have to be predefined.

The best way I can think of to do this is to mimic the way SAP handles idocs in the database. Your table would need a minimum of two fields, the first would be a descriptor field telling the caller what the table structure is, and the second field would be a very long character type field with all the data concatenated together, either fixed width or delimited. This way, you could pass data from multiple tables in the same return structure.

If your calling program really knows nothing about SAP data sets, you would probably also need to grab metadata from table DD02l.

1
votes

In short, that's not how ABAP and function modules work.

You have to define exactly what your input is and what your output structure/table looks like. You can return one structure that holds multiple deep nested tables, to have only one return structure, but not dynamically!

Making this all dynamic makes things a lot more complex. Mostly unnecessarily.

One possible way:

  1. you have to anaylize the input and build dynamic structures and tables for each input table result
  2. build a wrapping structure that consists of all the nested tables
  3. return a DATA reference object, because you cannot return generic datatypes
  4. your receiving program needs to have the same data structures defined, this means it must exactly know what it is getting back, to defer the data.

Another way: Use Function Module RFC_READ_TABLE in a loop in the caller program

Reading multiple single tables dynamically in a loop without a join does not sound like ABAP programming, more like "I need data from SAP in a third party tool".