7

I'm new to ABAP. Started learning about internal tables. I was reading on the ways to create internal tables.

I came across the following syntax to create an internal table from an existing database table:

data: it_mara type table of mara.

I'm confused since mara is a table and if both l.h.s and r.h.s are of the same type then shouldn't it be just:

data: it_mara type mara.

What is the need to convert mara into a table when it is already a table?

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
The-Droidster
  • 635
  • 7
  • 13

3 Answers3

4

Historical reasons (always a good guess...).

The original and nowadays obsolete way to declare a table (with a header line was DATA it_mara TYPE mara OCCURS 10. Without OCCURS, you didn't declare a table, so it became a structure. My guess would be that in order to maintain backwards compatibility, that wasn't changed when TYPE TABLE OF was introduced.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • 1
    Hi, thanks for your comment on OCCURS. That gave me a hint. But I was more interested in why the runtime treats mara as a structure instead of a table in the first place. @Jaggers's answer seems to address that point. – The-Droidster Jun 09 '16 at 15:29
4

MARA is a transparent table which means that it functions at the same time as the structure type MARA. This is the way SAP works. :)

Jagger
  • 10,350
  • 9
  • 51
  • 93
  • 1
    Yes, that seems to be the reason. I attached a debugger to the program today to see the type of 'mara'. It said 'flat structure' which is weird since mara is a table. But maybe that's how the SAP ABAP runtime works. Seems like it considers a table to be a array of fields(structure) when referenced by its name in declarations and definitions. The name then internally points to the underlying database table. Weird! – The-Droidster Jun 09 '16 at 15:05
  • 1
    @The-Droidster Maybe not necessarily weird but by all means confusing, especially for a rookie in the ABAP field! Enjoy learning it! – Jagger Jun 09 '16 at 15:07
  • @The-Droidster It becomes even funnier. Try the following program and see of what type the variable `MARA` is. Look that it is defined with `TABLES` keyword. `REPORT ZZY. TABLES MARA. START-OF-SELECTION. SELECT * FROM MARA UP TO 10 ROWS. WRITE / MARA-MATNR. ENDSELECT.` It shows also how the name `MARA` is overloaded in this case. – Jagger Jun 09 '16 at 15:20
  • 1
    Yes, I checked every occurrence of mara in the program today. It said 'flat structure'(even when the statement explicitly says 'TABLES' :D). Note: I can't actually run the code right now since I can only practice at my training institute(which has really bad trainers btw :D). – The-Droidster Jun 09 '16 at 15:24
1

SAP DDIC table (transparent table, pooled table, cluster table) function as a structure.

Internal table is a list of structure (= DDIC table) values.

In your example of SAP DDIC table MARA (General Material Data), we can define it as an internal table like

data: it_mara type STANDARD table of mara.

which creates an STANDARD internal table

data: it_mara type SORTED table of mara.

which creates an SORTED internal table

Klaus Hopp
  • 41
  • 2