1
votes

I need to write a ABAP program using an inner join on the tables mara and makt. I understand the idea of using data declarations like this:

data: imatnr type mara-matnr,
      ematnr type makt-matnr.

select mara~matnr makt~matnr into (imatnr, ematnr) from mara left join makt on mara~matnr = makt~matnr.
  write: / imatnr, ematnr.
endselect.

In my exams I have to write a ABAP program without internal tables, field symbols JUST TABLES DEKLARATIONS. I made several attempts to do this but I found no answer. I tried something like this:

tables: mara, makt.


select * from mara inner join makt on mara~matnr = makt~matnr.
  write: / mara-matnr, makt-matnr.
endselect.

The console says that I have to use an INTO if I want to use an JOIN within a select * from statement.

So my question is: Is it possible to build an JOIN only with TABLES or do I need DATA: anyways?

2

2 Answers

4
votes

So my question is:

Is it possible to build an JOIN only with tables: mara, makt.

No. Table is a work area aka flat structure, keyword is flat. You cannot select multiple rows only with TABLES and cannot join TABLES workareas.

BTW, TABLES help guidelines clearly says

No table work areas except for classic dynpros

so forget this obsolete stuff.

If your exams and your learning courses require you to use only TABLES then run away from those course and those school. Please, run away.

P.S. The only way you can fulfill your nasty requirement is nested SELECTs:

TABLES: mara, makt.

SELECT * FROM mara
  INTO mara.
  WRITE: / `MARA selected: `, mara-matnr.
  SELECT *
    INTO makt
    FROM makt WHERE matnr = mara-matnr.
    WRITE: / `MAKT selected: `, makt-matnr.
  ENDSELECT.
ENDSELECT.

But that feels disgusting.

2
votes

Few distinctions between SQL selection queries that might help you.

A generic select statement (without any particular type) looks like this.

Select * from [table] where [field] = [value]

Depending on whether or not and how you specify into clause a certain type of select is used.


into table

into table selects one or multiple records into internal table. can only be used when your select clause (field list) is identical to structure of your internal table.

   data: lt_ekko type table of ekko,
         ls_ekko type table of ekko,
   select * from ekko into table @lt_ekko.
   loop at lt_ekko into ls_ekko.
      write: / ls_ekko-ebeln.
   endloop.

into

into means you are selecting into a variable (if you are selecting 1 column) or a structure (if you are selecting more than 1). This is important, because structures can only and variables only store 1 value or row, which means you must specify that you are selecting a single row or use select/endselect statements to perform looping select.

  data: ls_ekko type ekko.
  select single * from ekko into @ls_ekko where ebeln = [some number]
  write: / ls_ekko-ebeln.

OR

    select * from ekko into @ls_ekko where ebeln = [some number].
       write: / ls_ekko-ebeln. "will print one for each row
    endselect.

into corresponding fields of (table)

into corresponding fields of (and into corresponding fields of table) selects records the same way into and into internal table. The difference is that your structure or internal table does not have to be identical to your selected field list. Selected fields will be stored into your table/structure fields with same names.

  data: ls_ekko type ekko.
  select single ebeln, bukrs from ekko into @ls_ekko where ebeln = [some number].
  write: / ls_ekko-ebeln.

no into clause

no into clause is similar to into [structure], because it can only select 1 record. That means you have to specify that you select a single record. Note: Tables you are selecting from must be declared in the program to use this type of select.

  select single * from ekko where ebeln = [some number].
  write: / ekko-ebeln.

Since you are not using into clause, you are using the last type, and that means you must use select single