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 between TYPE and LIKE?

Former Member
0 Likes
915

data: a type "a data element",

b like a.

I know the basic difference of these two statement. But what is the insight?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
666

Hi haojun Zhang.

When you take reference of objects that occupies memory ( data elements,variables decalared with DATA) then use <b>LIKE</b> and when you are taking ref. to a elementry data types(C,I,D,T...) or to a variables decalared with TYPES statement then we use <b>TYPE</b>

Regards

vikas vishnoi.

5 REPLIES 5
Read only

Former Member
0 Likes
666

Have a look at below links:

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb367a358411d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.htm

If you use the addition TYPE, you can either reference to an inbuilt ABAP type, a predefined elementary type in the local program, or to a data element defined in the ABAP Dictionary. If you use a LIKE reference, dobj can be an existing data object with an elementary data type. If you do not use the TYPE or LIKE addition, the system uses the default predefined type c.

In other words, LIKE means the datatype of the variable is similar to the referenced variable.

TYPE means it is a predefined data type.

Eg:

DATA int TYPE i.

Here int is of integer data type.

DATA var LIKE int.

var IS a variable having same data type of int. which in turn is integer.

You can find these helpful when you reference database table variables... You need not know what is the datatype defined.

Also it adds to FLEXIBILITY.

Whenever you make changes to your database tables and fields, that change is REFLECTED back to your program that is, You need not change all your program code when you change your table fields...

I hope it helps.

Best Regards,

Vibha

*Please mark all the helpful answers

Read only

former_member404244
Active Contributor
0 Likes
666

Hi,

in general the difference between type and like is

LIKE->it refers to the database object.for example

itab like mara occurs 0,

i_field like mara-matnr...

TYPE->it refers to the datatype.for example whther it is character,numeric etc..

in ur question, the field a refers to dataelement(means it is refering to the datatype ).

Regards,

Nagaraj

Read only

0 Likes
666

type -- refer to data type(may be user defined using types)

like -- refer to database fields.

For all practical purposes there are the same. The only additional advantage with types is that you can define your own types(including complex ones) in the data dictionary and reuse them accross various programs.

But within a program if two variables are defined one using LIKE and another using TYPE, both referring to the same field, then there is no difference.

If I include a type pool within a program, then I can define my variables only using TYPE to refer to any type defined in that pool. I cannot use LIKE in this scenario. Also, if I want to use native types like C, N, etc, I cannot use LIKE there either. I can use LIKE ABC only if ABC is in the database or if ABC is defined previously in the same program.

I can use TYPE ABC, if ABC is defined in database as a TYPE and included in the program with the statement TYPE-POOLS. I can use it, if it is the native types. I can use it, if it is already defined in the dictionary as a structure/table or structure/table field, or even if it is defined as a data element or a domain. So I can declare a variable V_BUKRS TYPE BUKRS, but I cannot define a variable V_BUKRS LIKE BUKRS.

But if I intend to use V_BUKRS to store company code, I will prefer to declare it as V_BUKRS LIKE T001-BUKRS, only because if tomorrow for some reason, the definition of T001-BUKRS changes to a data element for example, BUKRS_N(say DEC 4) instead of the data element BUKRS(CHAR 4) that it refers to now, I don't have to change my programs because I am referring to the table field and inhereting its properties. Whereas, had I declared my V_BUKRS TYPE BUKRS and the table now changed to BUKRS_N, I will be forced to change my program as there will be a type incompatability.

Read only

Former Member
0 Likes
667

Hi haojun Zhang.

When you take reference of objects that occupies memory ( data elements,variables decalared with DATA) then use <b>LIKE</b> and when you are taking ref. to a elementry data types(C,I,D,T...) or to a variables decalared with TYPES statement then we use <b>TYPE</b>

Regards

vikas vishnoi.

Read only

Former Member
0 Likes
666

thanks for all your answers