Syntax for creating internal table from existing database table?
Asked Answered
L

3

8

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?

Litotes answered 8/6, 2016 at 18:13 Comment(0)
C
5

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. :)

Cadence answered 9/6, 2016 at 12:32 Comment(4)
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!Litotes
@Litotes Maybe not necessarily weird but by all means confusing, especially for a rookie in the ABAP field! Enjoy learning it!Cadence
@Litotes 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.Cadence
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).Litotes
C
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.

Conspire answered 8/6, 2016 at 18:40 Comment(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.Litotes
B
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

Betz answered 7/7, 2016 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.