‎2006 Mar 09 11:07 AM
Hi All,
Wat is hte basic difference between "TYPE" and "like" declarations.
Regards
Saurabh.
‎2006 Mar 09 11:11 AM
Hi Saurabh,
There is not too much difference, except for TYPE might refer to a locally defined TYPES in your program, while LIKE always refers to the Data Dictionary.
Since LIKE is not allowed in the OO-context you should always use TYPE.
Regards,
John.
‎2006 Mar 09 11:09 AM
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...
Hope you understand and appreciate the flexibility that SAP provides to the programmers...
‎2006 Mar 09 11:11 AM
Hi,
like allow you to define a field in reference of a stucture, table, or internal table ....
type allow you to define a field in reference of a type, of a data element ....
toto like table_name-field_name ,
toto type type_type ...
Rgd
Frédéric
‎2006 Mar 09 11:11 AM
Hi Saurabh,
There is not too much difference, except for TYPE might refer to a locally defined TYPES in your program, while LIKE always refers to the Data Dictionary.
Since LIKE is not allowed in the OO-context you should always use TYPE.
Regards,
John.
‎2006 Mar 09 11:12 AM
hi
when u use LIKE the datatype of the variable is similar to the referenced variable.
With TYPE means it is referring to a predefined data type.
thanks,
priya
‎2006 Mar 09 11:13 AM
hi,
LIKE , we refer to only user defined data type.
type, we refer to standard and also to declarations which we made as standard with TYPES declaration.
examples:
1.data: p like i (wrong)
p type i (right) 'i' is standard type.
2. types: begin of typ,
sno type i,
sname,
end of typ.
data: itab <b>type</b> standard table of typ.
data: wa <b>like</b> line of itab
‎2006 Mar 09 11:13 AM
Hi saurabh,
TYPES <type>[<length>].
Default C(1).
<length> should only be used with the types C, N, P and X. Other types can only be created in the standard length.
Additions:
TYPE <knowntype>.
LIKE <f>.
Defines the new type. <f> - a database field or an already defined internal field.
For more check this link
http://www.geocities.com/victorav15/sapr3/abap.html?20066#itread
Cheers
Sunny
‎2006 Mar 09 11:14 AM
Hi,
Like and Type are much similar.
Only in OO concepts, use of like is forbidden.
By type, you can assign a predefined type to a variable or theuser defined type created by TYPES statement.
Hope this helps..
Regards,
Shashank
‎2006 Mar 09 11:15 AM
Hai Saurabh,
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 <b>types</b> 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.
Regards,
Srikanth.
Please Reward points if helpful.
‎2006 Mar 09 11:16 AM
type: is used to create new data type
like: is used to assign the existing data type to the variable.
modif id in parameters is used to send the variable data between
abap sessions.
regards
vinod
‎2006 Mar 09 11:17 AM
hi,
The main difference between TYPE and LIKE parameter when defining or declaring the object is that TYPE is used to refer existing DATA TYPE (elementary or structured or user defined) while LIKE is used to declare data objects with reference to existing DATA OBJECTS
Ex DATA: NUM TYPE I
NUM is a variable declared by DATA statement. Any variable, which you use in program, need to be declared before you use it and can be done by DATA statement.
Here variable is declared by referring to existing data type.
Variable can also be declared by referring existing data object.
Ex. We have already declared NUM by DATA statement.
DATA: PRICE LIKE NUM.
Here variable is declared by using LIKE parameter, which tells system that price has all the attributes of data object NUM i.e., PRICE is also of type I.
‎2006 Mar 09 11:18 AM
Hi saurabh,
1.
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.
2. try this code (just copy paste)
report abc.
types : char50(50) type c.
*----
type.
data : d1 type c, "--- native
d2 type n, "--- native
d25 type char50 , "----
User defined data type
d3 type bukrs, "---- data element / domain
d4 type persno, "---- data element / domain
d5 type t001, "---- table
d99 type c
.
data :
*l1 like c "----
Not Allowed
*l2 like n "----
Not Allowed
*l25 like char50 , "----
User defined data type
*l3 like bukrs "----
Not Allowed
*l4 like persno, "----
Not Allowed
l5 like t001 , "---- table
l99 like pa0001
.
I hope it helps.
regards,
amit M.
‎2006 Mar 09 11:20 AM
Hi Saurabh,
<b>TYPE</b>is used to refer to any data type <type> that is already known at this point in the program...
something like i,x,t,d,p..
<b>LIKE</b> can be used in the same ABAP statements as the TYPE to refer to any data object that is already visible at that point in the program...
A good example could be this..
data : b type p decimals 2.
data : a like b.
to define a lot of types with <b>type p decimals 2</b> you just use <b>LIKE b</b>
regards
satesh
‎2006 Mar 09 11:20 AM
Hi Joshi,
TYPE is used to refer to data types and user defined data types and it can also be used to refer to the dictionary objects as well.Eg
TYPES VAL1 TYPE I.
DATA VAL2 TYPE VAL1.
DATA MATNR TYPE MARA-MATNR.
LIKE is used to refer to data objects for eg.
DATA VAL1 TYPE I.
DATA VAL2 LIKE VAL1.
Regards,
Abdul Hakim
‎2011 Jul 28 10:33 AM