2
votes

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.
1
Sample 3 does not declare a table - t_sflight is a structure...vwegert

1 Answers

3
votes

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.