3

In ABAP, what would be the fastest way to concatenate items of the same field from multiple lines into a field of one line?

My program is supposed to report a list of payments, the vendor's ID, and the vendor's email addresses.

Email addresses are stored in table ADR6, one line per address, along with the vendor's ID they belong to.

For the report I would need an internal table populated with vendor IDs (unique key) and concatenated email_addresses, separated by semicolons.

How to populate this internal table?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Cutter
  • 1,673
  • 7
  • 27
  • 43
  • if you have any performance problems, they are most likely not from concatenating strings. In your case, you would select the needed entries from ADR6 into an internal table, loop over that table adding the entry from the current line to your concatenation string. Your code will spend most of its time collecting the data from the database, not concatenating strings. Time improving performance would best be spent there. – Dirk Trilsbeek Feb 11 '15 at 17:35

2 Answers2

4

There is no real magic way to concatenate some fields from a table. Just use something like:

data: email_addresses type string.

loop at [table with addresses] assigning field symbol(<address>).
  at first.
    email_addresses = <address>-[email field].
    continue.
  endat.

  concatenate email_addresses ';' <address>-[email field] into email_addresses.
endloop.

Only faster method I can think of would involve native SQL.

Gert Beukema
  • 2,510
  • 1
  • 17
  • 18
4

HANA Solution

Use string_aggr:

SELECT vendor, STRING_AGG(email_address,';') 
FROM ADR6
GROUP BY vendor;

HANA Academy has a video demonstrating the use, along with sample code. Note that this solution requires HANA SPS09.

Lilienthal
  • 4,327
  • 13
  • 52
  • 88