-1

I'm new in using SQL in ABAP, so it must be a silly question, but is this somehow possible?:

select statement

DB tables and expected internal table

as I'm new, I cannot add images directly.

Thanks a lot in advance, kind regards

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 4
    ​​Please don't post code as image, copy/paste the code as text so that people can edit it. – Sandra Rossi Nov 26 '22 at 08:16
  • 2
    What you describe in the wanted result is not a join. It's just a combination of the data from the two tables. No need to use JOIN, just use two SELECTs and use APPENDING in the second one or both. – Gert Beukema Nov 28 '22 at 01:36
  • 2
    @GertBeukema You are a bit behind the time. [Since 7.50, ABAP supports UNION SELECT](https://blogs.sap.com/2015/11/09/abap-news-for-release-750-select-union/), so you can do that without a 2nd network round-trip to the database server. – Philipp Nov 28 '22 at 15:28
  • Nice! Time to catch up on ABAP release notes. – Gert Beukema Nov 29 '22 at 11:44
  • Hi Everyone, yes I wanted to use UNION of course, but it should work also in smaller releases than 7.5, so I had to find some other solution. Sorry for not mentioning it in my question. It won't work with one select, I can see it now. –  Nov 30 '22 at 13:57

1 Answers1

4

It appears that what you want to do is create an SQL "Union": Select rows from two different database tables and put the results into one combined table. This is only possible if both SELECTs have the same fields. But you can usually accomplish that pretty easily by substituting the missing rows in each table by a constant value ('' in this example):

SELECT 
    key1
    '' as key2
    value1
    value2
    value3
    '' as value4
  FROM db1
UNION SELECT
    key1
    key2
    value1
    '' as value2
    '' as value3
    value4
  FROM db2
INTO CORRESPONDING FIELDS OF TABLE @it.

When you are using an older release (<7.50), then you can combine the data of multiple SELECTs by using APPENDING instead of INTO:

SELECT 
    key1
    value1
    value2
    value3
  FROM db1
INTO CORRESPONDING FIELDS OF TABLE @it.

SELECT
    key1
    key2
    value1
    value4
  FROM db2
APPENDING CORRESPONDING FIELDS OF TABLE @it.
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Thanks a lot, I cannot use UNION unfortunately (( wanted), but my change has to work also in smaller releases, too. –  Nov 30 '22 at 13:59
  • @Zsofia I added a solution that should work in older releases. In the future you might want to mention in your question if you need to support a system that wasn't patched for over 7 years. – Philipp Nov 30 '22 at 14:06