Internal tables declaration in ABAP

11,416

Sample 1 first declares a type ty_tab with some fields. ty_tab is not a table type, it is a locally defined flat structure type. The data declaration following the type definition defines a local variable with name x_Tab and type ty_tab. The third data declaration then uses the "like" keyword to create a table that has lines "like" the structure x_Tab.

Sample 2 begins again with the definition of a type. But instead of first declaring a structure the data definition defines a standard table of type ty_tab.

Sample 3, as hennes mentioned in his comment, doesn't actually define a table. It defines a local structure based on a structure or table defined in the SAP Data Dictionary, in this case the transparent table "sflight". If you wanted to create an internal standard table based on DDIC table sflight, you would have to change the statement to:

data t_sflight type standard table of sflight.

All three variants are valid. Variant 1 and 2 create the identical (identical as in 'same fields, same properties') internal table with different means. There is no best way, each variant can be used where it fits. If you need a table that looks like one that already exists in the DDIC, use #3. #1 and #2 seem redundant at a glance, but sometimes you may receive a structure as a parameter and now want a internal table with that structure, so you can use the "like" keyword in #1.

Have a look at the SAP Help pages for more information.

Share:
11,416
Andrei Georgescu
Author by

Andrei Georgescu

Updated on June 04, 2022

Comments

  • Andrei Georgescu
    Andrei Georgescu almost 2 years

    I just started learning ABAP and I came acroos some different ways of declaring internal tables but I don't understand the difference between these ways. Which one is the best way?

    Sample 1

    types: begin of ty_tab,
      field1,
      field 2,  
    end of ty_tab.
    
    data x_tab type ty_tab.
    data itab like standard table of x_Tab.
    

    Sample 2

    types: begin of ty_tab,
      field1,
      field2,
    end of ty_tab.
    
    types x_tab type standard table of ty_tab.
    data itab type x_tab.
    

    Sample 3

    data t_sflight type sflight.
    
  • Andrei Georgescu
    Andrei Georgescu over 9 years
    sample 1 creates an identical internal table as the sample2 but what are the different means if both are identical?
  • Dirk Trilsbeek
    Dirk Trilsbeek over 9 years
    what do you mean with that? The different means are the "like" and the "type" keyword in the table declaration.
  • hennes
    hennes over 9 years
    @DirkTrilsbeek Sample 3 does not create an internal table but a structure.
  • Dirk Trilsbeek
    Dirk Trilsbeek over 9 years
    you're right, the third sample is missing the table part of the declaration. I'm going to revise my answer.