Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Difference

Former Member
0 Likes
872

Hello

while declaring data we use statement

TYPE , FOR ,LIKE

Please tell me the diff between them

with regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
820

Hi,

TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .

LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.

Rewards

Aneesh.

7 REPLIES 7
Read only

Former Member
0 Likes
821

Hi,

TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .

LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.

Rewards

Aneesh.

Read only

0 Likes
820

Not similar. Identical.

Use LIKE e.g. when you declare a local copy of a parameter to a form. As far as I know SAP's recommendation is now to use LIKE when you want to ensure and indicate (to later program maintainers) that a data object is identical in type to another data object. The main point being that they are not <i>accidentally</i> the same data type, it is deliberate (and thus usually important so you don't want things to change if someone changes the referred data object).

For everything else you should use TYPE, also in function module parameters. Previously you were forced to use LIKE for some of the function module parameters.

FOR is not used to declare data, only to declare selection screen parameters (ranges).


Kjetil Kilhavn (Vettug AS) - ABAP developer since Feb 2000, but will probably never be a Rockstar developer
Read only

Former Member
0 Likes
820

hi,

<b>TYPE</b>

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

<b>LIKE</b>

You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

<b>FOR</b>

We use FOR statement for processing the selection table.

regards,

Ashok reddy

Read only

Former Member
0 Likes
820

Hi,

Check this SAP help :

... TYPE REF TO type

Defining a typed data reference variable. Types you can use include elementary types, types defined using TYPES, or types created in the ABAP Dictionary. You can dereference completely typed data reference variables using the dereferencing operator ->* at any operand position.

Example

types:

LNTYP type I.

data:

CNT type ref to I,

LINE type ref to LNTYP,

FLIGHT type ref to SFLIGHT.

create data CNT.

create data LINE.

create data FLIGHT.

CNT->* = 20.

LINE->* = 110.

FLIGHT->FLDATE = SY-DATUM.

... TYPE type

Effect

The field f is created with type type. For the type, you can specify either one of the predefined types listed below, a type defined using the TYPES statement, or a type created in the ABAP Dictionary.

The standard length ( SL ) of a field depends on its type.

Type Explanation SL Initial value

C Text (Character) 1 space

N Numeric text 1 '00...0'

D Date (YYYYMMDD) 8 '00000000'

T Time (HHMMSS) 6 '000000'

X Hexadecimal (HeX code) 1 X'00'

I Integer 4 0

P Packed number 8 0

F Floating point number 8 0

STRING Character sequence (string) variable-length empty string

XSTRING Byte sequence (X string)

variable-length empty hexadecimal string

Example

DATA NUMBER TYPE I.

DATA WA_SPFLI TYPE SPFLI.

The field NUMBER is created with type I. You can now use it in the program. In particular, you can assign numeric values to the field and use it in calculations ( ABAP number types).

The field WA_SPFLI is created using the type of the database table SPFLI from the ABAP Dictionary. This field is structured and can be used especially for working with data from database table SPFLI.

... LIKE f1

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.

See Cannot Use LIKE References to Dictionary Types.

Effect

Field f is created with the same field attribtues as the data object f1, which has already been declared. Any data object (field, parameter, structure...) is allowed as long as its type has been fully specified.

f1 can be any ABAP Dictionary reference.

Example

DATA TABLE_INDEX LIKE SY-TABIX.

The field TABLE_INDEX now has the same attributes as SY-TABIX (index field for internal tables).

Note

You should use this addition whenever you can. If the type of a field to which you are referring changes, the ABAP runtime system updates all references automatically. It also stops the system from carrying out unnecessry (and maybe undesirable) type conversions.

FOR

We use FOR statement for processing the selection table.

Regards

Message was edited by:

skk

Read only

Former Member
0 Likes
820
Hi, Basavraj 

<b>TYPE</b> : it will allocate memory during execution (object type).
<b>Type will improve performance.</b>

When we want to create a data object of an existing data type we use TYPE
E.g. DATA gv_var1 TYPE i. “Create a data object of type integer.

TYPES – When u want to create a data type and not an object.
TYPES : ty_int TYPE i. “ ty_int is an integer type



<b>LIKE</b> : it will allocate memory immediately.
When we declare a variable, with reference to some object in memory, we use LIKE.

When we have a data object and u want to create a similar data object. 
E.g.DATA gv_var2 LIKE gv_var1. “gv_var2 is an integer like gv_var1.


Whenever you need any internal table in your program which should have same fields as in a database table, we can declare the internal table 

Using LIKE addition.

DATA gt_itab LIKE <dbtab> WITH HEADER LINE.

But like addition is not recommended as a coding standard, and so is WITH HEADER LINE addition.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
820

LIKE only describes the characteristics of that field type.(default memory not allocated and cant store default values )

Type does the same and also we can store default values using type (ie....Default memory is allocated when we use type )

Reward if useful. and close the thread.

Read only

Former Member
0 Likes
820

in the program, then in the ABAP Dictionary for a database table or flat structure with the same name. You can no longer use this kind of type reference in ABAP Objects classes. You should also avoid using the LIKE addition in other ABAP programs except to refer to data objects. To refer to data types, you should use the TYPE addition instead.

reward points if it is usefull ..

Girish